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