Show More
@@ -1,150 +1,153 b'' | |||||
1 | from django.core.urlresolvers import reverse |
|
1 | from django.core.urlresolvers import reverse | |
2 | from django.core.files import File |
|
2 | from django.core.files import File | |
3 | from django.core.files.temp import NamedTemporaryFile |
|
3 | from django.core.files.temp import NamedTemporaryFile | |
4 | from django.core.paginator import EmptyPage |
|
4 | from django.core.paginator import EmptyPage | |
5 | from django.db import transaction |
|
5 | from django.db import transaction | |
6 | from django.http import Http404 |
|
6 | from django.http import Http404 | |
7 | from django.shortcuts import render, redirect |
|
7 | from django.shortcuts import render, redirect | |
8 | import requests |
|
8 | import requests | |
9 |
|
9 | |||
10 | from boards import utils, settings |
|
10 | from boards import utils, settings | |
11 | from boards.abstracts.paginator import get_paginator |
|
11 | from boards.abstracts.paginator import get_paginator | |
12 | from boards.abstracts.settingsmanager import get_settings_manager |
|
12 | from boards.abstracts.settingsmanager import get_settings_manager | |
13 | from boards.forms import ThreadForm, PlainErrorList |
|
13 | from boards.forms import ThreadForm, PlainErrorList | |
14 | from boards.models import Post, Thread, Ban, Tag, PostImage, Banner |
|
14 | from boards.models import Post, Thread, Ban, Tag, PostImage, Banner | |
15 | from boards.views.banned import BannedView |
|
15 | from boards.views.banned import BannedView | |
16 | from boards.views.base import BaseBoardView, CONTEXT_FORM |
|
16 | from boards.views.base import BaseBoardView, CONTEXT_FORM | |
17 | from boards.views.posting_mixin import PostMixin |
|
17 | from boards.views.posting_mixin import PostMixin | |
18 |
|
18 | |||
19 |
|
19 | |||
20 | FORM_TAGS = 'tags' |
|
20 | FORM_TAGS = 'tags' | |
21 | FORM_TEXT = 'text' |
|
21 | FORM_TEXT = 'text' | |
22 | FORM_TITLE = 'title' |
|
22 | FORM_TITLE = 'title' | |
23 | FORM_IMAGE = 'image' |
|
23 | FORM_IMAGE = 'image' | |
24 | FORM_THREADS = 'threads' |
|
24 | FORM_THREADS = 'threads' | |
25 |
|
25 | |||
26 | TAG_DELIMITER = ' ' |
|
26 | TAG_DELIMITER = ' ' | |
27 |
|
27 | |||
28 | PARAMETER_CURRENT_PAGE = 'current_page' |
|
28 | PARAMETER_CURRENT_PAGE = 'current_page' | |
29 | PARAMETER_PAGINATOR = 'paginator' |
|
29 | PARAMETER_PAGINATOR = 'paginator' | |
30 | PARAMETER_THREADS = 'threads' |
|
30 | PARAMETER_THREADS = 'threads' | |
31 | PARAMETER_BANNERS = 'banners' |
|
31 | PARAMETER_BANNERS = 'banners' | |
32 |
|
32 | |||
33 | PARAMETER_PREV_LINK = 'prev_page_link' |
|
33 | PARAMETER_PREV_LINK = 'prev_page_link' | |
34 | PARAMETER_NEXT_LINK = 'next_page_link' |
|
34 | PARAMETER_NEXT_LINK = 'next_page_link' | |
35 |
|
35 | |||
36 | TEMPLATE = 'boards/all_threads.html' |
|
36 | TEMPLATE = 'boards/all_threads.html' | |
37 | DEFAULT_PAGE = 1 |
|
37 | DEFAULT_PAGE = 1 | |
38 |
|
38 | |||
39 |
|
39 | |||
40 | class AllThreadsView(PostMixin, BaseBoardView): |
|
40 | class AllThreadsView(PostMixin, BaseBoardView): | |
41 |
|
41 | |||
42 | def __init__(self): |
|
42 | def __init__(self): | |
43 | self.settings_manager = None |
|
43 | self.settings_manager = None | |
44 | super(AllThreadsView, self).__init__() |
|
44 | super(AllThreadsView, self).__init__() | |
45 |
|
45 | |||
46 | def get(self, request, form: ThreadForm=None): |
|
46 | def get(self, request, form: ThreadForm=None): | |
47 | page = request.GET.get('page', DEFAULT_PAGE) |
|
47 | page = request.GET.get('page', DEFAULT_PAGE) | |
48 |
|
48 | |||
49 | params = self.get_context_data(request=request) |
|
49 | params = self.get_context_data(request=request) | |
50 |
|
50 | |||
51 | if not form: |
|
51 | if not form: | |
52 | form = ThreadForm(error_class=PlainErrorList) |
|
52 | form = ThreadForm(error_class=PlainErrorList) | |
53 |
|
53 | |||
54 | self.settings_manager = get_settings_manager(request) |
|
54 | self.settings_manager = get_settings_manager(request) | |
55 | paginator = get_paginator(self.get_threads(), |
|
55 | paginator = get_paginator(self.get_threads().order_by('-bump_time'), | |
56 | settings.get_int('View', 'ThreadsPerPage')) |
|
56 | settings.get_int('View', 'ThreadsPerPage')) | |
57 | paginator.current_page = int(page) |
|
57 | paginator.current_page = int(page) | |
58 |
|
58 | |||
59 | try: |
|
59 | try: | |
60 | threads = paginator.page(page).object_list |
|
60 | threads = paginator.page(page).object_list | |
61 | except EmptyPage: |
|
61 | except EmptyPage: | |
62 | raise Http404() |
|
62 | raise Http404() | |
63 |
|
63 | |||
64 | params[PARAMETER_THREADS] = threads |
|
64 | params[PARAMETER_THREADS] = threads | |
65 | params[CONTEXT_FORM] = form |
|
65 | params[CONTEXT_FORM] = form | |
66 | params[PARAMETER_BANNERS] = Banner.objects.order_by('-id').all() |
|
66 | params[PARAMETER_BANNERS] = Banner.objects.order_by('-id').all() | |
67 |
|
67 | |||
68 | self.get_page_context(paginator, params, page) |
|
68 | self.get_page_context(paginator, params, page) | |
69 |
|
69 | |||
70 | return render(request, TEMPLATE, params) |
|
70 | return render(request, TEMPLATE, params) | |
71 |
|
71 | |||
72 | def post(self, request): |
|
72 | def post(self, request): | |
73 | form = ThreadForm(request.POST, request.FILES, |
|
73 | form = ThreadForm(request.POST, request.FILES, | |
74 | error_class=PlainErrorList) |
|
74 | error_class=PlainErrorList) | |
75 | form.session = request.session |
|
75 | form.session = request.session | |
76 |
|
76 | |||
77 | if form.is_valid(): |
|
77 | if form.is_valid(): | |
78 | return self.create_thread(request, form) |
|
78 | return self.create_thread(request, form) | |
79 | if form.need_to_ban: |
|
79 | if form.need_to_ban: | |
80 | # Ban user because he is suspected to be a bot |
|
80 | # Ban user because he is suspected to be a bot | |
81 | self._ban_current_user(request) |
|
81 | self._ban_current_user(request) | |
82 |
|
82 | |||
83 | return self.get(request, form) |
|
83 | return self.get(request, form) | |
84 |
|
84 | |||
85 | def get_page_context(self, paginator, params, page): |
|
85 | def get_page_context(self, paginator, params, page): | |
86 | """ |
|
86 | """ | |
87 | Get pagination context variables |
|
87 | Get pagination context variables | |
88 | """ |
|
88 | """ | |
89 |
|
89 | |||
90 | params[PARAMETER_PAGINATOR] = paginator |
|
90 | params[PARAMETER_PAGINATOR] = paginator | |
91 | current_page = paginator.page(int(page)) |
|
91 | current_page = paginator.page(int(page)) | |
92 | params[PARAMETER_CURRENT_PAGE] = current_page |
|
92 | params[PARAMETER_CURRENT_PAGE] = current_page | |
93 | if current_page.has_previous(): |
|
93 | if current_page.has_previous(): | |
94 | params[PARAMETER_PREV_LINK] = self.get_previous_page_link( |
|
94 | params[PARAMETER_PREV_LINK] = self.get_previous_page_link( | |
95 | current_page) |
|
95 | current_page) | |
96 | if current_page.has_next(): |
|
96 | if current_page.has_next(): | |
97 | params[PARAMETER_NEXT_LINK] = self.get_next_page_link(current_page) |
|
97 | params[PARAMETER_NEXT_LINK] = self.get_next_page_link(current_page) | |
98 |
|
98 | |||
99 | def get_previous_page_link(self, current_page): |
|
99 | def get_previous_page_link(self, current_page): | |
100 |
return reverse( |
|
100 | return self.get_reverse_url() + '?page=' \ | |
101 | + str(current_page.previous_page_number()) |
|
101 | + str(current_page.previous_page_number()) | |
102 |
|
102 | |||
103 | def get_next_page_link(self, current_page): |
|
103 | def get_next_page_link(self, current_page): | |
104 |
return reverse( |
|
104 | return self.get_reverse_url() + '?page=' \ | |
105 | + str(current_page.next_page_number()) |
|
105 | + str(current_page.next_page_number()) | |
106 |
|
106 | |||
|
107 | def get_reverse_url(self): | |||
|
108 | return reverse('index') | |||
|
109 | ||||
107 | @transaction.atomic |
|
110 | @transaction.atomic | |
108 | def create_thread(self, request, form: ThreadForm, html_response=True): |
|
111 | def create_thread(self, request, form: ThreadForm, html_response=True): | |
109 | """ |
|
112 | """ | |
110 | Creates a new thread with an opening post. |
|
113 | Creates a new thread with an opening post. | |
111 | """ |
|
114 | """ | |
112 |
|
115 | |||
113 | ip = utils.get_client_ip(request) |
|
116 | ip = utils.get_client_ip(request) | |
114 | is_banned = Ban.objects.filter(ip=ip).exists() |
|
117 | is_banned = Ban.objects.filter(ip=ip).exists() | |
115 |
|
118 | |||
116 | if is_banned: |
|
119 | if is_banned: | |
117 | if html_response: |
|
120 | if html_response: | |
118 | return redirect(BannedView().as_view()) |
|
121 | return redirect(BannedView().as_view()) | |
119 | else: |
|
122 | else: | |
120 | return |
|
123 | return | |
121 |
|
124 | |||
122 | data = form.cleaned_data |
|
125 | data = form.cleaned_data | |
123 |
|
126 | |||
124 | title = form.get_title() |
|
127 | title = form.get_title() | |
125 | text = data[FORM_TEXT] |
|
128 | text = data[FORM_TEXT] | |
126 | file = form.get_file() |
|
129 | file = form.get_file() | |
127 | threads = data[FORM_THREADS] |
|
130 | threads = data[FORM_THREADS] | |
128 |
|
131 | |||
129 | text = self._remove_invalid_links(text) |
|
132 | text = self._remove_invalid_links(text) | |
130 |
|
133 | |||
131 | tags = data[FORM_TAGS] |
|
134 | tags = data[FORM_TAGS] | |
132 |
|
135 | |||
133 | post = Post.objects.create_post(title=title, text=text, file=file, |
|
136 | post = Post.objects.create_post(title=title, text=text, file=file, | |
134 | ip=ip, tags=tags, opening_posts=threads, |
|
137 | ip=ip, tags=tags, opening_posts=threads, | |
135 | tripcode=form.get_tripcode()) |
|
138 | tripcode=form.get_tripcode()) | |
136 |
|
139 | |||
137 | # This is required to update the threads to which posts we have replied |
|
140 | # This is required to update the threads to which posts we have replied | |
138 | # when creating this one |
|
141 | # when creating this one | |
139 | post.notify_clients() |
|
142 | post.notify_clients() | |
140 |
|
143 | |||
141 | if html_response: |
|
144 | if html_response: | |
142 | return redirect(post.get_absolute_url()) |
|
145 | return redirect(post.get_absolute_url()) | |
143 |
|
146 | |||
144 | def get_threads(self): |
|
147 | def get_threads(self): | |
145 | """ |
|
148 | """ | |
146 | Gets list of threads that will be shown on a page. |
|
149 | Gets list of threads that will be shown on a page. | |
147 | """ |
|
150 | """ | |
148 |
|
151 | |||
149 |
return Thread.objects |
|
152 | return Thread.objects\ | |
150 | .exclude(tags__in=self.settings_manager.get_hidden_tags()) |
|
153 | .exclude(tags__in=self.settings_manager.get_hidden_tags()) |
@@ -1,125 +1,119 b'' | |||||
1 | from django.shortcuts import get_object_or_404, redirect |
|
1 | from django.shortcuts import get_object_or_404, redirect | |
2 | from django.core.urlresolvers import reverse |
|
2 | from django.core.urlresolvers import reverse | |
3 |
|
3 | |||
4 | from boards.abstracts.settingsmanager import get_settings_manager, \ |
|
4 | from boards.abstracts.settingsmanager import get_settings_manager, \ | |
5 | SETTING_FAVORITE_TAGS, SETTING_HIDDEN_TAGS |
|
5 | SETTING_FAVORITE_TAGS, SETTING_HIDDEN_TAGS | |
6 | from boards.models import Tag, PostImage |
|
6 | from boards.models import Tag, PostImage | |
7 | from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE |
|
7 | from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE | |
8 | from boards.views.mixins import DispatcherMixin, PARAMETER_METHOD |
|
8 | from boards.views.mixins import DispatcherMixin, PARAMETER_METHOD | |
9 | from boards.forms import ThreadForm, PlainErrorList |
|
9 | from boards.forms import ThreadForm, PlainErrorList | |
10 |
|
10 | |||
11 | PARAM_HIDDEN_TAGS = 'hidden_tags' |
|
11 | PARAM_HIDDEN_TAGS = 'hidden_tags' | |
12 | PARAM_TAG = 'tag' |
|
12 | PARAM_TAG = 'tag' | |
13 | PARAM_IS_FAVORITE = 'is_favorite' |
|
13 | PARAM_IS_FAVORITE = 'is_favorite' | |
14 | PARAM_IS_HIDDEN = 'is_hidden' |
|
14 | PARAM_IS_HIDDEN = 'is_hidden' | |
15 | PARAM_RANDOM_IMAGE_POST = 'random_image_post' |
|
15 | PARAM_RANDOM_IMAGE_POST = 'random_image_post' | |
16 | PARAM_RELATED_TAGS = 'related_tags' |
|
16 | PARAM_RELATED_TAGS = 'related_tags' | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | __author__ = 'neko259' |
|
19 | __author__ = 'neko259' | |
20 |
|
20 | |||
21 |
|
21 | |||
22 | class TagView(AllThreadsView, DispatcherMixin): |
|
22 | class TagView(AllThreadsView, DispatcherMixin): | |
23 |
|
23 | |||
24 | tag_name = None |
|
24 | tag_name = None | |
25 |
|
25 | |||
26 | def get_threads(self): |
|
26 | def get_threads(self): | |
27 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
27 | tag = get_object_or_404(Tag, name=self.tag_name) | |
28 |
|
28 | |||
29 | hidden_tags = self.settings_manager.get_hidden_tags() |
|
29 | hidden_tags = self.settings_manager.get_hidden_tags() | |
30 |
|
30 | |||
31 | try: |
|
31 | try: | |
32 | hidden_tags.remove(tag) |
|
32 | hidden_tags.remove(tag) | |
33 | except ValueError: |
|
33 | except ValueError: | |
34 | pass |
|
34 | pass | |
35 |
|
35 | |||
36 | return tag.get_threads().exclude( |
|
36 | return tag.get_threads().exclude( | |
37 | tags__in=hidden_tags) |
|
37 | tags__in=hidden_tags) | |
38 |
|
38 | |||
39 | def get_context_data(self, **kwargs): |
|
39 | def get_context_data(self, **kwargs): | |
40 | params = super(TagView, self).get_context_data(**kwargs) |
|
40 | params = super(TagView, self).get_context_data(**kwargs) | |
41 |
|
41 | |||
42 | settings_manager = get_settings_manager(kwargs['request']) |
|
42 | settings_manager = get_settings_manager(kwargs['request']) | |
43 |
|
43 | |||
44 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
44 | tag = get_object_or_404(Tag, name=self.tag_name) | |
45 | params[PARAM_TAG] = tag |
|
45 | params[PARAM_TAG] = tag | |
46 |
|
46 | |||
47 | fav_tag_names = settings_manager.get_setting(SETTING_FAVORITE_TAGS) |
|
47 | fav_tag_names = settings_manager.get_setting(SETTING_FAVORITE_TAGS) | |
48 | hidden_tag_names = settings_manager.get_setting(SETTING_HIDDEN_TAGS) |
|
48 | hidden_tag_names = settings_manager.get_setting(SETTING_HIDDEN_TAGS) | |
49 |
|
49 | |||
50 | params[PARAM_IS_FAVORITE] = fav_tag_names is not None and tag.name in fav_tag_names |
|
50 | params[PARAM_IS_FAVORITE] = fav_tag_names is not None and tag.name in fav_tag_names | |
51 | params[PARAM_IS_HIDDEN] = hidden_tag_names is not None and tag.name in hidden_tag_names |
|
51 | params[PARAM_IS_HIDDEN] = hidden_tag_names is not None and tag.name in hidden_tag_names | |
52 |
|
52 | |||
53 | params[PARAM_RANDOM_IMAGE_POST] = tag.get_random_image_post() |
|
53 | params[PARAM_RANDOM_IMAGE_POST] = tag.get_random_image_post() | |
54 | params[PARAM_RELATED_TAGS] = tag.get_related_tags() |
|
54 | params[PARAM_RELATED_TAGS] = tag.get_related_tags() | |
55 |
|
55 | |||
56 | return params |
|
56 | return params | |
57 |
|
57 | |||
58 |
def get_ |
|
58 | def get_reverse_url(self): | |
59 | return reverse('tag', kwargs={ |
|
59 | return reverse('tag', kwargs={ | |
60 |
'tag_name': self.tag_name |
|
60 | 'tag_name': self.tag_name}) | |
61 | }) + '?page=' + str(current_page.previous_page_number()) |
|
|||
62 |
|
||||
63 | def get_next_page_link(self, current_page): |
|
|||
64 | return reverse('tag', kwargs={ |
|
|||
65 | 'tag_name': self.tag_name, |
|
|||
66 | }) + '?page=' + str(current_page.next_page_number()) |
|
|||
67 |
|
61 | |||
68 | def get(self, request, tag_name, form=None): |
|
62 | def get(self, request, tag_name, form=None): | |
69 | self.tag_name = tag_name |
|
63 | self.tag_name = tag_name | |
70 |
|
64 | |||
71 | return super(TagView, self).get(request, form) |
|
65 | return super(TagView, self).get(request, form) | |
72 |
|
66 | |||
73 |
|
67 | |||
74 | def post(self, request, tag_name): |
|
68 | def post(self, request, tag_name): | |
75 | self.tag_name = tag_name |
|
69 | self.tag_name = tag_name | |
76 |
|
70 | |||
77 | if PARAMETER_METHOD in request.POST: |
|
71 | if PARAMETER_METHOD in request.POST: | |
78 | self.dispatch_method(request) |
|
72 | self.dispatch_method(request) | |
79 |
|
73 | |||
80 | return redirect('tag', tag_name) |
|
74 | return redirect('tag', tag_name) | |
81 | else: |
|
75 | else: | |
82 | form = ThreadForm(request.POST, request.FILES, |
|
76 | form = ThreadForm(request.POST, request.FILES, | |
83 | error_class=PlainErrorList) |
|
77 | error_class=PlainErrorList) | |
84 | form.session = request.session |
|
78 | form.session = request.session | |
85 |
|
79 | |||
86 | if form.is_valid(): |
|
80 | if form.is_valid(): | |
87 | return self.create_thread(request, form) |
|
81 | return self.create_thread(request, form) | |
88 | if form.need_to_ban: |
|
82 | if form.need_to_ban: | |
89 | # Ban user because he is suspected to be a bot |
|
83 | # Ban user because he is suspected to be a bot | |
90 | self._ban_current_user(request) |
|
84 | self._ban_current_user(request) | |
91 |
|
85 | |||
92 | return self.get(request, tag_name, form) |
|
86 | return self.get(request, tag_name, form) | |
93 |
|
87 | |||
94 | def subscribe(self, request): |
|
88 | def subscribe(self, request): | |
95 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
89 | tag = get_object_or_404(Tag, name=self.tag_name) | |
96 |
|
90 | |||
97 | settings_manager = get_settings_manager(request) |
|
91 | settings_manager = get_settings_manager(request) | |
98 | settings_manager.add_fav_tag(tag) |
|
92 | settings_manager.add_fav_tag(tag) | |
99 |
|
93 | |||
100 | def unsubscribe(self, request): |
|
94 | def unsubscribe(self, request): | |
101 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
95 | tag = get_object_or_404(Tag, name=self.tag_name) | |
102 |
|
96 | |||
103 | settings_manager = get_settings_manager(request) |
|
97 | settings_manager = get_settings_manager(request) | |
104 | settings_manager.del_fav_tag(tag) |
|
98 | settings_manager.del_fav_tag(tag) | |
105 |
|
99 | |||
106 | def hide(self, request): |
|
100 | def hide(self, request): | |
107 | """ |
|
101 | """ | |
108 | Adds tag to user's hidden tags. Threads with this tag will not be |
|
102 | Adds tag to user's hidden tags. Threads with this tag will not be | |
109 | shown. |
|
103 | shown. | |
110 | """ |
|
104 | """ | |
111 |
|
105 | |||
112 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
106 | tag = get_object_or_404(Tag, name=self.tag_name) | |
113 |
|
107 | |||
114 | settings_manager = get_settings_manager(request) |
|
108 | settings_manager = get_settings_manager(request) | |
115 | settings_manager.add_hidden_tag(tag) |
|
109 | settings_manager.add_hidden_tag(tag) | |
116 |
|
110 | |||
117 | def unhide(self, request): |
|
111 | def unhide(self, request): | |
118 | """ |
|
112 | """ | |
119 | Removed tag from user's hidden tags. |
|
113 | Removed tag from user's hidden tags. | |
120 | """ |
|
114 | """ | |
121 |
|
115 | |||
122 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
116 | tag = get_object_or_404(Tag, name=self.tag_name) | |
123 |
|
117 | |||
124 | settings_manager = get_settings_manager(request) |
|
118 | settings_manager = get_settings_manager(request) | |
125 | settings_manager.del_hidden_tag(tag) |
|
119 | settings_manager.del_hidden_tag(tag) |
General Comments 0
You need to be logged in to leave comments.
Login now