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