##// END OF EJS Templates
Refactoring
neko259 -
r917:3aa27a4d default
parent child Browse files
Show More
@@ -8,7 +8,6 from django.core.cache import cache
8 8 from django.core.urlresolvers import reverse
9 9 from django.db import models, transaction
10 10 from django.db.models import TextField
11 from django.template import RequestContext
12 11 from django.template.loader import render_to_string
13 12 from django.utils import timezone
14 13
@@ -19,6 +18,7 from boards.models.base import Viewable
19 18 from boards.models.thread import Thread
20 19 from boards.utils import datetime_to_epoch
21 20
21
22 22 WS_NOTIFICATION_TYPE_NEW_POST = 'new_post'
23 23 WS_NOTIFICATION_TYPE = 'notification_type'
24 24
@@ -49,6 +49,13 PARAMETER_TRUNCATED = 'truncated'
49 49 PARAMETER_TAG = 'tag'
50 50 PARAMETER_OFFSET = 'offset'
51 51 PARAMETER_DIFF_TYPE = 'type'
52 PARAMETER_BUMPABLE = 'bumpable'
53 PARAMETER_THREAD = 'thread'
54 PARAMETER_IS_OPENING = 'is_opening'
55 PARAMETER_MODERATOR = 'moderator'
56 PARAMETER_POST = 'post'
57 PARAMETER_OP_ID = 'opening_post_id'
58 PARAMETER_NEED_OPEN_LINK = 'need_open_link'
52 59
53 60 DIFF_TYPE_HTML = 'html'
54 61 DIFF_TYPE_JSON = 'json'
@@ -307,20 +314,15 class Post(models.Model, Viewable):
307 314
308 315 def get_view(self, moderator=False, need_open_link=False,
309 316 truncated=False, *args, **kwargs):
310 if 'is_opening' in kwargs:
311 is_opening = kwargs['is_opening']
312 else:
313 is_opening = self.is_opening()
317 """
318 Renders post's HTML view. Some of the post params can be passed over
319 kwargs for the means of caching (if we view the thread, some params
320 are same for every post and don't need to be computed over and over.
321 """
314 322
315 if 'thread' in kwargs:
316 thread = kwargs['thread']
317 else:
318 thread = self.get_thread()
319
320 if 'can_bump' in kwargs:
321 can_bump = kwargs['can_bump']
322 else:
323 can_bump = thread.can_bump()
323 is_opening = kwargs.get(PARAMETER_IS_OPENING, self.is_opening())
324 thread = kwargs.get(PARAMETER_THREAD, self.get_thread())
325 can_bump = kwargs.get(PARAMETER_BUMPABLE, thread.can_bump())
324 326
325 327 if is_opening:
326 328 opening_post_id = self.id
@@ -328,14 +330,14 class Post(models.Model, Viewable):
328 330 opening_post_id = thread.get_opening_post_id()
329 331
330 332 return render_to_string('boards/post.html', {
331 'post': self,
332 'moderator': moderator,
333 'is_opening': is_opening,
334 'thread': thread,
335 'bumpable': can_bump,
336 'need_open_link': need_open_link,
337 'truncated': truncated,
338 'opening_post_id': opening_post_id,
333 PARAMETER_POST: self,
334 PARAMETER_MODERATOR: moderator,
335 PARAMETER_IS_OPENING: is_opening,
336 PARAMETER_THREAD: thread,
337 PARAMETER_BUMPABLE: can_bump,
338 PARAMETER_NEED_OPEN_LINK: need_open_link,
339 PARAMETER_TRUNCATED: truncated,
340 PARAMETER_OP_ID: opening_post_id,
339 341 })
340 342
341 343 def get_first_image(self) -> PostImage:
@@ -369,14 +371,12 class Post(models.Model, Viewable):
369 371 """
370 372
371 373 if format_type == DIFF_TYPE_HTML:
372 context = RequestContext(request)
373 context['post'] = self
374 params = dict()
375 params['post'] = self
374 376 if PARAMETER_TRUNCATED in request.GET:
375 context[PARAMETER_TRUNCATED] = True
377 params[PARAMETER_TRUNCATED] = True
376 378
377 # TODO Use dict here
378 return render_to_string('boards/api_post.html',
379 context_instance=context)
379 return render_to_string('boards/api_post.html', params)
380 380 elif format_type == DIFF_TYPE_JSON:
381 381 post_json = {
382 382 'id': self.id,
@@ -34,7 +34,7
34 34 {% with can_bump=thread.can_bump %}
35 35 {% for post in thread.get_replies %}
36 36 {% with is_opening=forloop.first %}
37 {% post_view post moderator=moderator is_opening=is_opening thread=thread can_bump=can_bump opening_post_id=opening_post.id %}
37 {% post_view post moderator=moderator is_opening=is_opening thread=thread bumpable=can_bump opening_post_id=opening_post.id %}
38 38 {% endwith %}
39 39 {% endfor %}
40 40 {% endwith %}
@@ -112,14 +112,10 class AllThreadsView(PostMixin, BaseBoar
112 112
113 113 title = data[FORM_TITLE]
114 114 text = data[FORM_TEXT]
115 image = data.get(FORM_IMAGE)
115 116
116 117 text = self._remove_invalid_links(text)
117 118
118 if FORM_IMAGE in list(data.keys()):
119 image = data[FORM_IMAGE]
120 else:
121 image = None
122
123 119 tag_strings = data[FORM_TAGS]
124 120
125 121 tags = self.parse_tags_string(tag_strings)
@@ -55,9 +55,7 def api_get_threaddiff(request, thread_i
55 55 pub_time__lte=filter_time,
56 56 last_edit_time__gt=filter_time)
57 57
58 diff_type = DIFF_TYPE_HTML
59 if PARAMETER_DIFF_TYPE in request.GET:
60 diff_type = request.GET[PARAMETER_DIFF_TYPE]
58 diff_type = request.GET.get(PARAMETER_DIFF_TYPE, DIFF_TYPE_HTML)
61 59
62 60 for post in added_posts:
63 61 json_data['added'].append(get_post_data(post.id, diff_type, request))
@@ -1,10 +1,14
1 1 from django.shortcuts import render
2 from django.template import RequestContext
3 2 from django.views.generic import View
4 3 from haystack.query import SearchQuerySet
4
5 5 from boards.abstracts.paginator import get_paginator
6 6 from boards.forms import SearchForm, PlainErrorList
7 7
8
9 MIN_QUERY_LENGTH = 3
10 RESULTS_PER_PAGE = 10
11
8 12 FORM_QUERY = 'query'
9 13
10 14 CONTEXT_QUERY = 'query'
@@ -20,22 +24,20 TEMPLATE = 'search/search.html'
20 24
21 25 class BoardSearchView(View):
22 26 def get(self, request):
23 context = RequestContext(request)
27 params = dict()
28
24 29 form = SearchForm(request.GET, error_class=PlainErrorList)
25 context[CONTEXT_FORM] = form
30 params[CONTEXT_FORM] = form
26 31
27 32 if form.is_valid():
28 33 query = form.cleaned_data[FORM_QUERY]
29 if len(query) >= 3:
34 if len(query) >= MIN_QUERY_LENGTH:
30 35 results = SearchQuerySet().auto_query(query).order_by('-id')
31 paginator = get_paginator(results, 10)
36 paginator = get_paginator(results, RESULTS_PER_PAGE)
32 37
33 if REQUEST_PAGE in request.GET:
34 page = int(request.GET[REQUEST_PAGE])
35 else:
36 page = 1
37 context[CONTEXT_PAGE] = paginator.page(page)
38 context[CONTEXT_QUERY] = query
38 page = int(request.GET.get(REQUEST_PAGE, '1'))
39 39
40 # TODO Use dict here
41 return render(request, TEMPLATE, context_instance=context)
40 params[CONTEXT_PAGE] = paginator.page(page)
41 params[CONTEXT_QUERY] = query
42
43 return render(request, TEMPLATE, params)
General Comments 0
You need to be logged in to leave comments. Login now