##// END OF EJS Templates
Refactored views
neko259 -
r1090:a66d091f default
parent child Browse files
Show More
@@ -1,42 +1,44
1 1 import pytz
2 2
3 3 from django.shortcuts import redirect
4 4 from django.utils import timezone
5 5
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'
12 14
13 15
14 16 class BanMiddleware:
15 17 """
16 18 This is run before showing the thread. Banned users don't need to see
17 19 anything
18 20 """
19 21
20 22 def __init__(self):
21 23 pass
22 24
23 25 def process_view(self, request, view_func, view_args, view_kwargs):
24 26
25 27 if request.path != '/banned/':
26 28 ip = utils.get_client_ip(request)
27 29 bans = Ban.objects.filter(ip=ip)
28 30
29 31 if bans.exists():
30 32 ban = bans[0]
31 33 if not ban.can_read:
32 34 return redirect('banned')
33 35
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:
41 43 timezone.deactivate()
42 44
@@ -1,81 +1,80
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 }
17 17
18 18 urlpatterns = patterns('',
19 19 # /boards/
20 20 url(r'^$', all_threads.AllThreadsView.as_view(), name='index'),
21 21 # /boards/page/
22 22 url(r'^page/(?P<page>\w+)/$', all_threads.AllThreadsView.as_view(),
23 23 name='index'),
24 24
25 25 # /boards/tag/tag_name/
26 26 url(r'^tag/(?P<tag_name>\w+)/$', tag_threads.TagView.as_view(),
27 27 name='tag'),
28 28 # /boards/tag/tag_id/page/
29 29 url(r'^tag/(?P<tag_name>\w+)/page/(?P<page>\w+)/$',
30 30 tag_threads.TagView.as_view(), name='tag'),
31 31
32 32 # /boards/thread/
33 33 url(r'^thread/(?P<post_id>\d+)/$', views.thread.normal.NormalThreadView.as_view(),
34 34 name='thread'),
35 35 url(r'^thread/(?P<post_id>\d+)/mode/gallery/$', views.thread.gallery.GalleryThreadView.as_view(),
36 36 name='thread_gallery'),
37 37
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(),
45 44 name='staticpage'),
46 45
47 46 # RSS feeds
48 47 url(r'^rss/$', AllThreadsFeed()),
49 48 url(r'^page/(?P<page>\d+)/rss/$', AllThreadsFeed()),
50 49 url(r'^tag/(?P<tag_name>\w+)/rss/$', TagThreadsFeed()),
51 50 url(r'^tag/(?P<tag_name>\w+)/page/(?P<page>\w+)/rss/$', TagThreadsFeed()),
52 51 url(r'^thread/(?P<post_id>\d+)/rss/$', ThreadPostsFeed()),
53 52
54 53 # i18n
55 54 url(r'^jsi18n/$', 'boards.views.cached_js_catalog', js_info_dict,
56 55 name='js_info_dict'),
57 56
58 57 # API
59 58 url(r'^api/post/(?P<post_id>\d+)/$', api.get_post, name="get_post"),
60 59 url(r'^api/diff_thread$',
61 60 api.api_get_threaddiff, name="get_thread_diff"),
62 61 url(r'^api/threads/(?P<count>\w+)/$', api.api_get_threads,
63 62 name='get_threads'),
64 63 url(r'^api/tags/$', api.api_get_tags, name='get_tags'),
65 64 url(r'^api/thread/(?P<opening_post_id>\w+)/$', api.api_get_thread_posts,
66 65 name='get_thread'),
67 66 url(r'^api/add_post/(?P<opening_post_id>\w+)/$', api.api_add_post,
68 67 name='add_post'),
69 68 url(r'^api/notifications/(?P<username>\w+)/$', api.api_get_notifications,
70 69 name='api_notifications'),
71 70
72 71 # Search
73 72 url(r'^search/$', BoardSearchView.as_view(), name='search'),
74 73
75 74 # Notifications
76 75 url(r'^notifications/(?P<username>\w+)$', NotificationView.as_view(), name='notifications'),
77 76
78 77 # Post preview
79 78 url(r'^preview/$', PostPreviewView.as_view(), name='preview')
80 79
81 80 )
@@ -1,40 +1,21
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 """
26 7 This class contains a dispather method that can run a method specified by
27 8 'method' request parameter.
28 9 """
29 10
30 11 def dispatch_method(self, *args, **kwargs):
31 12 request = args[0]
32 13
33 14 method_name = None
34 15 if PARAMETER_METHOD in request.GET:
35 16 method_name = request.GET[PARAMETER_METHOD]
36 17 elif PARAMETER_METHOD in request.POST:
37 18 method_name = request.POST[PARAMETER_METHOD]
38 19
39 20 if method_name:
40 21 return getattr(self, method_name)(*args, **kwargs)
@@ -1,44 +1,46
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'
12 14 REQUEST_PAGE = 'page'
13 15 RESULTS_PER_PAGE = 10
14 16
15 17
16 18 class NotificationView(BaseBoardView):
17 19
18 20 def get(self, request, username):
19 21 params = self.get_context_data()
20 22
21 23 settings_manager = get_settings_manager(request)
22 24
23 25 # If we open our notifications, reset the "new" count
24 26 my_username = settings_manager.get_setting(SETTING_USERNAME)
25 27
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:
33 35 last_id = last.id
34 36 settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID,
35 37 last_id)
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
43 45
44 46 return render(request, TEMPLATE, params)
@@ -1,39 +1,38
1 1 from django.shortcuts import render
2 2 from django.template import RequestContext
3 3 from django.views.generic import View
4 4
5 5 from boards.mdx_neboard import Parser
6 6
7 7
8 8 FORM_QUERY = 'query'
9 9
10 10 CONTEXT_RESULT = 'result'
11 11 CONTEXT_QUERY = 'query'
12 12
13 13 __author__ = 'neko259'
14 14
15 15 TEMPLATE = 'boards/preview.html'
16 16
17 17
18 18 class PostPreviewView(View):
19 19 def get(self, request):
20 20 context = RequestContext(request)
21 21
22 22 # TODO Use dict here
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]
30 30
31 31 if len(raw_text) >= 0:
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,69 +1,70
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'
15 15
16 16 CONTEXT_HIDDEN_TAGS = 'hidden_tags'
17 17
18 18 TEMPLATE = 'boards/settings.html'
19 19
20 20
21 21 class SettingsView(BaseBoardView):
22 22
23 23 def get(self, request):
24 24 params = dict()
25 25 settings_manager = get_settings_manager(request)
26 26
27 27 selected_theme = settings_manager.get_theme()
28 28
29 29 form = SettingsForm(
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
37 38 params[CONTEXT_FORM] = form
38 39 params[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags()
39 40
40 41 return render(request, TEMPLATE, params)
41 42
42 43 def post(self, request):
43 44 settings_manager = get_settings_manager(request)
44 45
45 46 with transaction.atomic():
46 47 form = SettingsForm(request.POST, error_class=PlainErrorList)
47 48
48 49 if form.is_valid():
49 50 selected_theme = form.cleaned_data[FORM_THEME]
50 51 username = form.cleaned_data[FORM_USERNAME].lower()
51 52
52 53 settings_manager.set_theme(selected_theme)
53 54
54 55 old_username = settings_manager.get_setting(SETTING_USERNAME)
55 56 if username != old_username:
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:
63 64 params = dict()
64 65
65 66 params[CONTEXT_FORM] = form
66 67 params[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags()
67 68
68 69 return render(request, TEMPLATE, params)
69 70
@@ -1,107 +1,107
1 1 from django.shortcuts import get_object_or_404
2 2
3 3 from boards.abstracts.settingsmanager import get_settings_manager, \
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'
11 11 PARAM_TAG = 'tag'
12 12 PARAM_IS_FAVORITE = 'is_favorite'
13 13 PARAM_IS_HIDDEN = 'is_hidden'
14 14
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
22 22 def get_threads(self):
23 23 tag = get_object_or_404(Tag, name=self.tag_name)
24 24
25 25 hidden_tags = self.settings_manager.get_hidden_tags()
26 26
27 27 try:
28 28 hidden_tags.remove(tag)
29 29 except ValueError:
30 30 pass
31 31
32 32 return tag.get_threads().exclude(
33 33 tags__in=hidden_tags)
34 34
35 35 def get_context_data(self, **kwargs):
36 36 params = super(TagView, self).get_context_data(**kwargs)
37 37
38 38 settings_manager = get_settings_manager(kwargs['request'])
39 39
40 40 tag = get_object_or_404(Tag, name=self.tag_name)
41 41 params[PARAM_TAG] = tag
42 42
43 43 fav_tag_names = settings_manager.get_setting(SETTING_FAVORITE_TAGS)
44 44 hidden_tag_names = settings_manager.get_setting(SETTING_HIDDEN_TAGS)
45 45
46 46 params[PARAM_IS_FAVORITE] = fav_tag_names is not None and tag.name in fav_tag_names
47 47 params[PARAM_IS_HIDDEN] = hidden_tag_names is not None and tag.name in hidden_tag_names
48 48
49 49 return params
50 50
51 51 def get(self, request, tag_name, page=DEFAULT_PAGE, form=None):
52 52 self.tag_name = tag_name
53 53
54 54 return super(TagView, self).get(request, page, form)
55 55
56 56
57 57 def post(self, request, tag_name, page=DEFAULT_PAGE):
58 58 self.tag_name = tag_name
59 59
60 60 if 'method' in request.POST:
61 61 self.dispatch_method(request)
62 62 form = None
63 63 else:
64 64 form = ThreadForm(request.POST, request.FILES,
65 65 error_class=PlainErrorList)
66 66 form.session = request.session
67 67
68 68 if form.is_valid():
69 69 return self.create_thread(request, form)
70 70 if form.need_to_ban:
71 71 # Ban user because he is suspected to be a bot
72 72 self._ban_current_user(request)
73 73
74 74 return self.get(request, tag_name, page, form)
75 75
76 76 def subscribe(self, request):
77 77 tag = get_object_or_404(Tag, name=self.tag_name)
78 78
79 79 settings_manager = get_settings_manager(request)
80 80 settings_manager.add_fav_tag(tag)
81 81
82 82 def unsubscribe(self, request):
83 83 tag = get_object_or_404(Tag, name=self.tag_name)
84 84
85 85 settings_manager = get_settings_manager(request)
86 86 settings_manager.del_fav_tag(tag)
87 87
88 88 def hide(self, request):
89 89 """
90 90 Adds tag to user's hidden tags. Threads with this tag will not be
91 91 shown.
92 92 """
93 93
94 94 tag = get_object_or_404(Tag, name=self.tag_name)
95 95
96 96 settings_manager = get_settings_manager(request)
97 97 settings_manager.add_hidden_tag(tag)
98 98
99 99 def unhide(self, request):
100 100 """
101 101 Removed tag from user's hidden tags.
102 102 """
103 103
104 104 tag = get_object_or_404(Tag, name=self.tag_name)
105 105
106 106 settings_manager = get_settings_manager(request)
107 107 settings_manager.del_hidden_tag(tag)
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