##// END OF EJS Templates
Refactored views
neko259 -
r1090:a66d091f default
parent child Browse files
Show More
@@ -6,6 +6,8 b' from django.utils import timezone'
6 6 from boards import utils
7 7 from boards.models import Ban
8 8
9 SESSION_TIMEZONE = 'django_timezone'
10
9 11 RESPONSE_CONTENT_TYPE = 'Content-Type'
10 12
11 13 TYPE_HTML = 'text/html'
@@ -34,7 +36,7 b' class BanMiddleware:'
34 36
35 37 class TimezoneMiddleware(object):
36 38 def process_request(self, request):
37 tzname = request.session.get('django_timezone')
39 tzname = request.session.get(SESSION_TIMEZONE)
38 40 if tzname:
39 41 timezone.activate(pytz.timezone(tzname))
40 42 else:
@@ -1,16 +1,16 b''
1 from django.conf.urls import patterns, url, include
2 from django.contrib import admin
1 from django.conf.urls import patterns, url
2
3 3 from boards import views
4 4 from boards.rss import AllThreadsFeed, TagThreadsFeed, ThreadPostsFeed
5 5 from boards.views import api, tag_threads, all_threads, \
6 6 settings, all_tags
7 7 from boards.views.authors import AuthorsView
8 from boards.views.ban import BanUserView
9 8 from boards.views.notifications import NotificationView
10 9 from boards.views.search import BoardSearchView
11 10 from boards.views.static import StaticPageView
12 11 from boards.views.preview import PostPreviewView
13 12
13
14 14 js_info_dict = {
15 15 'packages': ('boards',),
16 16 }
@@ -38,7 +38,6 b" urlpatterns = patterns('',"
38 38 url(r'^settings/$', settings.SettingsView.as_view(), name='settings'),
39 39 url(r'^tags/(?P<query>\w+)?/?$', all_tags.AllTagsView.as_view(), name='tags'),
40 40 url(r'^authors/$', AuthorsView.as_view(), name='authors'),
41 url(r'^ban/(?P<post_id>\w+)/$', BanUserView.as_view(), name='ban'),
42 41
43 42 url(r'^banned/$', views.banned.BannedView.as_view(), name='banned'),
44 43 url(r'^staticpage/(?P<name>\w+)/$', StaticPageView.as_view(),
@@ -1,25 +1,6 b''
1 1 PARAM_NEXT = 'next'
2 2 PARAMETER_METHOD = 'method'
3 3
4 from django.shortcuts import redirect
5 from django.http import HttpResponseRedirect
6
7
8 class RedirectNextMixin:
9
10 def redirect_to_next(self, request):
11 """
12 If a 'next' parameter was specified, redirect to the next page. This
13 is used when the user is required to return to some page after the
14 current view has finished its work.
15 """
16
17 if PARAM_NEXT in request.GET:
18 next_page = request.GET[PARAM_NEXT]
19 return HttpResponseRedirect(next_page)
20 else:
21 return redirect('index')
22
23 4
24 5 class DispatcherMixin:
25 6 """
@@ -1,11 +1,13 b''
1 1 from django.shortcuts import render
2
2 3 from boards.abstracts.paginator import get_paginator
3 4 from boards.abstracts.settingsmanager import get_settings_manager, \
4 5 SETTING_USERNAME, SETTING_LAST_NOTIFICATION_ID
5 from boards.models import Post
6 6 from boards.models.user import Notification
7 7 from boards.views.base import BaseBoardView
8 8
9 DEFAULT_PAGE = '1'
10
9 11 TEMPLATE = 'boards/notifications.html'
10 12 PARAM_PAGE = 'page'
11 13 PARAM_USERNAME = 'notification_username'
@@ -26,7 +28,7 b' class NotificationView(BaseBoardView):'
26 28 notification_username = username.lower()
27 29
28 30 posts = Notification.objects.get_notification_posts(
29 username=notification_username)
31 username=notification_username)
30 32 if notification_username == my_username:
31 33 last = posts.first()
32 34 if last is not None:
@@ -36,7 +38,7 b' class NotificationView(BaseBoardView):'
36 38
37 39 paginator = get_paginator(posts, RESULTS_PER_PAGE)
38 40
39 page = int(request.GET.get(REQUEST_PAGE, '1'))
41 page = int(request.GET.get(REQUEST_PAGE, DEFAULT_PAGE))
40 42
41 43 params[PARAM_PAGE] = paginator.page(page)
42 44 params[PARAM_USERNAME] = notification_username
@@ -23,7 +23,7 b' class PostPreviewView(View):'
23 23 return render(request, TEMPLATE, context_instance=context)
24 24
25 25 def post(self, request):
26 context = RequestContext(request)
26 params = dict()
27 27
28 28 if FORM_QUERY in request.POST:
29 29 raw_text = request.POST[FORM_QUERY]
@@ -32,8 +32,7 b' class PostPreviewView(View):'
32 32 parser = Parser()
33 33 rendered_text = parser.parse(parser.preparse(raw_text))
34 34
35 context[CONTEXT_RESULT] = rendered_text
36 context[CONTEXT_QUERY] = raw_text
35 params[CONTEXT_RESULT] = rendered_text
36 params[CONTEXT_QUERY] = raw_text
37 37
38 # TODO Use dict here
39 return render(request, TEMPLATE, context_instance=context)
38 return render(request, TEMPLATE, params)
@@ -1,14 +1,14 b''
1 import pytz
2
3 1 from django.db import transaction
4 2 from django.shortcuts import render, redirect
5 3 from django.utils import timezone
6 4
7 5 from boards.abstracts.settingsmanager import get_settings_manager, \
8 6 SETTING_USERNAME, SETTING_LAST_NOTIFICATION_ID
7 from boards.middlewares import SESSION_TIMEZONE
9 8 from boards.views.base import BaseBoardView, CONTEXT_FORM
10 9 from boards.forms import SettingsForm, PlainErrorList
11 10
11
12 12 FORM_THEME = 'theme'
13 13 FORM_USERNAME = 'username'
14 14 FORM_TIMEZONE = 'timezone'
@@ -30,7 +30,8 b' class SettingsView(BaseBoardView):'
30 30 initial={
31 31 FORM_THEME: selected_theme,
32 32 FORM_USERNAME: settings_manager.get_setting(SETTING_USERNAME),
33 FORM_TIMEZONE: request.session.get('django_timezone', timezone.get_current_timezone()),
33 FORM_TIMEZONE: request.session.get(
34 SESSION_TIMEZONE, timezone.get_current_timezone()),
34 35 },
35 36 error_class=PlainErrorList)
36 37
@@ -56,7 +57,7 b' class SettingsView(BaseBoardView):'
56 57 settings_manager.set_setting(SETTING_USERNAME, username)
57 58 settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID, None)
58 59
59 request.session['django_timezone'] = form.cleaned_data[FORM_TIMEZONE]
60 request.session[SESSION_TIMEZONE] = form.cleaned_data[FORM_TIMEZONE]
60 61
61 62 return redirect('settings')
62 63 else:
@@ -4,7 +4,7 b' from boards.abstracts.settingsmanager im'
4 4 SETTING_FAVORITE_TAGS, SETTING_HIDDEN_TAGS
5 5 from boards.models import Tag
6 6 from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE
7 from boards.views.mixins import DispatcherMixin, RedirectNextMixin
7 from boards.views.mixins import DispatcherMixin
8 8 from boards.forms import ThreadForm, PlainErrorList
9 9
10 10 PARAM_HIDDEN_TAGS = 'hidden_tags'
@@ -15,7 +15,7 b" PARAM_IS_HIDDEN = 'is_hidden'"
15 15 __author__ = 'neko259'
16 16
17 17
18 class TagView(AllThreadsView, DispatcherMixin, RedirectNextMixin):
18 class TagView(AllThreadsView, DispatcherMixin):
19 19
20 20 tag_name = None
21 21
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now