##// END OF EJS Templates
Fixed tag threads view
neko259 -
r919:84a3726d default
parent child Browse files
Show More
@@ -1,137 +1,137 b''
1 1 from django.db import transaction
2 2 from django.shortcuts import render, redirect
3 3
4 4 from boards import utils, settings
5 5 from boards.abstracts.paginator import get_paginator
6 6 from boards.abstracts.settingsmanager import get_settings_manager
7 7 from boards.forms import ThreadForm, PlainErrorList
8 8 from boards.models import Post, Thread, Ban, Tag
9 9 from boards.views.banned import BannedView
10 10 from boards.views.base import BaseBoardView, CONTEXT_FORM
11 11 from boards.views.posting_mixin import PostMixin
12 12
13 13
14 14 FORM_TAGS = 'tags'
15 15 FORM_TEXT = 'text'
16 16 FORM_TITLE = 'title'
17 17 FORM_IMAGE = 'image'
18 18
19 19 TAG_DELIMITER = ' '
20 20
21 21 PARAMETER_CURRENT_PAGE = 'current_page'
22 22 PARAMETER_PAGINATOR = 'paginator'
23 23 PARAMETER_THREADS = 'threads'
24 24
25 25 TEMPLATE = 'boards/posting_general.html'
26 26 DEFAULT_PAGE = 1
27 27
28 28
29 29 class AllThreadsView(PostMixin, BaseBoardView):
30 30
31 31 def __init__(self):
32 32 self.settings_manager = None
33 33 super(AllThreadsView, self).__init__()
34 34
35 35 def get(self, request, page=DEFAULT_PAGE, form=None):
36 params = dict()
36 params = self.get_context_data(request=request)
37 37
38 38 if not form:
39 39 form = ThreadForm(error_class=PlainErrorList)
40 40
41 41 self.settings_manager = get_settings_manager(request)
42 42 paginator = get_paginator(self.get_threads(),
43 43 settings.THREADS_PER_PAGE)
44 44 paginator.current_page = int(page)
45 45
46 46 threads = paginator.page(page).object_list
47 47
48 48 params[PARAMETER_THREADS] = threads
49 49 params[CONTEXT_FORM] = form
50 50
51 51 self._get_page_context(paginator, params, page)
52 52
53 53 return render(request, TEMPLATE, params)
54 54
55 55 def post(self, request, page=DEFAULT_PAGE):
56 56 form = ThreadForm(request.POST, request.FILES,
57 57 error_class=PlainErrorList)
58 58 form.session = request.session
59 59
60 60 if form.is_valid():
61 61 return self.create_thread(request, form)
62 62 if form.need_to_ban:
63 63 # Ban user because he is suspected to be a bot
64 64 self._ban_current_user(request)
65 65
66 66 return self.get(request, page, form)
67 67
68 68 def _get_page_context(self, paginator, params, page):
69 69 """
70 70 Get pagination context variables
71 71 """
72 72
73 73 params[PARAMETER_PAGINATOR] = paginator
74 74 params[PARAMETER_CURRENT_PAGE] = paginator.page(int(page))
75 75
76 76 @staticmethod
77 77 def parse_tags_string(tag_strings):
78 78 """
79 79 Parses tag list string and returns tag object list.
80 80 """
81 81
82 82 tags = []
83 83
84 84 if tag_strings:
85 85 tag_strings = tag_strings.split(TAG_DELIMITER)
86 86 for tag_name in tag_strings:
87 87 tag_name = tag_name.strip().lower()
88 88 if len(tag_name) > 0:
89 89 tag, created = Tag.objects.get_or_create(name=tag_name)
90 90 tags.append(tag)
91 91
92 92 return tags
93 93
94 94 @transaction.atomic
95 95 def create_thread(self, request, form, html_response=True):
96 96 """
97 97 Creates a new thread with an opening post.
98 98 """
99 99
100 100 ip = utils.get_client_ip(request)
101 101 is_banned = Ban.objects.filter(ip=ip).exists()
102 102
103 103 if is_banned:
104 104 if html_response:
105 105 return redirect(BannedView().as_view())
106 106 else:
107 107 return
108 108
109 109 data = form.cleaned_data
110 110
111 111 title = data[FORM_TITLE]
112 112 text = data[FORM_TEXT]
113 113 image = data.get(FORM_IMAGE)
114 114
115 115 text = self._remove_invalid_links(text)
116 116
117 117 tag_strings = data[FORM_TAGS]
118 118
119 119 tags = self.parse_tags_string(tag_strings)
120 120
121 121 post = Post.objects.create_post(title=title, text=text, image=image,
122 122 ip=ip, tags=tags)
123 123
124 124 # This is required to update the threads to which posts we have replied
125 125 # when creating this one
126 126 post.send_to_websocket(request)
127 127
128 128 if html_response:
129 129 return redirect(post.get_url())
130 130
131 131 def get_threads(self):
132 132 """
133 133 Gets list of threads that will be shown on a page.
134 134 """
135 135
136 136 return Thread.objects.all().order_by('-bump_time')\
137 137 .exclude(tags__in=self.settings_manager.get_hidden_tags())
@@ -1,39 +1,31 b''
1 1 from django.db import transaction
2 2 from django.template import RequestContext
3 3 from django.views.generic import View
4 4
5 5 from boards import utils
6 6 from boards.models.user import Ban
7 7
8 8
9 9 BAN_REASON_SPAM = 'Autoban: spam bot'
10 10
11 11 CONTEXT_FORM = 'form'
12 12
13 13
14 14 class BaseBoardView(View):
15 15
16 16 def get_context_data(self, **kwargs):
17 """
18 This method is deprecated. You need to use dicts instead of context
19 instances in all places it is used now.
20 """
21
22 request = kwargs['request']
23 context = RequestContext(request)
24
25 return context
17 return dict()
26 18
27 19 @transaction.atomic
28 20 def _ban_current_user(self, request):
29 21 """
30 22 Add current user to the IP ban list
31 23 """
32 24
33 25 ip = utils.get_client_ip(request)
34 26 ban, created = Ban.objects.get_or_create(ip=ip)
35 27 if created:
36 28 ban.can_read = False
37 29 ban.reason = BAN_REASON_SPAM
38 30 ban.save()
39 31
@@ -1,18 +1,17 b''
1 1 from django.shortcuts import render
2 2
3 3 from boards.views.base import BaseBoardView
4 4
5 5
6 6 class NotFoundView(BaseBoardView):
7 7 """
8 8 Page 404 (not found)
9 9 """
10 10
11 11 def get(self, request):
12 context = self.get_context_data(request=request)
12 params = self.get_context_data()
13 13
14 # TODO Use dict here
15 response = render(request, 'boards/404.html', context_instance=context)
14 response = render(request, 'boards/404.html', params)
16 15 response.status_code = 404
17 16
18 17 return response
@@ -1,41 +1,41 b''
1 1 from django.db import transaction
2 2 from django.shortcuts import render, redirect
3 3
4 4 from boards.abstracts.settingsmanager import get_settings_manager
5 5 from boards.views.base import BaseBoardView, CONTEXT_FORM
6 6 from boards.forms import SettingsForm, PlainErrorList
7 7
8 8 FORM_THEME = 'theme'
9 9
10 10 CONTEXT_HIDDEN_TAGS = 'hidden_tags'
11 11
12 12
13 13 class SettingsView(BaseBoardView):
14 14
15 15 def get(self, request):
16 context = self.get_context_data(request=request)
16 params = self.get_context_data()
17 17 settings_manager = get_settings_manager(request)
18 18
19 19 selected_theme = settings_manager.get_theme()
20 20
21 21 form = SettingsForm(initial={FORM_THEME: selected_theme},
22 22 error_class=PlainErrorList)
23 23
24 context[CONTEXT_FORM] = form
25 context[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags()
24 params[CONTEXT_FORM] = form
25 params[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags()
26 26
27 27 # TODO Use dict here
28 return render(request, 'boards/settings.html', context_instance=context)
28 return render(request, 'boards/settings.html', params)
29 29
30 30 def post(self, request):
31 31 settings_manager = get_settings_manager(request)
32 32
33 33 with transaction.atomic():
34 34 form = SettingsForm(request.POST, error_class=PlainErrorList)
35 35
36 36 if form.is_valid():
37 37 selected_theme = form.cleaned_data[FORM_THEME]
38 38
39 39 settings_manager.set_theme(selected_theme)
40 40
41 41 return redirect('settings')
@@ -1,17 +1,13 b''
1 1 from django.shortcuts import render
2 2
3 3 from boards.views.base import BaseBoardView
4 4
5 5
6 6 class StaticPageView(BaseBoardView):
7 7
8 8 def get(self, request, name):
9 9 """
10 10 Show a static page that needs only tags list and a CSS
11 11 """
12 12
13 context = self.get_context_data(request=request)
14
15 # TODO Use dict here
16 return render(request, 'boards/staticpages/' + name + '.html',
17 context_instance=context)
13 return render(request, 'boards/staticpages/' + name + '.html')
@@ -1,95 +1,95 b''
1 1 from django.shortcuts import get_object_or_404
2 2
3 3 from boards.abstracts.settingsmanager import get_settings_manager
4 4 from boards.models import Tag, Thread
5 5 from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE
6 6 from boards.views.mixins import DispatcherMixin, RedirectNextMixin
7 7 from boards.forms import ThreadForm, PlainErrorList
8 8
9 9 PARAM_HIDDEN_TAGS = 'hidden_tags'
10 10 PARAM_FAV_TAGS = 'fav_tags'
11 11 PARAM_TAG = 'tag'
12 12
13 13 __author__ = 'neko259'
14 14
15 15
16 16 class TagView(AllThreadsView, DispatcherMixin, RedirectNextMixin):
17 17
18 18 tag_name = None
19 19
20 20 def get_threads(self):
21 21 tag = get_object_or_404(Tag, name=self.tag_name)
22 22
23 23 return tag.get_threads()
24 24
25 25 def get_context_data(self, **kwargs):
26 context = super(TagView, self).get_context_data(**kwargs)
26 params = super(TagView, self).get_context_data(**kwargs)
27 27
28 28 settings_manager = get_settings_manager(kwargs['request'])
29 29
30 30 tag = get_object_or_404(Tag, name=self.tag_name)
31 context[PARAM_TAG] = tag
31 params[PARAM_TAG] = tag
32 32
33 context[PARAM_FAV_TAGS] = settings_manager.get_fav_tags()
34 context[PARAM_HIDDEN_TAGS] = settings_manager.get_hidden_tags()
33 params[PARAM_FAV_TAGS] = settings_manager.get_fav_tags()
34 params[PARAM_HIDDEN_TAGS] = settings_manager.get_hidden_tags()
35 35
36 return context
36 return params
37 37
38 38 def get(self, request, tag_name, page=DEFAULT_PAGE, form=None):
39 39 self.tag_name = tag_name
40 40
41 41 dispatch_result = self.dispatch_method(request)
42 42 if dispatch_result:
43 43 return dispatch_result
44 44 else:
45 45 return super(TagView, self).get(request, page, form)
46 46
47 47 def post(self, request, tag_name, page=DEFAULT_PAGE):
48 48 form = ThreadForm(request.POST, request.FILES,
49 49 error_class=PlainErrorList)
50 50 form.session = request.session
51 51
52 52 if form.is_valid():
53 53 return self.create_thread(request, form)
54 54 if form.need_to_ban:
55 55 # Ban user because he is suspected to be a bot
56 56 self._ban_current_user(request)
57 57
58 58 return self.get(request, tag_name, page, form)
59 59
60 60 def subscribe(self, request):
61 61 tag = get_object_or_404(Tag, name=self.tag_name)
62 62
63 63 settings_manager = get_settings_manager(request)
64 64 settings_manager.add_fav_tag(tag)
65 65
66 66 return self.redirect_to_next(request)
67 67
68 68 def unsubscribe(self, request):
69 69 tag = get_object_or_404(Tag, name=self.tag_name)
70 70
71 71 settings_manager = get_settings_manager(request)
72 72 settings_manager.del_fav_tag(tag)
73 73
74 74 return self.redirect_to_next(request)
75 75
76 76 def hide(self, request):
77 77 """
78 78 Adds tag to user's hidden tags. Threads with this tag will not be
79 79 shown.
80 80 """
81 81
82 82 tag = get_object_or_404(Tag, name=self.tag_name)
83 83
84 84 settings_manager = get_settings_manager(request)
85 85 settings_manager.add_hidden_tag(tag)
86 86
87 87 def unhide(self, request):
88 88 """
89 89 Removed tag from user's hidden tags.
90 90 """
91 91
92 92 tag = get_object_or_404(Tag, name=self.tag_name)
93 93
94 94 settings_manager = get_settings_manager(request)
95 95 settings_manager.del_hidden_tag(tag)
General Comments 0
You need to be logged in to leave comments. Login now