# HG changeset patch # User neko259 # Date 2015-04-13 10:41:57 # Node ID a66d091f1cf70ef3925550c771fb05d944cf3274 # Parent e299d6dbd5d82954c3299874d50314aa7c78e1d7 Refactored views diff --git a/boards/middlewares.py b/boards/middlewares.py --- a/boards/middlewares.py +++ b/boards/middlewares.py @@ -6,6 +6,8 @@ from django.utils import timezone from boards import utils from boards.models import Ban +SESSION_TIMEZONE = 'django_timezone' + RESPONSE_CONTENT_TYPE = 'Content-Type' TYPE_HTML = 'text/html' @@ -34,7 +36,7 @@ class BanMiddleware: class TimezoneMiddleware(object): def process_request(self, request): - tzname = request.session.get('django_timezone') + tzname = request.session.get(SESSION_TIMEZONE) if tzname: timezone.activate(pytz.timezone(tzname)) else: diff --git a/boards/profiler.py b/boards/profiler.py deleted file mode 100644 --- a/boards/profiler.py +++ /dev/null @@ -1,26 +0,0 @@ -import sys -from cStringIO import StringIO -from django.conf import settings -import line_profiler - - -class ProfilerMiddleware(): - def __init__(self): - self.profiler = None - - def process_view(self, request, callback, callback_args, callback_kwargs): - if settings.DEBUG and 'prof' in request.GET: - self.profiler = line_profiler.LineProfiler() - self.profiler.add_function(callback) - self.profiler.enable() - args = (request,) + callback_args - return callback(*args, **callback_kwargs) - - def process_response(self, request, response): - if settings.DEBUG and 'prof' in request.GET: - out = StringIO() - old_stdout, sys.stdout = sys.stdout, out - self.profiler.print_stats() - sys.stdout = old_stdout - response.content = '
%s
' % out.getvalue() - return response diff --git a/boards/urls.py b/boards/urls.py --- a/boards/urls.py +++ b/boards/urls.py @@ -1,16 +1,16 @@ -from django.conf.urls import patterns, url, include -from django.contrib import admin +from django.conf.urls import patterns, url + from boards import views from boards.rss import AllThreadsFeed, TagThreadsFeed, ThreadPostsFeed from boards.views import api, tag_threads, all_threads, \ settings, all_tags from boards.views.authors import AuthorsView -from boards.views.ban import BanUserView from boards.views.notifications import NotificationView from boards.views.search import BoardSearchView from boards.views.static import StaticPageView from boards.views.preview import PostPreviewView + js_info_dict = { 'packages': ('boards',), } @@ -38,7 +38,6 @@ urlpatterns = patterns('', url(r'^settings/$', settings.SettingsView.as_view(), name='settings'), url(r'^tags/(?P\w+)?/?$', all_tags.AllTagsView.as_view(), name='tags'), url(r'^authors/$', AuthorsView.as_view(), name='authors'), - url(r'^ban/(?P\w+)/$', BanUserView.as_view(), name='ban'), url(r'^banned/$', views.banned.BannedView.as_view(), name='banned'), url(r'^staticpage/(?P\w+)/$', StaticPageView.as_view(), diff --git a/boards/views/ban.py b/boards/views/ban.py deleted file mode 100644 --- a/boards/views/ban.py +++ /dev/null @@ -1,26 +0,0 @@ -from django.db import transaction -from django.shortcuts import get_object_or_404 - -from boards.abstracts.settingsmanager import PERMISSION_MODERATE, \ - get_settings_manager -from boards.views.base import BaseBoardView -from boards.models import Post, Ban -from boards.views.mixins import RedirectNextMixin - - -class BanUserView(BaseBoardView, RedirectNextMixin): - - @transaction.atomic - def get(self, request, post_id): - post = get_object_or_404(Post, id=post_id) - - settings_manager = get_settings_manager(request) - - if settings_manager.has_permission(PERMISSION_MODERATE): - # TODO Show confirmation page before ban - ban, created = Ban.objects.get_or_create(ip=post.poster_ip) - if created: - ban.reason = 'Banned for post ' + str(post_id) - ban.save() - - return self.redirect_to_next(request) diff --git a/boards/views/mixins.py b/boards/views/mixins.py --- a/boards/views/mixins.py +++ b/boards/views/mixins.py @@ -1,25 +1,6 @@ PARAM_NEXT = 'next' PARAMETER_METHOD = 'method' -from django.shortcuts import redirect -from django.http import HttpResponseRedirect - - -class RedirectNextMixin: - - def redirect_to_next(self, request): - """ - If a 'next' parameter was specified, redirect to the next page. This - is used when the user is required to return to some page after the - current view has finished its work. - """ - - if PARAM_NEXT in request.GET: - next_page = request.GET[PARAM_NEXT] - return HttpResponseRedirect(next_page) - else: - return redirect('index') - class DispatcherMixin: """ diff --git a/boards/views/notifications.py b/boards/views/notifications.py --- a/boards/views/notifications.py +++ b/boards/views/notifications.py @@ -1,11 +1,13 @@ from django.shortcuts import render + from boards.abstracts.paginator import get_paginator from boards.abstracts.settingsmanager import get_settings_manager, \ SETTING_USERNAME, SETTING_LAST_NOTIFICATION_ID -from boards.models import Post from boards.models.user import Notification from boards.views.base import BaseBoardView +DEFAULT_PAGE = '1' + TEMPLATE = 'boards/notifications.html' PARAM_PAGE = 'page' PARAM_USERNAME = 'notification_username' @@ -26,7 +28,7 @@ class NotificationView(BaseBoardView): notification_username = username.lower() posts = Notification.objects.get_notification_posts( - username=notification_username) + username=notification_username) if notification_username == my_username: last = posts.first() if last is not None: @@ -36,7 +38,7 @@ class NotificationView(BaseBoardView): paginator = get_paginator(posts, RESULTS_PER_PAGE) - page = int(request.GET.get(REQUEST_PAGE, '1')) + page = int(request.GET.get(REQUEST_PAGE, DEFAULT_PAGE)) params[PARAM_PAGE] = paginator.page(page) params[PARAM_USERNAME] = notification_username diff --git a/boards/views/preview.py b/boards/views/preview.py --- a/boards/views/preview.py +++ b/boards/views/preview.py @@ -23,7 +23,7 @@ class PostPreviewView(View): return render(request, TEMPLATE, context_instance=context) def post(self, request): - context = RequestContext(request) + params = dict() if FORM_QUERY in request.POST: raw_text = request.POST[FORM_QUERY] @@ -32,8 +32,7 @@ class PostPreviewView(View): parser = Parser() rendered_text = parser.parse(parser.preparse(raw_text)) - context[CONTEXT_RESULT] = rendered_text - context[CONTEXT_QUERY] = raw_text + params[CONTEXT_RESULT] = rendered_text + params[CONTEXT_QUERY] = raw_text - # TODO Use dict here - return render(request, TEMPLATE, context_instance=context) + return render(request, TEMPLATE, params) diff --git a/boards/views/settings.py b/boards/views/settings.py --- a/boards/views/settings.py +++ b/boards/views/settings.py @@ -1,14 +1,14 @@ -import pytz - from django.db import transaction from django.shortcuts import render, redirect from django.utils import timezone from boards.abstracts.settingsmanager import get_settings_manager, \ SETTING_USERNAME, SETTING_LAST_NOTIFICATION_ID +from boards.middlewares import SESSION_TIMEZONE from boards.views.base import BaseBoardView, CONTEXT_FORM from boards.forms import SettingsForm, PlainErrorList + FORM_THEME = 'theme' FORM_USERNAME = 'username' FORM_TIMEZONE = 'timezone' @@ -30,7 +30,8 @@ class SettingsView(BaseBoardView): initial={ FORM_THEME: selected_theme, FORM_USERNAME: settings_manager.get_setting(SETTING_USERNAME), - FORM_TIMEZONE: request.session.get('django_timezone', timezone.get_current_timezone()), + FORM_TIMEZONE: request.session.get( + SESSION_TIMEZONE, timezone.get_current_timezone()), }, error_class=PlainErrorList) @@ -56,7 +57,7 @@ class SettingsView(BaseBoardView): settings_manager.set_setting(SETTING_USERNAME, username) settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID, None) - request.session['django_timezone'] = form.cleaned_data[FORM_TIMEZONE] + request.session[SESSION_TIMEZONE] = form.cleaned_data[FORM_TIMEZONE] return redirect('settings') else: diff --git a/boards/views/tag_threads.py b/boards/views/tag_threads.py --- a/boards/views/tag_threads.py +++ b/boards/views/tag_threads.py @@ -4,7 +4,7 @@ from boards.abstracts.settingsmanager im SETTING_FAVORITE_TAGS, SETTING_HIDDEN_TAGS from boards.models import Tag from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE -from boards.views.mixins import DispatcherMixin, RedirectNextMixin +from boards.views.mixins import DispatcherMixin from boards.forms import ThreadForm, PlainErrorList PARAM_HIDDEN_TAGS = 'hidden_tags' @@ -15,7 +15,7 @@ PARAM_IS_HIDDEN = 'is_hidden' __author__ = 'neko259' -class TagView(AllThreadsView, DispatcherMixin, RedirectNextMixin): +class TagView(AllThreadsView, DispatcherMixin): tag_name = None