##// END OF EJS Templates
Throw error 404 when trying to open invalid page in threads list
neko259 -
r977:6b112efe default
parent child Browse files
Show More
@@ -1,140 +1,145 b''
1 from django.core.files import File
1 from django.core.files import File
2 from django.core.files.temp import NamedTemporaryFile
2 from django.core.files.temp import NamedTemporaryFile
3 from django.core.paginator import EmptyPage
3 from django.db import transaction
4 from django.db import transaction
5 from django.http import Http404
4 from django.shortcuts import render, redirect
6 from django.shortcuts import render, redirect
5 import requests
7 import requests
6
8
7 from boards import utils, settings
9 from boards import utils, settings
8 from boards.abstracts.paginator import get_paginator
10 from boards.abstracts.paginator import get_paginator
9 from boards.abstracts.settingsmanager import get_settings_manager
11 from boards.abstracts.settingsmanager import get_settings_manager
10 from boards.forms import ThreadForm, PlainErrorList
12 from boards.forms import ThreadForm, PlainErrorList
11 from boards.models import Post, Thread, Ban, Tag, PostImage
13 from boards.models import Post, Thread, Ban, Tag, PostImage
12 from boards.views.banned import BannedView
14 from boards.views.banned import BannedView
13 from boards.views.base import BaseBoardView, CONTEXT_FORM
15 from boards.views.base import BaseBoardView, CONTEXT_FORM
14 from boards.views.posting_mixin import PostMixin
16 from boards.views.posting_mixin import PostMixin
15
17
16
18
17 FORM_TAGS = 'tags'
19 FORM_TAGS = 'tags'
18 FORM_TEXT = 'text'
20 FORM_TEXT = 'text'
19 FORM_TITLE = 'title'
21 FORM_TITLE = 'title'
20 FORM_IMAGE = 'image'
22 FORM_IMAGE = 'image'
21
23
22 TAG_DELIMITER = ' '
24 TAG_DELIMITER = ' '
23
25
24 PARAMETER_CURRENT_PAGE = 'current_page'
26 PARAMETER_CURRENT_PAGE = 'current_page'
25 PARAMETER_PAGINATOR = 'paginator'
27 PARAMETER_PAGINATOR = 'paginator'
26 PARAMETER_THREADS = 'threads'
28 PARAMETER_THREADS = 'threads'
27
29
28 TEMPLATE = 'boards/posting_general.html'
30 TEMPLATE = 'boards/posting_general.html'
29 DEFAULT_PAGE = 1
31 DEFAULT_PAGE = 1
30
32
31
33
32 class AllThreadsView(PostMixin, BaseBoardView):
34 class AllThreadsView(PostMixin, BaseBoardView):
33
35
34 def __init__(self):
36 def __init__(self):
35 self.settings_manager = None
37 self.settings_manager = None
36 super(AllThreadsView, self).__init__()
38 super(AllThreadsView, self).__init__()
37
39
38 def get(self, request, page=DEFAULT_PAGE, form: ThreadForm=None):
40 def get(self, request, page=DEFAULT_PAGE, form: ThreadForm=None):
39 params = self.get_context_data(request=request)
41 params = self.get_context_data(request=request)
40
42
41 if not form:
43 if not form:
42 form = ThreadForm(error_class=PlainErrorList)
44 form = ThreadForm(error_class=PlainErrorList)
43
45
44 self.settings_manager = get_settings_manager(request)
46 self.settings_manager = get_settings_manager(request)
45 paginator = get_paginator(self.get_threads(),
47 paginator = get_paginator(self.get_threads(),
46 settings.THREADS_PER_PAGE)
48 settings.THREADS_PER_PAGE)
47 paginator.current_page = int(page)
49 paginator.current_page = int(page)
48
50
49 threads = paginator.page(page).object_list
51 try:
52 threads = paginator.page(page).object_list
53 except EmptyPage:
54 raise Http404()
50
55
51 params[PARAMETER_THREADS] = threads
56 params[PARAMETER_THREADS] = threads
52 params[CONTEXT_FORM] = form
57 params[CONTEXT_FORM] = form
53
58
54 self._get_page_context(paginator, params, page)
59 self._get_page_context(paginator, params, page)
55
60
56 return render(request, TEMPLATE, params)
61 return render(request, TEMPLATE, params)
57
62
58 def post(self, request, page=DEFAULT_PAGE):
63 def post(self, request, page=DEFAULT_PAGE):
59 form = ThreadForm(request.POST, request.FILES,
64 form = ThreadForm(request.POST, request.FILES,
60 error_class=PlainErrorList)
65 error_class=PlainErrorList)
61 form.session = request.session
66 form.session = request.session
62
67
63 if form.is_valid():
68 if form.is_valid():
64 return self.create_thread(request, form)
69 return self.create_thread(request, form)
65 if form.need_to_ban:
70 if form.need_to_ban:
66 # Ban user because he is suspected to be a bot
71 # Ban user because he is suspected to be a bot
67 self._ban_current_user(request)
72 self._ban_current_user(request)
68
73
69 return self.get(request, page, form)
74 return self.get(request, page, form)
70
75
71 def _get_page_context(self, paginator, params, page):
76 def _get_page_context(self, paginator, params, page):
72 """
77 """
73 Get pagination context variables
78 Get pagination context variables
74 """
79 """
75
80
76 params[PARAMETER_PAGINATOR] = paginator
81 params[PARAMETER_PAGINATOR] = paginator
77 params[PARAMETER_CURRENT_PAGE] = paginator.page(int(page))
82 params[PARAMETER_CURRENT_PAGE] = paginator.page(int(page))
78
83
79 @staticmethod
84 @staticmethod
80 def parse_tags_string(tag_strings):
85 def parse_tags_string(tag_strings):
81 """
86 """
82 Parses tag list string and returns tag object list.
87 Parses tag list string and returns tag object list.
83 """
88 """
84
89
85 tags = []
90 tags = []
86
91
87 if tag_strings:
92 if tag_strings:
88 tag_strings = tag_strings.split(TAG_DELIMITER)
93 tag_strings = tag_strings.split(TAG_DELIMITER)
89 for tag_name in tag_strings:
94 for tag_name in tag_strings:
90 tag_name = tag_name.strip().lower()
95 tag_name = tag_name.strip().lower()
91 if len(tag_name) > 0:
96 if len(tag_name) > 0:
92 tag, created = Tag.objects.get_or_create(name=tag_name)
97 tag, created = Tag.objects.get_or_create(name=tag_name)
93 tags.append(tag)
98 tags.append(tag)
94
99
95 return tags
100 return tags
96
101
97 @transaction.atomic
102 @transaction.atomic
98 def create_thread(self, request, form: ThreadForm, html_response=True):
103 def create_thread(self, request, form: ThreadForm, html_response=True):
99 """
104 """
100 Creates a new thread with an opening post.
105 Creates a new thread with an opening post.
101 """
106 """
102
107
103 ip = utils.get_client_ip(request)
108 ip = utils.get_client_ip(request)
104 is_banned = Ban.objects.filter(ip=ip).exists()
109 is_banned = Ban.objects.filter(ip=ip).exists()
105
110
106 if is_banned:
111 if is_banned:
107 if html_response:
112 if html_response:
108 return redirect(BannedView().as_view())
113 return redirect(BannedView().as_view())
109 else:
114 else:
110 return
115 return
111
116
112 data = form.cleaned_data
117 data = form.cleaned_data
113
118
114 title = data[FORM_TITLE]
119 title = data[FORM_TITLE]
115 text = data[FORM_TEXT]
120 text = data[FORM_TEXT]
116 image = form.get_image()
121 image = form.get_image()
117
122
118 text = self._remove_invalid_links(text)
123 text = self._remove_invalid_links(text)
119
124
120 tag_strings = data[FORM_TAGS]
125 tag_strings = data[FORM_TAGS]
121
126
122 tags = self.parse_tags_string(tag_strings)
127 tags = self.parse_tags_string(tag_strings)
123
128
124 post = Post.objects.create_post(title=title, text=text, image=image,
129 post = Post.objects.create_post(title=title, text=text, image=image,
125 ip=ip, tags=tags)
130 ip=ip, tags=tags)
126
131
127 # This is required to update the threads to which posts we have replied
132 # This is required to update the threads to which posts we have replied
128 # when creating this one
133 # when creating this one
129 post.send_to_websocket(request)
134 post.send_to_websocket(request)
130
135
131 if html_response:
136 if html_response:
132 return redirect(post.get_url())
137 return redirect(post.get_url())
133
138
134 def get_threads(self):
139 def get_threads(self):
135 """
140 """
136 Gets list of threads that will be shown on a page.
141 Gets list of threads that will be shown on a page.
137 """
142 """
138
143
139 return Thread.objects.order_by('-bump_time')\
144 return Thread.objects.order_by('-bump_time')\
140 .exclude(tags__in=self.settings_manager.get_hidden_tags())
145 .exclude(tags__in=self.settings_manager.get_hidden_tags())
General Comments 0
You need to be logged in to leave comments. Login now