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