diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -8,7 +8,6 @@ from django.core.cache import cache from django.core.urlresolvers import reverse from django.db import models, transaction from django.db.models import TextField -from django.template import RequestContext from django.template.loader import render_to_string from django.utils import timezone @@ -19,6 +18,7 @@ from boards.models.base import Viewable from boards.models.thread import Thread from boards.utils import datetime_to_epoch + WS_NOTIFICATION_TYPE_NEW_POST = 'new_post' WS_NOTIFICATION_TYPE = 'notification_type' @@ -49,6 +49,13 @@ PARAMETER_TRUNCATED = 'truncated' PARAMETER_TAG = 'tag' PARAMETER_OFFSET = 'offset' PARAMETER_DIFF_TYPE = 'type' +PARAMETER_BUMPABLE = 'bumpable' +PARAMETER_THREAD = 'thread' +PARAMETER_IS_OPENING = 'is_opening' +PARAMETER_MODERATOR = 'moderator' +PARAMETER_POST = 'post' +PARAMETER_OP_ID = 'opening_post_id' +PARAMETER_NEED_OPEN_LINK = 'need_open_link' DIFF_TYPE_HTML = 'html' DIFF_TYPE_JSON = 'json' @@ -307,20 +314,15 @@ class Post(models.Model, Viewable): def get_view(self, moderator=False, need_open_link=False, truncated=False, *args, **kwargs): - if 'is_opening' in kwargs: - is_opening = kwargs['is_opening'] - else: - is_opening = self.is_opening() + """ + Renders post's HTML view. Some of the post params can be passed over + kwargs for the means of caching (if we view the thread, some params + are same for every post and don't need to be computed over and over. + """ - if 'thread' in kwargs: - thread = kwargs['thread'] - else: - thread = self.get_thread() - - if 'can_bump' in kwargs: - can_bump = kwargs['can_bump'] - else: - can_bump = thread.can_bump() + is_opening = kwargs.get(PARAMETER_IS_OPENING, self.is_opening()) + thread = kwargs.get(PARAMETER_THREAD, self.get_thread()) + can_bump = kwargs.get(PARAMETER_BUMPABLE, thread.can_bump()) if is_opening: opening_post_id = self.id @@ -328,14 +330,14 @@ class Post(models.Model, Viewable): opening_post_id = thread.get_opening_post_id() return render_to_string('boards/post.html', { - 'post': self, - 'moderator': moderator, - 'is_opening': is_opening, - 'thread': thread, - 'bumpable': can_bump, - 'need_open_link': need_open_link, - 'truncated': truncated, - 'opening_post_id': opening_post_id, + PARAMETER_POST: self, + PARAMETER_MODERATOR: moderator, + PARAMETER_IS_OPENING: is_opening, + PARAMETER_THREAD: thread, + PARAMETER_BUMPABLE: can_bump, + PARAMETER_NEED_OPEN_LINK: need_open_link, + PARAMETER_TRUNCATED: truncated, + PARAMETER_OP_ID: opening_post_id, }) def get_first_image(self) -> PostImage: @@ -369,14 +371,12 @@ class Post(models.Model, Viewable): """ if format_type == DIFF_TYPE_HTML: - context = RequestContext(request) - context['post'] = self + params = dict() + params['post'] = self if PARAMETER_TRUNCATED in request.GET: - context[PARAMETER_TRUNCATED] = True + params[PARAMETER_TRUNCATED] = True - # TODO Use dict here - return render_to_string('boards/api_post.html', - context_instance=context) + return render_to_string('boards/api_post.html', params) elif format_type == DIFF_TYPE_JSON: post_json = { 'id': self.id, diff --git a/boards/templates/boards/thread.html b/boards/templates/boards/thread.html --- a/boards/templates/boards/thread.html +++ b/boards/templates/boards/thread.html @@ -34,7 +34,7 @@ {% with can_bump=thread.can_bump %} {% for post in thread.get_replies %} {% with is_opening=forloop.first %} - {% post_view post moderator=moderator is_opening=is_opening thread=thread can_bump=can_bump opening_post_id=opening_post.id %} + {% post_view post moderator=moderator is_opening=is_opening thread=thread bumpable=can_bump opening_post_id=opening_post.id %} {% endwith %} {% endfor %} {% endwith %} diff --git a/boards/views/all_threads.py b/boards/views/all_threads.py --- a/boards/views/all_threads.py +++ b/boards/views/all_threads.py @@ -112,14 +112,10 @@ class AllThreadsView(PostMixin, BaseBoar title = data[FORM_TITLE] text = data[FORM_TEXT] + image = data.get(FORM_IMAGE) text = self._remove_invalid_links(text) - if FORM_IMAGE in list(data.keys()): - image = data[FORM_IMAGE] - else: - image = None - tag_strings = data[FORM_TAGS] tags = self.parse_tags_string(tag_strings) diff --git a/boards/views/api.py b/boards/views/api.py --- a/boards/views/api.py +++ b/boards/views/api.py @@ -55,9 +55,7 @@ def api_get_threaddiff(request, thread_i pub_time__lte=filter_time, last_edit_time__gt=filter_time) - diff_type = DIFF_TYPE_HTML - if PARAMETER_DIFF_TYPE in request.GET: - diff_type = request.GET[PARAMETER_DIFF_TYPE] + diff_type = request.GET.get(PARAMETER_DIFF_TYPE, DIFF_TYPE_HTML) for post in added_posts: json_data['added'].append(get_post_data(post.id, diff_type, request)) diff --git a/boards/views/search.py b/boards/views/search.py --- a/boards/views/search.py +++ b/boards/views/search.py @@ -1,10 +1,14 @@ from django.shortcuts import render -from django.template import RequestContext from django.views.generic import View from haystack.query import SearchQuerySet + from boards.abstracts.paginator import get_paginator from boards.forms import SearchForm, PlainErrorList + +MIN_QUERY_LENGTH = 3 +RESULTS_PER_PAGE = 10 + FORM_QUERY = 'query' CONTEXT_QUERY = 'query' @@ -20,22 +24,20 @@ TEMPLATE = 'search/search.html' class BoardSearchView(View): def get(self, request): - context = RequestContext(request) + params = dict() + form = SearchForm(request.GET, error_class=PlainErrorList) - context[CONTEXT_FORM] = form + params[CONTEXT_FORM] = form if form.is_valid(): query = form.cleaned_data[FORM_QUERY] - if len(query) >= 3: + if len(query) >= MIN_QUERY_LENGTH: results = SearchQuerySet().auto_query(query).order_by('-id') - paginator = get_paginator(results, 10) + paginator = get_paginator(results, RESULTS_PER_PAGE) - if REQUEST_PAGE in request.GET: - page = int(request.GET[REQUEST_PAGE]) - else: - page = 1 - context[CONTEXT_PAGE] = paginator.page(page) - context[CONTEXT_QUERY] = query + page = int(request.GET.get(REQUEST_PAGE, '1')) - # TODO Use dict here - return render(request, TEMPLATE, context_instance=context) + params[CONTEXT_PAGE] = paginator.page(page) + params[CONTEXT_QUERY] = query + + return render(request, TEMPLATE, params)