##// END OF EJS Templates
Fixed toggling "only favorite tags" setting
neko259 -
r2053:5a2e843c default
parent child Browse files
Show More
@@ -1,132 +1,132 b''
1 from django.core.paginator import EmptyPage
1 from django.core.paginator import EmptyPage
2 from django.http import Http404
2 from django.http import Http404
3 from django.shortcuts import render
3 from django.shortcuts import render, redirect
4 from django.urls import reverse
4 from django.urls import reverse
5 from django.utils.decorators import method_decorator
5 from django.utils.decorators import method_decorator
6 from django.views.decorators.csrf import csrf_protect
6 from django.views.decorators.csrf import csrf_protect
7
7
8 from boards import settings
8 from boards import settings
9 from boards.abstracts.constants import PARAM_PAGE
9 from boards.abstracts.constants import PARAM_PAGE
10 from boards.abstracts.paginator import get_paginator
10 from boards.abstracts.paginator import get_paginator
11 from boards.abstracts.settingsmanager import get_settings_manager, \
11 from boards.abstracts.settingsmanager import get_settings_manager, \
12 SETTING_ONLY_FAVORITES
12 SETTING_ONLY_FAVORITES
13 from boards.forms import ThreadForm, PlainErrorList
13 from boards.forms import ThreadForm, PlainErrorList
14 from boards.models import Post, Thread
14 from boards.models import Post, Thread
15 from boards.settings import SECTION_VIEW
15 from boards.settings import SECTION_VIEW
16 from boards.views.base import BaseBoardView, CONTEXT_FORM
16 from boards.views.base import BaseBoardView, CONTEXT_FORM
17 from boards.views.mixins import PaginatedMixin, \
17 from boards.views.mixins import PaginatedMixin, \
18 DispatcherMixin, PARAMETER_METHOD
18 DispatcherMixin, PARAMETER_METHOD
19
19
20 ORDER_BUMP = 'bump'
20 ORDER_BUMP = 'bump'
21
21
22 PARAM_ORDER = 'order'
22 PARAM_ORDER = 'order'
23
23
24 FORM_TAGS = 'tags'
24 FORM_TAGS = 'tags'
25 FORM_TEXT = 'text'
25 FORM_TEXT = 'text'
26 FORM_TITLE = 'title'
26 FORM_TITLE = 'title'
27 FORM_IMAGE = 'image'
27 FORM_IMAGE = 'image'
28 FORM_THREADS = 'threads'
28 FORM_THREADS = 'threads'
29
29
30 TAG_DELIMITER = ' '
30 TAG_DELIMITER = ' '
31
31
32 PARAMETER_CURRENT_PAGE = 'current_page'
32 PARAMETER_CURRENT_PAGE = 'current_page'
33 PARAMETER_PAGINATOR = 'paginator'
33 PARAMETER_PAGINATOR = 'paginator'
34 PARAMETER_THREADS = 'threads'
34 PARAMETER_THREADS = 'threads'
35 PARAMETER_ADDITIONAL = 'additional_params'
35 PARAMETER_ADDITIONAL = 'additional_params'
36 PARAMETER_RSS_URL = 'rss_url'
36 PARAMETER_RSS_URL = 'rss_url'
37
37
38 TEMPLATE = 'boards/all_threads.html'
38 TEMPLATE = 'boards/all_threads.html'
39 DEFAULT_PAGE = 1
39 DEFAULT_PAGE = 1
40
40
41
41
42 class AllThreadsView(BaseBoardView, PaginatedMixin, DispatcherMixin):
42 class AllThreadsView(BaseBoardView, PaginatedMixin, DispatcherMixin):
43
43
44 tag_name = ''
44 tag_name = ''
45
45
46 def __init__(self):
46 def __init__(self):
47 self.settings_manager = None
47 self.settings_manager = None
48 super(AllThreadsView, self).__init__()
48 super(AllThreadsView, self).__init__()
49
49
50 @method_decorator(csrf_protect)
50 @method_decorator(csrf_protect)
51 def get(self, request, form: ThreadForm=None):
51 def get(self, request, form: ThreadForm=None):
52 page = request.GET.get(PARAM_PAGE, DEFAULT_PAGE)
52 page = request.GET.get(PARAM_PAGE, DEFAULT_PAGE)
53
53
54 params = self.get_context_data(request=request)
54 params = self.get_context_data(request=request)
55
55
56 if not form:
56 if not form:
57 form = ThreadForm(error_class=PlainErrorList,
57 form = ThreadForm(error_class=PlainErrorList,
58 initial={FORM_TAGS: self.tag_name})
58 initial={FORM_TAGS: self.tag_name})
59
59
60 self.settings_manager = get_settings_manager(request)
60 self.settings_manager = get_settings_manager(request)
61
61
62 threads = self.get_threads()
62 threads = self.get_threads()
63
63
64 order = request.GET.get(PARAM_ORDER, ORDER_BUMP)
64 order = request.GET.get(PARAM_ORDER, ORDER_BUMP)
65 if order == ORDER_BUMP:
65 if order == ORDER_BUMP:
66 threads = threads.order_by('-bump_time')
66 threads = threads.order_by('-bump_time')
67 else:
67 else:
68 threads = threads.filter(replies__opening=True)\
68 threads = threads.filter(replies__opening=True)\
69 .order_by('-replies__pub_time')
69 .order_by('-replies__pub_time')
70 threads = threads.distinct()
70 threads = threads.distinct()
71
71
72 paginator = get_paginator(threads, settings.get_int(
72 paginator = get_paginator(threads, settings.get_int(
73 SECTION_VIEW, 'ThreadsPerPage'))
73 SECTION_VIEW, 'ThreadsPerPage'))
74 paginator.current_page = int(page)
74 paginator.current_page = int(page)
75
75
76 try:
76 try:
77 threads = paginator.page(page).object_list
77 threads = paginator.page(page).object_list
78 except EmptyPage:
78 except EmptyPage:
79 raise Http404()
79 raise Http404()
80
80
81 params[PARAMETER_THREADS] = threads
81 params[PARAMETER_THREADS] = threads
82 params[CONTEXT_FORM] = form
82 params[CONTEXT_FORM] = form
83 params[PARAMETER_RSS_URL] = self.get_rss_url()
83 params[PARAMETER_RSS_URL] = self.get_rss_url()
84
84
85 paginator.set_url(self.get_reverse_url(), request.GET.dict())
85 paginator.set_url(self.get_reverse_url(), request.GET.dict())
86 params.update(self.get_page_context(paginator, page))
86 params.update(self.get_page_context(paginator, page))
87
87
88 return render(request, TEMPLATE, params)
88 return render(request, TEMPLATE, params)
89
89
90 @method_decorator(csrf_protect)
90 @method_decorator(csrf_protect)
91 def post(self, request):
91 def post(self, request):
92 if PARAMETER_METHOD in request.POST:
92 if PARAMETER_METHOD in request.POST:
93 self.dispatch_method(request)
93 self.dispatch_method(request)
94
94
95 return self.get_reverse_url()
95 return redirect(self.get_reverse_url())
96
96
97 form = ThreadForm(request.POST, request.FILES,
97 form = ThreadForm(request.POST, request.FILES,
98 error_class=PlainErrorList)
98 error_class=PlainErrorList)
99 form.session = request.session
99 form.session = request.session
100
100
101 if form.is_valid():
101 if form.is_valid():
102 return Post.objects.create_from_form(request, form, None)
102 return Post.objects.create_from_form(request, form, None)
103 if form.need_to_ban:
103 if form.need_to_ban:
104 # Ban user because he is suspected to be a bot
104 # Ban user because he is suspected to be a bot
105 self._ban_current_user(request)
105 self._ban_current_user(request)
106
106
107 return self.get(request, form)
107 return self.get(request, form)
108
108
109 def get_reverse_url(self):
109 def get_reverse_url(self):
110 return reverse('index')
110 return reverse('index')
111
111
112 def get_threads(self):
112 def get_threads(self):
113 """
113 """
114 Gets list of threads that will be shown on a page.
114 Gets list of threads that will be shown on a page.
115 """
115 """
116
116
117 threads = Thread.objects\
117 threads = Thread.objects\
118 .exclude(tags__in=self.settings_manager.get_hidden_tags())
118 .exclude(tags__in=self.settings_manager.get_hidden_tags())
119 if self.settings_manager.get_setting(SETTING_ONLY_FAVORITES):
119 if self.settings_manager.get_setting(SETTING_ONLY_FAVORITES):
120 fav_tags = self.settings_manager.get_fav_tags()
120 fav_tags = self.settings_manager.get_fav_tags()
121 if len(fav_tags) > 0:
121 if len(fav_tags) > 0:
122 threads = threads.filter(tags__in=fav_tags)
122 threads = threads.filter(tags__in=fav_tags)
123
123
124 return threads
124 return threads
125
125
126 def get_rss_url(self):
126 def get_rss_url(self):
127 return self.get_reverse_url() + 'rss/'
127 return self.get_reverse_url() + 'rss/'
128
128
129 def toggle_fav(self, request):
129 def toggle_fav(self, request):
130 settings_manager = get_settings_manager(request)
130 settings_manager = get_settings_manager(request)
131 settings_manager.set_setting(SETTING_ONLY_FAVORITES,
131 settings_manager.set_setting(SETTING_ONLY_FAVORITES,
132 not settings_manager.get_setting(SETTING_ONLY_FAVORITES, False))
132 not settings_manager.get_setting(SETTING_ONLY_FAVORITES, False))
General Comments 0
You need to be logged in to leave comments. Login now