##// END OF EJS Templates
Speed up gallery loading a bit. Removed duplicate getting of post list in...
neko259 -
r467:a0c69560 default
parent child Browse files
Show More
@@ -1,136 +1,136 b''
1 1 {% extends "boards/base.html" %}
2 2
3 3 {% load i18n %}
4 4 {% load cache %}
5 5 {% load static from staticfiles %}
6 6 {% load board %}
7 7
8 8 {% block head %}
9 9 <title>Neboard - {{ thread.get_opening_post.get_title }}</title>
10 10 {% endblock %}
11 11
12 12 {% block content %}
13 13 {% spaceless %}
14 14 {% get_current_language as LANGUAGE_CODE %}
15 15
16 16 <script src="{% static 'js/thread_update.js' %}"></script>
17 17 <script src="{% static 'js/thread.js' %}"></script>
18 18
19 19 {% cache 600 thread_view thread.id thread.last_edit_time moderator LANGUAGE_CODE %}
20 20
21 21 <div class="image-mode-tab">
22 22 <a class="current_mode" href="{% url 'thread' thread.get_opening_post.id %}">{% trans 'Normal mode' %}</a>,
23 23 <a href="{% url 'thread_mode' thread.get_opening_post.id 'gallery' %}">{% trans 'Gallery mode' %}</a>
24 24 </div>
25 25
26 26 {% if bumpable %}
27 27 <div class="bar-bg">
28 28 <div class="bar-value" style="width:{{ bumplimit_progress }}%" id="bumplimit_progress">
29 29 </div>
30 30 <div class="bar-text">
31 31 <span id="left_to_limit">{{ posts_left }}</span> {% trans 'posts to bumplimit' %}
32 32 </div>
33 33 </div>
34 34 {% endif %}
35 35 <div class="thread">
36 {% for post in thread.get_replies %}
36 {% for post in posts %}
37 37 {% if bumpable %}
38 38 <div class="post" id="{{ post.id }}">
39 39 {% else %}
40 40 <div class="post dead_post" id="{{ post.id }}">
41 41 {% endif %}
42 42 {% if post.image %}
43 43 <div class="image">
44 44 <a
45 45 class="thumb"
46 46 href="{{ post.image.url }}"><img
47 47 src="{{ post.image.url_200x150 }}"
48 48 alt="{{ post.id }}"
49 49 width="{{ post.image_pre_width }}"
50 50 height="{{ post.image_pre_height }}"
51 51 data-width="{{ post.image_width }}"
52 52 data-height="{{ post.image_height }}"/>
53 53 </a>
54 54 </div>
55 55 {% endif %}
56 56 <div class="message">
57 57 <div class="post-info">
58 58 <span class="title">{{ post.title }}</span>
59 59 <a class="post_id" href="#{{ post.id }}">
60 60 ({{ post.id }})</a>
61 61 [{{ post.pub_time }}]
62 62 [<a href="#" onclick="javascript:addQuickReply('{{ post.id }}')
63 63 ; return false;">&gt;&gt;</a>]
64 64
65 65 {% if moderator %}
66 66 <span class="moderator_info">
67 67 [<a href="{% url 'delete' post_id=post.id %}"
68 68 >{% trans 'Delete' %}</a>]
69 69 ({{ post.poster_ip }})
70 70 [<a href="{% url 'ban' post_id=post.id %}?next={{ request.path }}"
71 71 >{% trans 'Ban IP' %}</a>]
72 72 </span>
73 73 {% endif %}
74 74 </div>
75 75 {% autoescape off %}
76 76 {{ post.text.rendered }}
77 77 {% endautoescape %}
78 78 {% if post.is_referenced %}
79 79 <div class="refmap">
80 80 {% trans "Replies" %}:
81 81 {% for ref_post in post.get_sorted_referenced_posts %}
82 82 <a href="{% post_url ref_post.id %}">&gt;&gt;{{ ref_post.id }}</a
83 83 >{% if not forloop.last %},{% endif %}
84 84 {% endfor %}
85 85 </div>
86 86 {% endif %}
87 87 </div>
88 88 {% if forloop.first %}
89 89 <div class="metadata">
90 90 <span class="tags">
91 91 {% for tag in thread.get_tags %}
92 92 <a class="tag" href="{% url 'tag' tag.name %}">
93 93 #{{ tag.name }}</a
94 94 >{% if not forloop.last %},{% endif %}
95 95 {% endfor %}
96 96 </span>
97 97 </div>
98 98 {% endif %}
99 99 </div>
100 100 {% endfor %}
101 101 </div>
102 102 {% endcache %}
103 103
104 104 <div class="post-form-w">
105 105 <script src="{% static 'js/panel.js' %}"></script>
106 106 <div class="form-title">{% trans "Reply to thread" %} #{{ thread.get_opening_post.id }}</div>
107 107 <div class="post-form">
108 108 <form id="form" enctype="multipart/form-data" method="post"
109 109 >{% csrf_token %}
110 110 {{ form.as_div }}
111 111 <div class="form-submit">
112 112 <input type="submit" value="{% trans "Post" %}"/>
113 113 </div>
114 114 </form>
115 115 <div><a href="{% url "staticpage" name="help" %}">
116 116 {% trans 'Text syntax' %}</a></div>
117 117 </div>
118 118 </div>
119 119
120 120 {% endspaceless %}
121 121 {% endblock %}
122 122
123 123 {% block metapanel %}
124 124
125 125 {% get_current_language as LANGUAGE_CODE %}
126 126
127 127 <span class="metapanel" data-last-update="{{ last_update }}">
128 128 {% cache 600 thread_meta thread.last_edit_time moderator LANGUAGE_CODE %}
129 129 <span id="reply-count">{{ thread.get_reply_count }}</span> {% trans 'replies' %},
130 130 <span id="image-count">{{ thread.get_images_count }}</span> {% trans 'images' %}.
131 131 {% trans 'Last update: ' %}{{ thread.last_edit_time }}
132 132 [<a href="rss/">RSS</a>]
133 133 {% endcache %}
134 134 </span>
135 135
136 136 {% endblock %}
@@ -1,66 +1,64 b''
1 1 {% extends "boards/base.html" %}
2 2
3 3 {% load i18n %}
4 4 {% load cache %}
5 5 {% load static from staticfiles %}
6 6 {% load board %}
7 7
8 8 {% block head %}
9 9 <title>Neboard - {{ thread.get_opening_post.get_title }}</title>
10 10 {% endblock %}
11 11
12 12 {% block content %}
13 13 {% spaceless %}
14 14 {% get_current_language as LANGUAGE_CODE %}
15 15
16 16 <script src="{% static 'js/thread.js' %}"></script>
17 17
18 18 {% cache 600 thread_gallery_view thread.id thread.last_edit_time LANGUAGE_CODE request.get_host %}
19 19 <div class="image-mode-tab">
20 20 <a href="{% url 'thread' thread.get_opening_post.id %}">{% trans 'Normal mode' %}</a>,
21 21 <a class="current_mode" href="{% url 'thread_mode' thread.get_opening_post.id 'gallery' %}">{% trans 'Gallery mode' %}</a>
22 22 </div>
23 23
24 24 <div id="posts-table">
25 {% for post in thread.get_replies %}
26 {% if post.image %}
27 <div class="gallery_image">
28 <div>
29 <a
30 class="thumb"
31 href="{{ post.image.url }}"><img
32 src="{{ post.image.url_200x150 }}"
33 alt="{{ post.id }}"
34 width="{{ post.image_pre_width }}"
35 height="{{ post.image_pre_height }}"
36 data-width="{{ post.image_width }}"
37 data-height="{{ post.image_height }}"/>
38 </a>
39 </div>
40 <div class="gallery_image_metadata">
41 {{ post.image_width }}x{{ post.image_height }}
42 {% image_actions post.image.url request.get_host %}
43 </div>
25 {% for post in posts %}
26 <div class="gallery_image">
27 <div>
28 <a
29 class="thumb"
30 href="{{ post.image.url }}"><img
31 src="{{ post.image.url_200x150 }}"
32 alt="{{ post.id }}"
33 width="{{ post.image_pre_width }}"
34 height="{{ post.image_pre_height }}"
35 data-width="{{ post.image_width }}"
36 data-height="{{ post.image_height }}"/>
37 </a>
44 38 </div>
45 {% endif %}
39 <div class="gallery_image_metadata">
40 {{ post.image_width }}x{{ post.image_height }}
41 {% image_actions post.image.url request.get_host %}
42 </div>
43 </div>
46 44 {% endfor %}
47 45 </div>
48 46 {% endcache %}
49 47
50 48 {% endspaceless %}
51 49 {% endblock %}
52 50
53 51 {% block metapanel %}
54 52
55 53 {% get_current_language as LANGUAGE_CODE %}
56 54
57 55 <span class="metapanel" data-last-update="{{ last_update }}">
58 56 {% cache 600 thread_meta thread.last_edit_time moderator LANGUAGE_CODE %}
59 57 <span id="reply-count">{{ thread.get_reply_count }}</span> {% trans 'replies' %},
60 58 <span id="image-count">{{ thread.get_images_count }}</span> {% trans 'images' %}.
61 59 {% trans 'Last update: ' %}{{ thread.last_edit_time }}
62 60 [<a href="rss/">RSS</a>]
63 61 {% endcache %}
64 62 </span>
65 63
66 64 {% endblock %}
@@ -1,559 +1,564 b''
1 1 __author__ = 'neko259'
2 2
3 3 import hashlib
4 4 import string
5 5 import time
6 6 import re
7 7
8 8 from django.core import serializers
9 9 from django.core.urlresolvers import reverse
10 10 from django.http import HttpResponseRedirect, Http404
11 11 from django.http.response import HttpResponse
12 12 from django.template import RequestContext
13 13 from django.shortcuts import render, redirect, get_object_or_404
14 14 from django.utils import timezone
15 15 from django.db import transaction
16 16 from django.views.decorators.cache import cache_page
17 17 from django.views.i18n import javascript_catalog
18 18
19 19 from boards import forms
20 20 import boards
21 21 from boards import utils
22 22 from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \
23 23 ThreadCaptchaForm, PostCaptchaForm, LoginForm, ModeratorSettingsForm
24 24 from boards.models import Post, Tag, Ban, User
25 25 from boards.models.post import SETTING_MODERATE, REGEX_REPLY
26 26 from boards.models.user import RANK_USER
27 27 from boards import authors
28 28 from boards.utils import get_client_ip
29 29 import neboard
30 30
31 31
32 32 BAN_REASON_SPAM = 'Autoban: spam bot'
33 33 MODE_GALLERY = 'gallery'
34 34 MODE_NORMAL = 'normal'
35 35
36 36
37 37 def index(request, page=0):
38 38 context = _init_default_context(request)
39 39
40 40 if utils.need_include_captcha(request):
41 41 threadFormClass = ThreadCaptchaForm
42 42 kwargs = {'request': request}
43 43 else:
44 44 threadFormClass = ThreadForm
45 45 kwargs = {}
46 46
47 47 if request.method == 'POST':
48 48 form = threadFormClass(request.POST, request.FILES,
49 49 error_class=PlainErrorList, **kwargs)
50 50 form.session = request.session
51 51
52 52 if form.is_valid():
53 53 return _new_post(request, form)
54 54 if form.need_to_ban:
55 55 # Ban user because he is suspected to be a bot
56 56 _ban_current_user(request)
57 57 else:
58 58 form = threadFormClass(error_class=PlainErrorList, **kwargs)
59 59
60 60 threads = []
61 61 for thread_to_show in Post.objects.get_threads(page=int(page)):
62 62 threads.append(_get_template_thread(thread_to_show))
63 63
64 64 # TODO Make this generic for tag and threads list pages
65 65 context['threads'] = None if len(threads) == 0 else threads
66 66 context['form'] = form
67 67 context['current_page'] = int(page)
68 68
69 69 page_count = Post.objects.get_thread_page_count()
70 70 context['pages'] = range(page_count)
71 71 page = int(page)
72 72 if page < page_count - 1:
73 73 context['next_page'] = str(page + 1)
74 74 if page > 0:
75 75 context['prev_page'] = str(page - 1)
76 76
77 77 return render(request, 'boards/posting_general.html',
78 78 context)
79 79
80 80
81 81 @transaction.atomic
82 82 def _new_post(request, form, opening_post=None):
83 83 """Add a new post (in thread or as a reply)."""
84 84
85 85 ip = get_client_ip(request)
86 86 is_banned = Ban.objects.filter(ip=ip).exists()
87 87
88 88 if is_banned:
89 89 return redirect(you_are_banned)
90 90
91 91 data = form.cleaned_data
92 92
93 93 title = data['title']
94 94 text = data['text']
95 95
96 96 text = _remove_invalid_links(text)
97 97
98 98 if 'image' in data.keys():
99 99 image = data['image']
100 100 else:
101 101 image = None
102 102
103 103 tags = []
104 104
105 105 if not opening_post:
106 106 tag_strings = data['tags']
107 107
108 108 if tag_strings:
109 109 tag_strings = tag_strings.split(' ')
110 110 for tag_name in tag_strings:
111 111 tag_name = string.lower(tag_name.strip())
112 112 if len(tag_name) > 0:
113 113 tag, created = Tag.objects.get_or_create(name=tag_name)
114 114 tags.append(tag)
115 115 post_thread = None
116 116 else:
117 117 post_thread = opening_post.thread_new
118 118
119 119 post = Post.objects.create_post(title=title, text=text, ip=ip,
120 120 thread=post_thread, image=image,
121 121 tags=tags, user=_get_user(request))
122 122
123 123 thread_to_show = (opening_post.id if opening_post else post.id)
124 124
125 125 if opening_post:
126 126 return redirect(reverse(thread, kwargs={'post_id': thread_to_show}) +
127 127 '#' + str(post.id))
128 128 else:
129 129 return redirect(thread, post_id=thread_to_show)
130 130
131 131
132 132 def tag(request, tag_name, page=0):
133 133 """
134 134 Get all tag threads. Threads are split in pages, so some page is
135 135 requested. Default page is 0.
136 136 """
137 137
138 138 tag = get_object_or_404(Tag, name=tag_name)
139 139 threads = []
140 140 for thread_to_show in Post.objects.get_threads(page=int(page), tag=tag):
141 141 threads.append(_get_template_thread(thread_to_show))
142 142
143 143 if request.method == 'POST':
144 144 form = ThreadForm(request.POST, request.FILES,
145 145 error_class=PlainErrorList)
146 146 form.session = request.session
147 147
148 148 if form.is_valid():
149 149 return _new_post(request, form)
150 150 if form.need_to_ban:
151 151 # Ban user because he is suspected to be a bot
152 152 _ban_current_user(request)
153 153 else:
154 154 form = forms.ThreadForm(initial={'tags': tag_name},
155 155 error_class=PlainErrorList)
156 156
157 157 context = _init_default_context(request)
158 158 context['threads'] = None if len(threads) == 0 else threads
159 159 context['tag'] = tag
160 160 context['current_page'] = int(page)
161 161
162 162 page_count = Post.objects.get_thread_page_count(tag=tag)
163 163 context['pages'] = range(page_count)
164 164 page = int(page)
165 165 if page < page_count - 1:
166 166 context['next_page'] = str(page + 1)
167 167 if page > 0:
168 168 context['prev_page'] = str(page - 1)
169 169
170 170 context['form'] = form
171 171
172 172 return render(request, 'boards/posting_general.html',
173 173 context)
174 174
175 175
176 176 def thread(request, post_id, mode=MODE_NORMAL):
177 177 """Get all thread posts"""
178 178
179 179 if utils.need_include_captcha(request):
180 180 postFormClass = PostCaptchaForm
181 181 kwargs = {'request': request}
182 182 else:
183 183 postFormClass = PostForm
184 184 kwargs = {}
185 185
186 186 if request.method == 'POST':
187 187 form = postFormClass(request.POST, request.FILES,
188 188 error_class=PlainErrorList, **kwargs)
189 189 form.session = request.session
190 190
191 191 opening_post = get_object_or_404(Post, id=post_id)
192 192 if form.is_valid():
193 193 return _new_post(request, form, opening_post)
194 194 if form.need_to_ban:
195 195 # Ban user because he is suspected to be a bot
196 196 _ban_current_user(request)
197 197 else:
198 198 form = postFormClass(error_class=PlainErrorList, **kwargs)
199 199
200 200 thread_to_show = get_object_or_404(Post, id=post_id).thread_new
201 201
202 202 context = _init_default_context(request)
203 203
204 204 posts = thread_to_show.get_replies()
205 205 context['form'] = form
206 context['bumpable'] = thread_to_show.can_bump()
207 if context['bumpable']:
208 context['posts_left'] = neboard.settings.MAX_POSTS_PER_THREAD - posts \
209 .count()
210 context['bumplimit_progress'] = str(
211 float(context['posts_left']) /
212 neboard.settings.MAX_POSTS_PER_THREAD * 100)
213 206 context["last_update"] = _datetime_to_epoch(thread_to_show.last_edit_time)
214 207 context["thread"] = thread_to_show
215 208
216 209 if MODE_NORMAL == mode:
210 context['bumpable'] = thread_to_show.can_bump()
211 if context['bumpable']:
212 context['posts_left'] = neboard.settings.MAX_POSTS_PER_THREAD - posts \
213 .count()
214 context['bumplimit_progress'] = str(
215 float(context['posts_left']) /
216 neboard.settings.MAX_POSTS_PER_THREAD * 100)
217
218 context['posts'] = posts
219
217 220 document = 'boards/thread.html'
218 221 elif MODE_GALLERY == mode:
222 context['posts'] = posts.filter(image_width__gt=0)
223
219 224 document = 'boards/thread_gallery.html'
220 225 else:
221 226 raise Http404
222 227
223 228 return render(request, document, context)
224 229
225 230
226 231 def login(request):
227 232 """Log in with user id"""
228 233
229 234 context = _init_default_context(request)
230 235
231 236 if request.method == 'POST':
232 237 form = LoginForm(request.POST, request.FILES,
233 238 error_class=PlainErrorList)
234 239 form.session = request.session
235 240
236 241 if form.is_valid():
237 242 user = User.objects.get(user_id=form.cleaned_data['user_id'])
238 243 request.session['user_id'] = user.id
239 244 return redirect(index)
240 245
241 246 else:
242 247 form = LoginForm()
243 248
244 249 context['form'] = form
245 250
246 251 return render(request, 'boards/login.html', context)
247 252
248 253
249 254 def settings(request):
250 255 """User's settings"""
251 256
252 257 context = _init_default_context(request)
253 258 user = _get_user(request)
254 259 is_moderator = user.is_moderator()
255 260
256 261 if request.method == 'POST':
257 262 with transaction.atomic():
258 263 if is_moderator:
259 264 form = ModeratorSettingsForm(request.POST,
260 265 error_class=PlainErrorList)
261 266 else:
262 267 form = SettingsForm(request.POST, error_class=PlainErrorList)
263 268
264 269 if form.is_valid():
265 270 selected_theme = form.cleaned_data['theme']
266 271
267 272 user.save_setting('theme', selected_theme)
268 273
269 274 if is_moderator:
270 275 moderate = form.cleaned_data['moderate']
271 276 user.save_setting(SETTING_MODERATE, moderate)
272 277
273 278 return redirect(settings)
274 279 else:
275 280 selected_theme = _get_theme(request)
276 281
277 282 if is_moderator:
278 283 form = ModeratorSettingsForm(initial={'theme': selected_theme,
279 284 'moderate': context['moderator']},
280 285 error_class=PlainErrorList)
281 286 else:
282 287 form = SettingsForm(initial={'theme': selected_theme},
283 288 error_class=PlainErrorList)
284 289
285 290 context['form'] = form
286 291
287 292 return render(request, 'boards/settings.html', context)
288 293
289 294
290 295 def all_tags(request):
291 296 """All tags list"""
292 297
293 298 context = _init_default_context(request)
294 299 context['all_tags'] = Tag.objects.get_not_empty_tags()
295 300
296 301 return render(request, 'boards/tags.html', context)
297 302
298 303
299 304 def jump_to_post(request, post_id):
300 305 """Determine thread in which the requested post is and open it's page"""
301 306
302 307 post = get_object_or_404(Post, id=post_id)
303 308
304 309 if not post.thread:
305 310 return redirect(thread, post_id=post.id)
306 311 else:
307 312 return redirect(reverse(thread, kwargs={'post_id': post.thread.id})
308 313 + '#' + str(post.id))
309 314
310 315
311 316 def authors(request):
312 317 """Show authors list"""
313 318
314 319 context = _init_default_context(request)
315 320 context['authors'] = boards.authors.authors
316 321
317 322 return render(request, 'boards/authors.html', context)
318 323
319 324
320 325 @transaction.atomic
321 326 def delete(request, post_id):
322 327 """Delete post"""
323 328
324 329 user = _get_user(request)
325 330 post = get_object_or_404(Post, id=post_id)
326 331
327 332 if user.is_moderator():
328 333 # TODO Show confirmation page before deletion
329 334 Post.objects.delete_post(post)
330 335
331 336 if not post.thread:
332 337 return _redirect_to_next(request)
333 338 else:
334 339 return redirect(thread, post_id=post.thread.id)
335 340
336 341
337 342 @transaction.atomic
338 343 def ban(request, post_id):
339 344 """Ban user"""
340 345
341 346 user = _get_user(request)
342 347 post = get_object_or_404(Post, id=post_id)
343 348
344 349 if user.is_moderator():
345 350 # TODO Show confirmation page before ban
346 351 ban, created = Ban.objects.get_or_create(ip=post.poster_ip)
347 352 if created:
348 353 ban.reason = 'Banned for post ' + str(post_id)
349 354 ban.save()
350 355
351 356 return _redirect_to_next(request)
352 357
353 358
354 359 def you_are_banned(request):
355 360 """Show the page that notifies that user is banned"""
356 361
357 362 context = _init_default_context(request)
358 363
359 364 ban = get_object_or_404(Ban, ip=utils.get_client_ip(request))
360 365 context['ban_reason'] = ban.reason
361 366 return render(request, 'boards/staticpages/banned.html', context)
362 367
363 368
364 369 def page_404(request):
365 370 """Show page 404 (not found error)"""
366 371
367 372 context = _init_default_context(request)
368 373 return render(request, 'boards/404.html', context)
369 374
370 375
371 376 @transaction.atomic
372 377 def tag_subscribe(request, tag_name):
373 378 """Add tag to favorites"""
374 379
375 380 user = _get_user(request)
376 381 tag = get_object_or_404(Tag, name=tag_name)
377 382
378 383 if not tag in user.fav_tags.all():
379 384 user.add_tag(tag)
380 385
381 386 return _redirect_to_next(request)
382 387
383 388
384 389 @transaction.atomic
385 390 def tag_unsubscribe(request, tag_name):
386 391 """Remove tag from favorites"""
387 392
388 393 user = _get_user(request)
389 394 tag = get_object_or_404(Tag, name=tag_name)
390 395
391 396 if tag in user.fav_tags.all():
392 397 user.remove_tag(tag)
393 398
394 399 return _redirect_to_next(request)
395 400
396 401
397 402 def static_page(request, name):
398 403 """Show a static page that needs only tags list and a CSS"""
399 404
400 405 context = _init_default_context(request)
401 406 return render(request, 'boards/staticpages/' + name + '.html', context)
402 407
403 408
404 409 def api_get_post(request, post_id):
405 410 """
406 411 Get the JSON of a post. This can be
407 412 used as and API for external clients.
408 413 """
409 414
410 415 post = get_object_or_404(Post, id=post_id)
411 416
412 417 json = serializers.serialize("json", [post], fields=(
413 418 "pub_time", "_text_rendered", "title", "text", "image",
414 419 "image_width", "image_height", "replies", "tags"
415 420 ))
416 421
417 422 return HttpResponse(content=json)
418 423
419 424
420 425 def get_post(request, post_id):
421 426 """Get the html of a post. Used for popups."""
422 427
423 428 post = get_object_or_404(Post, id=post_id)
424 429 thread = post.thread_new
425 430
426 431 context = RequestContext(request)
427 432 context["post"] = post
428 433 context["can_bump"] = thread.can_bump()
429 434 if "truncated" in request.GET:
430 435 context["truncated"] = True
431 436
432 437 return render(request, 'boards/post.html', context)
433 438
434 439 @cache_page(86400)
435 440 def cached_js_catalog(request, domain='djangojs', packages=None):
436 441 return javascript_catalog(request, domain, packages)
437 442
438 443
439 444 def _get_theme(request, user=None):
440 445 """Get user's CSS theme"""
441 446
442 447 if not user:
443 448 user = _get_user(request)
444 449 theme = user.get_setting('theme')
445 450 if not theme:
446 451 theme = neboard.settings.DEFAULT_THEME
447 452
448 453 return theme
449 454
450 455
451 456 def _init_default_context(request):
452 457 """Create context with default values that are used in most views"""
453 458
454 459 context = RequestContext(request)
455 460
456 461 user = _get_user(request)
457 462 context['user'] = user
458 463 context['tags'] = user.get_sorted_fav_tags()
459 464 context['posts_per_day'] = float(Post.objects.get_posts_per_day())
460 465
461 466 theme = _get_theme(request, user)
462 467 context['theme'] = theme
463 468 context['theme_css'] = 'css/' + theme + '/base_page.css'
464 469
465 470 # This shows the moderator panel
466 471 moderate = user.get_setting(SETTING_MODERATE)
467 472 if moderate == 'True':
468 473 context['moderator'] = user.is_moderator()
469 474 else:
470 475 context['moderator'] = False
471 476
472 477 return context
473 478
474 479
475 480 def _get_user(request):
476 481 """
477 482 Get current user from the session. If the user does not exist, create
478 483 a new one.
479 484 """
480 485
481 486 session = request.session
482 487 if not 'user_id' in session:
483 488 request.session.save()
484 489
485 490 md5 = hashlib.md5()
486 491 md5.update(session.session_key)
487 492 new_id = md5.hexdigest()
488 493
489 494 time_now = timezone.now()
490 495 user = User.objects.create(user_id=new_id, rank=RANK_USER,
491 496 registration_time=time_now)
492 497
493 498 session['user_id'] = user.id
494 499 else:
495 500 user = User.objects.get(id=session['user_id'])
496 501
497 502 return user
498 503
499 504
500 505 def _redirect_to_next(request):
501 506 """
502 507 If a 'next' parameter was specified, redirect to the next page. This is
503 508 used when the user is required to return to some page after the current
504 509 view has finished its work.
505 510 """
506 511
507 512 if 'next' in request.GET:
508 513 next_page = request.GET['next']
509 514 return HttpResponseRedirect(next_page)
510 515 else:
511 516 return redirect(index)
512 517
513 518
514 519 @transaction.atomic
515 520 def _ban_current_user(request):
516 521 """Add current user to the IP ban list"""
517 522
518 523 ip = utils.get_client_ip(request)
519 524 ban, created = Ban.objects.get_or_create(ip=ip)
520 525 if created:
521 526 ban.can_read = False
522 527 ban.reason = BAN_REASON_SPAM
523 528 ban.save()
524 529
525 530
526 531 def _remove_invalid_links(text):
527 532 """
528 533 Replace invalid links in posts so that they won't be parsed.
529 534 Invalid links are links to non-existent posts
530 535 """
531 536
532 537 for reply_number in re.finditer(REGEX_REPLY, text):
533 538 post_id = reply_number.group(1)
534 539 post = Post.objects.filter(id=post_id)
535 540 if not post.exists():
536 541 text = string.replace(text, '>>' + post_id, post_id)
537 542
538 543 return text
539 544
540 545
541 546 def _datetime_to_epoch(datetime):
542 547 return int(time.mktime(timezone.localtime(
543 548 datetime,timezone.get_current_timezone()).timetuple())
544 549 * 1000000 + datetime.microsecond)
545 550
546 551
547 552 def _get_template_thread(thread_to_show):
548 553 """Get template values for thread"""
549 554
550 555 last_replies = thread_to_show.get_last_replies()
551 556 skipped_replies_count = thread_to_show.get_replies().count() \
552 557 - len(last_replies) - 1
553 558 return {
554 559 'thread': thread_to_show,
555 560 'op': thread_to_show.get_replies()[0],
556 561 'bumpable': thread_to_show.can_bump(),
557 562 'last_replies': last_replies,
558 563 'skipped_replies': skipped_replies_count,
559 564 }
General Comments 0
You need to be logged in to leave comments. Login now