Show More
@@ -1,283 +1,284 b'' | |||
|
1 | 1 | __author__ = 'neko259' |
|
2 | 2 | |
|
3 | 3 | import hashlib |
|
4 | 4 | |
|
5 | 5 | from django.core import serializers |
|
6 | 6 | from django.core.urlresolvers import reverse |
|
7 | 7 | from django.http import HttpResponseRedirect |
|
8 | 8 | from django.http.response import HttpResponse |
|
9 | 9 | from django.template import RequestContext |
|
10 | 10 | from django.shortcuts import render, redirect, get_object_or_404 |
|
11 | 11 | from django.utils import timezone |
|
12 | 12 | from django.db import transaction |
|
13 | 13 | from django.views.decorators.cache import cache_page |
|
14 | 14 | from django.views.i18n import javascript_catalog |
|
15 | 15 | |
|
16 | 16 | import boards |
|
17 | 17 | from boards.forms import SettingsForm, PlainErrorList, \ |
|
18 | 18 | ModeratorSettingsForm |
|
19 | 19 | from boards.models import Post, Tag, Ban, User |
|
20 | 20 | from boards.models.post import SETTING_MODERATE |
|
21 | 21 | from boards.models.user import RANK_USER |
|
22 | 22 | from boards import authors |
|
23 | 23 | import neboard |
|
24 | 24 | import all_threads |
|
25 | 25 | |
|
26 | 26 | |
|
27 | 27 | BAN_REASON_SPAM = 'Autoban: spam bot' |
|
28 | 28 | |
|
29 | 29 | DEFAULT_PAGE = 1 |
|
30 | 30 | |
|
31 | 31 | |
|
32 | 32 | def settings(request): |
|
33 | 33 | """User's settings""" |
|
34 | 34 | |
|
35 | 35 | context = _init_default_context(request) |
|
36 | 36 | user = _get_user(request) |
|
37 | 37 | is_moderator = user.is_moderator() |
|
38 | 38 | |
|
39 | 39 | if request.method == 'POST': |
|
40 | 40 | with transaction.atomic(): |
|
41 | 41 | if is_moderator: |
|
42 | 42 | form = ModeratorSettingsForm(request.POST, |
|
43 | 43 | error_class=PlainErrorList) |
|
44 | 44 | else: |
|
45 | 45 | form = SettingsForm(request.POST, error_class=PlainErrorList) |
|
46 | 46 | |
|
47 | 47 | if form.is_valid(): |
|
48 | 48 | selected_theme = form.cleaned_data['theme'] |
|
49 | 49 | |
|
50 | 50 | user.save_setting('theme', selected_theme) |
|
51 | 51 | |
|
52 | 52 | if is_moderator: |
|
53 | 53 | moderate = form.cleaned_data['moderate'] |
|
54 | 54 | user.save_setting(SETTING_MODERATE, moderate) |
|
55 | 55 | |
|
56 | 56 | return redirect(settings) |
|
57 | 57 | else: |
|
58 | 58 | selected_theme = _get_theme(request) |
|
59 | 59 | |
|
60 | 60 | if is_moderator: |
|
61 | 61 | form = ModeratorSettingsForm(initial={'theme': selected_theme, |
|
62 | 62 | 'moderate': context['moderator']}, |
|
63 | 63 | error_class=PlainErrorList) |
|
64 | 64 | else: |
|
65 | 65 | form = SettingsForm(initial={'theme': selected_theme}, |
|
66 | 66 | error_class=PlainErrorList) |
|
67 | 67 | |
|
68 | 68 | context['form'] = form |
|
69 | 69 | |
|
70 | 70 | return render(request, 'boards/settings.html', context) |
|
71 | 71 | |
|
72 | 72 | |
|
73 | 73 | def all_tags(request): |
|
74 | 74 | """All tags list""" |
|
75 | 75 | |
|
76 | 76 | context = _init_default_context(request) |
|
77 | 77 | context['all_tags'] = Tag.objects.get_not_empty_tags() |
|
78 | 78 | |
|
79 | 79 | return render(request, 'boards/tags.html', context) |
|
80 | 80 | |
|
81 | 81 | |
|
82 | 82 | def jump_to_post(request, post_id): |
|
83 | 83 | """Determine thread in which the requested post is and open it's page""" |
|
84 | 84 | |
|
85 | 85 | post = get_object_or_404(Post, id=post_id) |
|
86 | 86 | |
|
87 | 87 | if not post.thread: |
|
88 | 88 | return redirect('thread', post_id=post.id) |
|
89 | 89 | else: |
|
90 | 90 | return redirect(reverse('thread', kwargs={'post_id': post.thread.id}) |
|
91 | 91 | + '#' + str(post.id)) |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | def authors(request): |
|
95 | 95 | """Show authors list""" |
|
96 | 96 | |
|
97 | 97 | context = _init_default_context(request) |
|
98 | 98 | context['authors'] = boards.authors.authors |
|
99 | 99 | |
|
100 | 100 | return render(request, 'boards/authors.html', context) |
|
101 | 101 | |
|
102 | 102 | |
|
103 | 103 | @transaction.atomic |
|
104 | 104 | def delete(request, post_id): |
|
105 | 105 | """Delete post""" |
|
106 | 106 | |
|
107 | 107 | user = _get_user(request) |
|
108 | 108 | post = get_object_or_404(Post, id=post_id) |
|
109 | 109 | |
|
110 | 110 | if user.is_moderator(): |
|
111 | 111 | # TODO Show confirmation page before deletion |
|
112 | 112 | Post.objects.delete_post(post) |
|
113 | 113 | |
|
114 | 114 | if not post.thread: |
|
115 | 115 | return _redirect_to_next(request) |
|
116 | 116 | else: |
|
117 | 117 | return redirect('thread', post_id=post.thread.id) |
|
118 | 118 | |
|
119 | 119 | |
|
120 | 120 | @transaction.atomic |
|
121 | 121 | def ban(request, post_id): |
|
122 | 122 | """Ban user""" |
|
123 | 123 | |
|
124 | 124 | user = _get_user(request) |
|
125 | 125 | post = get_object_or_404(Post, id=post_id) |
|
126 | 126 | |
|
127 | 127 | if user.is_moderator(): |
|
128 | 128 | # TODO Show confirmation page before ban |
|
129 | 129 | ban, created = Ban.objects.get_or_create(ip=post.poster_ip) |
|
130 | 130 | if created: |
|
131 | 131 | ban.reason = 'Banned for post ' + str(post_id) |
|
132 | 132 | ban.save() |
|
133 | 133 | |
|
134 | 134 | return _redirect_to_next(request) |
|
135 | 135 | |
|
136 | 136 | |
|
137 | 137 | def page_404(request): |
|
138 | 138 | """Show page 404 (not found error)""" |
|
139 | 139 | |
|
140 | 140 | context = _init_default_context(request) |
|
141 | 141 | return render(request, 'boards/404.html', context) |
|
142 | 142 | |
|
143 | 143 | |
|
144 | 144 | @transaction.atomic |
|
145 | 145 | def tag_subscribe(request, tag_name): |
|
146 | 146 | """Add tag to favorites""" |
|
147 | 147 | |
|
148 | 148 | user = _get_user(request) |
|
149 | 149 | tag = get_object_or_404(Tag, name=tag_name) |
|
150 | 150 | |
|
151 | 151 | if not tag in user.fav_tags.all(): |
|
152 | 152 | user.add_tag(tag) |
|
153 | 153 | |
|
154 | 154 | return _redirect_to_next(request) |
|
155 | 155 | |
|
156 | 156 | |
|
157 | 157 | @transaction.atomic |
|
158 | 158 | def tag_unsubscribe(request, tag_name): |
|
159 | 159 | """Remove tag from favorites""" |
|
160 | 160 | |
|
161 | 161 | user = _get_user(request) |
|
162 | 162 | tag = get_object_or_404(Tag, name=tag_name) |
|
163 | 163 | |
|
164 | 164 | if tag in user.fav_tags.all(): |
|
165 | 165 | user.remove_tag(tag) |
|
166 | 166 | |
|
167 | 167 | return _redirect_to_next(request) |
|
168 | 168 | |
|
169 | 169 | |
|
170 | 170 | def static_page(request, name): |
|
171 | 171 | """Show a static page that needs only tags list and a CSS""" |
|
172 | 172 | |
|
173 | 173 | context = _init_default_context(request) |
|
174 | 174 | return render(request, 'boards/staticpages/' + name + '.html', context) |
|
175 | 175 | |
|
176 | 176 | |
|
177 | # TODO This has to be moved under the api module | |
|
177 | 178 | def api_get_post(request, post_id): |
|
178 | 179 | """ |
|
179 | 180 | Get the JSON of a post. This can be |
|
180 | 181 | used as and API for external clients. |
|
181 | 182 | """ |
|
182 | 183 | |
|
183 | 184 | post = get_object_or_404(Post, id=post_id) |
|
184 | 185 | |
|
185 | 186 | json = serializers.serialize("json", [post], fields=( |
|
186 | 187 | "pub_time", "_text_rendered", "title", "text", "image", |
|
187 | 188 | "image_width", "image_height", "replies", "tags" |
|
188 | 189 | )) |
|
189 | 190 | |
|
190 | 191 | return HttpResponse(content=json) |
|
191 | 192 | |
|
192 | 193 | |
|
193 | 194 | @cache_page(86400) |
|
194 | 195 | def cached_js_catalog(request, domain='djangojs', packages=None): |
|
195 | 196 | return javascript_catalog(request, domain, packages) |
|
196 | 197 | |
|
197 | 198 | |
|
198 | 199 | # TODO This method is deprecated and should be removed after switching to |
|
199 | 200 | # class-based view |
|
200 | 201 | def _get_theme(request, user=None): |
|
201 | 202 | """Get user's CSS theme""" |
|
202 | 203 | |
|
203 | 204 | if not user: |
|
204 | 205 | user = _get_user(request) |
|
205 | 206 | theme = user.get_setting('theme') |
|
206 | 207 | if not theme: |
|
207 | 208 | theme = neboard.settings.DEFAULT_THEME |
|
208 | 209 | |
|
209 | 210 | return theme |
|
210 | 211 | |
|
211 | 212 | |
|
212 | 213 | # TODO This method is deprecated and should be removed after switching to |
|
213 | 214 | # class-based view |
|
214 | 215 | def _init_default_context(request): |
|
215 | 216 | """Create context with default values that are used in most views""" |
|
216 | 217 | |
|
217 | 218 | context = RequestContext(request) |
|
218 | 219 | |
|
219 | 220 | user = _get_user(request) |
|
220 | 221 | context['user'] = user |
|
221 | 222 | context['tags'] = user.get_sorted_fav_tags() |
|
222 | 223 | context['posts_per_day'] = float(Post.objects.get_posts_per_day()) |
|
223 | 224 | |
|
224 | 225 | theme = _get_theme(request, user) |
|
225 | 226 | context['theme'] = theme |
|
226 | 227 | context['theme_css'] = 'css/' + theme + '/base_page.css' |
|
227 | 228 | |
|
228 | 229 | # This shows the moderator panel |
|
229 | 230 | moderate = user.get_setting(SETTING_MODERATE) |
|
230 | 231 | if moderate == 'True': |
|
231 | 232 | context['moderator'] = user.is_moderator() |
|
232 | 233 | else: |
|
233 | 234 | context['moderator'] = False |
|
234 | 235 | |
|
235 | 236 | return context |
|
236 | 237 | |
|
237 | 238 | |
|
238 | 239 | # TODO This method is deprecated and should be removed after switching to |
|
239 | 240 | # class-based view |
|
240 | 241 | def _get_user(request): |
|
241 | 242 | """ |
|
242 | 243 | Get current user from the session. If the user does not exist, create |
|
243 | 244 | a new one. |
|
244 | 245 | """ |
|
245 | 246 | |
|
246 | 247 | session = request.session |
|
247 | 248 | if not 'user_id' in session: |
|
248 | 249 | request.session.save() |
|
249 | 250 | |
|
250 | 251 | md5 = hashlib.md5() |
|
251 | 252 | md5.update(session.session_key) |
|
252 | 253 | new_id = md5.hexdigest() |
|
253 | 254 | |
|
254 | 255 | while User.objects.filter(user_id=new_id).exists(): |
|
255 | 256 | md5.update(str(timezone.now())) |
|
256 | 257 | new_id = md5.hexdigest() |
|
257 | 258 | |
|
258 | 259 | time_now = timezone.now() |
|
259 | 260 | user = User.objects.create(user_id=new_id, rank=RANK_USER, |
|
260 | 261 | registration_time=time_now) |
|
261 | 262 | |
|
262 | 263 | # TODO This is just a test. This method should be removed |
|
263 | 264 | # _delete_old_users() |
|
264 | 265 | |
|
265 | 266 | session['user_id'] = user.id |
|
266 | 267 | else: |
|
267 | 268 | user = User.objects.get(id=session['user_id']) |
|
268 | 269 | |
|
269 | 270 | return user |
|
270 | 271 | |
|
271 | 272 | |
|
272 | 273 | def _redirect_to_next(request): |
|
273 | 274 | """ |
|
274 | 275 | If a 'next' parameter was specified, redirect to the next page. This is |
|
275 | 276 | used when the user is required to return to some page after the current |
|
276 | 277 | view has finished its work. |
|
277 | 278 | """ |
|
278 | 279 | |
|
279 | 280 | if 'next' in request.GET: |
|
280 | 281 | next_page = request.GET['next'] |
|
281 | 282 | return HttpResponseRedirect(next_page) |
|
282 | 283 | else: |
|
283 | return redirect('index') No newline at end of file | |
|
284 | return redirect('index') |
General Comments 0
You need to be logged in to leave comments.
Login now