##// END OF EJS Templates
Simplify paginator page list building and unified it between different views
neko259 -
r2061:b31844e6 default
parent child Browse files
Show More
@@ -1,93 +1,106 b''
1 from django.core.paginator import Paginator
1 from boards.abstracts.constants import PARAM_PAGE
2 from boards.abstracts.constants import PARAM_PAGE
2
3
3 __author__ = 'neko259'
4 __author__ = 'neko259'
4
5
5 from django.core.paginator import Paginator
6 PATTERN_PAGE_URL = '<a {} href="{}">{}</a>'
6
7 PAGINATOR_LOOKAROUND_SIZE = 2
7 PAGINATOR_LOOKAROUND_SIZE = 2
8
8
9
9
10 def get_paginator(*args, **kwargs):
10 def get_paginator(*args, **kwargs):
11 return DividedPaginator(*args, **kwargs)
11 return DividedPaginator(*args, **kwargs)
12
12
13
13
14 class DividedPaginator(Paginator):
14 class DividedPaginator(Paginator):
15
15
16 lookaround_size = PAGINATOR_LOOKAROUND_SIZE
16 lookaround_size = PAGINATOR_LOOKAROUND_SIZE
17
17
18 def __init__(self, object_list, per_page, orphans=0,
18 def __init__(self, object_list, per_page, orphans=0,
19 allow_empty_first_page=True, current_page=1):
19 allow_empty_first_page=True, current_page=1, link=None, params=None):
20 super().__init__(object_list, per_page, orphans, allow_empty_first_page)
20 super().__init__(object_list, per_page, orphans, allow_empty_first_page)
21
21
22 self.link = None
22 self.link = link
23 self.params = None
23 self.params = params
24 self.current_page = current_page
24 self.current_page = current_page
25
25
26 def _left_range(self):
26 def _left_range(self):
27 return self.page_range[:self.lookaround_size]
27 return self.page_range[:self.lookaround_size]
28
28
29 def _right_range(self):
29 def _right_range(self):
30 pages = self.num_pages-self.lookaround_size
30 pages = self.num_pages-self.lookaround_size
31 if pages <= 0:
31 if pages <= 0:
32 return []
32 return []
33 else:
33 else:
34 return self.page_range[pages:]
34 return self.page_range[pages:]
35
35
36 def _center_range(self):
36 def _center_range(self):
37 index = self.page_range.index(self.current_page)
37 index = self.page_range.index(self.current_page)
38
38
39 start = max(self.lookaround_size, index - self.lookaround_size)
39 start = max(self.lookaround_size, index - self.lookaround_size)
40 end = min(self.num_pages - self.lookaround_size, index + self.lookaround_size + 1)
40 end = min(self.num_pages - self.lookaround_size, index + self.lookaround_size + 1)
41 return self.page_range[start:end]
41 return self.page_range[start:end]
42
42
43 def get_divided_range(self):
43 def get_divided_range(self):
44 dr = list()
44 dr = list()
45
45
46 dr += self._left_range()
46 dr += self._left_range()
47 dr += self._center_range()
47 dr += self._center_range()
48 dr += self._right_range()
48 dr += self._right_range()
49
49
50 # Remove duplicates
50 # Remove duplicates
51 dr = list(set(dr))
51 dr = list(set(dr))
52 dr.sort()
52 dr.sort()
53
53
54 return dr
54 return dr
55
55
56 def get_dividers(self):
56 def get_dividers(self):
57 dividers = []
57 dividers = []
58
58
59 prev_page = 1
59 prev_page = 1
60 for page in self.get_divided_range():
60 for page in self.get_divided_range():
61 if page - prev_page > 1:
61 if page - prev_page > 1:
62 dividers.append(page)
62 dividers.append(page)
63
63
64 # There can be no more than 2 dividers, so don't bother going
64 # There can be no more than 2 dividers, so don't bother going
65 # further
65 # further
66 if len(dividers) > 2:
66 if len(dividers) > 2:
67 break
67 break
68 prev_page = page
68 prev_page = page
69
69
70 return dividers
70 return dividers
71
71
72 def set_url(self, link, params):
73 self.link = link
74 self.params = params
75
76 def get_page_url(self, page):
72 def get_page_url(self, page):
77 self.params[PARAM_PAGE] = page
73 self.params[PARAM_PAGE] = page
78 url_params = '&'.join(['{}={}'.format(key, self.params[key])
74 url_params = '&'.join(['{}={}'.format(key, self.params[key])
79 for key in self.params.keys()])
75 for key in self.params.keys()])
80 return '{}?{}'.format(self.link, url_params)
76 return '{}?{}'.format(self.link, url_params)
81
77
82 def supports_urls(self):
78 def supports_urls(self):
83 return self.link is not None and self.params is not None
79 return self.link is not None and self.params is not None
84
80
85 def get_next_page_url(self):
81 def get_next_page_url(self):
86 current = self.page(self.current_page)
82 current = self.page(self.current_page)
87 if current.has_next():
83 if current.has_next():
88 return self.get_page_url(current.next_page_number())
84 return self.get_page_url(current.next_page_number())
89
85
90 def get_prev_page_url(self):
86 def get_prev_page_url(self):
91 current = self.page(self.current_page)
87 current = self.page(self.current_page)
92 if current.has_previous():
88 if current.has_previous():
93 return self.get_page_url(current.previous_page_number())
89 return self.get_page_url(current.previous_page_number())
90
91 def get_page_url_list(self):
92 output = []
93
94 dividers = self.get_dividers()
95 for page in self.get_divided_range():
96 if page in dividers:
97 output.append('...')
98
99 if page == self.current_page:
100 cls = 'class="current_page'
101 else:
102 cls = ''
103 output.append(PATTERN_PAGE_URL.format(cls, self.get_page_url(page),
104 page))
105
106 return ', '.join(output)
@@ -1,184 +1,162 b''
1 {% extends "boards/paginated.html" %}
1 {% extends "boards/paginated.html" %}
2
2
3 {% load i18n %}
3 {% load i18n %}
4 {% load board %}
4 {% load board %}
5 {% load static %}
5 {% load static %}
6 {% load tz %}
6 {% load tz %}
7
7
8 {% block head %}
8 {% block head %}
9 <meta name="robots" content="noindex">
9 <meta name="robots" content="noindex">
10
10
11 {% if tag %}
11 {% if tag %}
12 <title>{{ tag.get_localized_name }} - {{ site_name }}</title>
12 <title>{{ tag.get_localized_name }} - {{ site_name }}</title>
13 {% else %}
13 {% else %}
14 <title>{{ site_name }}</title>
14 <title>{{ site_name }}</title>
15 {% endif %}
15 {% endif %}
16
16
17 {% if prev_page_link %}
17 {% if prev_page_link %}
18 <link rel="prev" href="{{ prev_page_link|safe }}" />
18 <link rel="prev" href="{{ prev_page_link|safe }}" />
19 {% endif %}
19 {% endif %}
20 {% if next_page_link %}
20 {% if next_page_link %}
21 <link rel="next" href="{{ next_page_link|safe }}" />
21 <link rel="next" href="{{ next_page_link|safe }}" />
22 {% endif %}
22 {% endif %}
23
23
24 {% endblock %}
24 {% endblock %}
25
25
26 {% block content %}
26 {% block content %}
27
27
28 {% get_current_language as LANGUAGE_CODE %}
28 {% get_current_language as LANGUAGE_CODE %}
29 {% get_current_timezone as TIME_ZONE %}
29 {% get_current_timezone as TIME_ZONE %}
30
30
31 {% for banner in banners %}
31 {% for banner in banners %}
32 <div class="post">
32 <div class="post">
33 <div class="title">{{ banner.title }}</div>
33 <div class="title">{{ banner.title }}</div>
34 <div>{{ banner.get_text|safe }}</div>
34 <div>{{ banner.get_text|safe }}</div>
35 <div>{% trans 'Details' %}: <a href="{{ banner.post.get_absolute_url|safe }}">>>{{ banner.post.id }}</a></div>
35 <div>{% trans 'Details' %}: <a href="{{ banner.post.get_absolute_url|safe }}">>>{{ banner.post.id }}</a></div>
36 </div>
36 </div>
37 {% endfor %}
37 {% endfor %}
38
38
39 {% if tag %}
39 {% if tag %}
40 <div class="tag_info" style="border: solid .5ex #{{ tag.get_color }}">
40 <div class="tag_info" style="border: solid .5ex #{{ tag.get_color }}">
41 {% if random_image_post %}
41 {% if random_image_post %}
42 <div class="tag-image">
42 <div class="tag-image">
43 {% with image=random_image_post.get_first_image %}
43 {% with image=random_image_post.get_first_image %}
44 <a href="{{ random_image_post.get_absolute_url|safe }}"><img
44 <a href="{{ random_image_post.get_absolute_url|safe }}"><img
45 src="{{ image.get_thumb_url|safe }}"
45 src="{{ image.get_thumb_url|safe }}"
46 width="{{ image.get_preview_size.0 }}"
46 width="{{ image.get_preview_size.0 }}"
47 height="{{ image.get_preview_size.1 }}"
47 height="{{ image.get_preview_size.1 }}"
48 alt="{{ random_image_post.id }}"/></a>
48 alt="{{ random_image_post.id }}"/></a>
49 {% endwith %}
49 {% endwith %}
50 </div>
50 </div>
51 {% endif %}
51 {% endif %}
52 <div class="tag-text-data">
52 <div class="tag-text-data">
53 <h2>
53 <h2>
54 /{{ tag.get_view|safe }}/
54 /{{ tag.get_view|safe }}/
55 </h2>
55 </h2>
56 {% if perms.change_tag %}
56 {% if perms.change_tag %}
57 <div class="moderator_info"><a href="{% url 'admin:boards_tag_change' tag.id %}">{% trans 'Edit tag' %}</a></div>
57 <div class="moderator_info"><a href="{% url 'admin:boards_tag_change' tag.id %}">{% trans 'Edit tag' %}</a></div>
58 {% endif %}
58 {% endif %}
59 <p>
59 <p>
60 <form action="{% url 'tag' tag.get_name %}" method="post" class="post-button-form">
60 <form action="{% url 'tag' tag.get_name %}" method="post" class="post-button-form">
61 {% if is_favorite %}
61 {% if is_favorite %}
62 <button name="method" value="unsubscribe" class="fav">β˜… {% trans "Remove from favorites" %}</button>
62 <button name="method" value="unsubscribe" class="fav">β˜… {% trans "Remove from favorites" %}</button>
63 {% else %}
63 {% else %}
64 <button name="method" value="subscribe" class="not_fav">β˜… {% trans "Add to favorites" %}</button>
64 <button name="method" value="subscribe" class="not_fav">β˜… {% trans "Add to favorites" %}</button>
65 {% endif %}
65 {% endif %}
66 </form>
66 </form>
67 &bull;
67 &bull;
68 <form action="{% url 'tag' tag.get_name %}" method="post" class="post-button-form">
68 <form action="{% url 'tag' tag.get_name %}" method="post" class="post-button-form">
69 {% if is_hidden %}
69 {% if is_hidden %}
70 <button name="method" value="unhide" class="fav">{% trans "Show" %}</button>
70 <button name="method" value="unhide" class="fav">{% trans "Show" %}</button>
71 {% else %}
71 {% else %}
72 <button name="method" value="hide" class="not_fav">{% trans "Hide" %}</button>
72 <button name="method" value="hide" class="not_fav">{% trans "Hide" %}</button>
73 {% endif %}
73 {% endif %}
74 </form>
74 </form>
75 &bull;
75 &bull;
76 <a href="{% url 'tag_gallery' tag.get_name %}">{% trans 'Gallery' %}</a>
76 <a href="{% url 'tag_gallery' tag.get_name %}">{% trans 'Gallery' %}</a>
77 </p>
77 </p>
78 {% if tag.get_description %}
78 {% if tag.get_description %}
79 <p>{{ tag.get_description|safe }}</p>
79 <p>{{ tag.get_description|safe }}</p>
80 {% endif %}
80 {% endif %}
81 <p>
81 <p>
82 {% with active_count=tag.get_active_thread_count bumplimit_count=tag.get_bumplimit_thread_count archived_count=tag.get_archived_thread_count %}
82 {% with active_count=tag.get_active_thread_count bumplimit_count=tag.get_bumplimit_thread_count archived_count=tag.get_archived_thread_count %}
83 {% if active_count %}
83 {% if active_count %}
84 ● {{ active_count }}&ensp;
84 ● {{ active_count }}&ensp;
85 {% endif %}
85 {% endif %}
86 {% if bumplimit_count %}
86 {% if bumplimit_count %}
87 ◍ {{ bumplimit_count }}&ensp;
87 ◍ {{ bumplimit_count }}&ensp;
88 {% endif %}
88 {% endif %}
89 {% if archived_count %}
89 {% if archived_count %}
90 β—‹ {{ archived_count }}&ensp;
90 β—‹ {{ archived_count }}&ensp;
91 {% endif %}
91 {% endif %}
92 {% endwith %}
92 {% endwith %}
93 β™₯ {{ tag.get_post_count }}
93 β™₯ {{ tag.get_post_count }}
94 </p>
94 </p>
95 {% if tag.get_all_parents %}
95 {% if tag.get_all_parents %}
96 <p>
96 <p>
97 {% for parent in tag.get_all_parents %}
97 {% for parent in tag.get_all_parents %}
98 {{ parent.get_view|safe }} &gt;
98 {{ parent.get_view|safe }} &gt;
99 {% endfor %}
99 {% endfor %}
100 {{ tag.get_view|safe }}
100 {{ tag.get_view|safe }}
101 </p>
101 </p>
102 {% endif %}
102 {% endif %}
103 {% if tag.get_children.all %}
103 {% if tag.get_children.all %}
104 <p>
104 <p>
105 {% trans "Subsections: " %}
105 {% trans "Subsections: " %}
106 {% for child in tag.get_children.all %}
106 {% for child in tag.get_children.all %}
107 {{ child.get_view|safe }}{% if not forloop.last%}, {% endif %}
107 {{ child.get_view|safe }}{% if not forloop.last%}, {% endif %}
108 {% endfor %}
108 {% endfor %}
109 </p>
109 </p>
110 {% endif %}
110 {% endif %}
111 </div>
111 </div>
112 </div>
112 </div>
113 {% endif %}
113 {% endif %}
114
114
115 {% if threads %}
115 {% if threads %}
116 {% if prev_page_link %}
116 {% if prev_page_link %}
117 <div class="page_link">
117 <div class="page_link">
118 <a href="{{ prev_page_link }}">&lt;&lt; {% trans "Previous page" %} &lt;&lt;</a>
118 <a href="{{ prev_page_link }}">&lt;&lt; {% trans "Previous page" %} &lt;&lt;</a>
119 </div>
119 </div>
120 {% endif %}
120 {% endif %}
121
121
122 {% for thread in threads %}
122 {% for thread in threads %}
123 <div class="thread">
123 <div class="thread">
124 {% post_view thread.get_opening_post thread=thread truncated=True need_open_link=True %}
124 {% post_view thread.get_opening_post thread=thread truncated=True need_open_link=True %}
125 {% if not thread.archived %}
125 {% if not thread.archived %}
126 {% with last_replies=thread.get_last_replies %}
126 {% with last_replies=thread.get_last_replies %}
127 {% if last_replies %}
127 {% if last_replies %}
128 {% with skipped_replies_count=thread.get_skipped_replies_count %}
128 {% with skipped_replies_count=thread.get_skipped_replies_count %}
129 {% if skipped_replies_count %}
129 {% if skipped_replies_count %}
130 <div class="skipped_replies">
130 <div class="skipped_replies">
131 <a href="{% url 'thread' thread.get_opening_post_id %}">
131 <a href="{% url 'thread' thread.get_opening_post_id %}">
132 {% blocktrans count count=skipped_replies_count %}Skipped {{ count }} reply. Open thread to see all replies.{% plural %}Skipped {{ count }} replies. Open thread to see all replies.{% endblocktrans %}
132 {% blocktrans count count=skipped_replies_count %}Skipped {{ count }} reply. Open thread to see all replies.{% plural %}Skipped {{ count }} replies. Open thread to see all replies.{% endblocktrans %}
133 </a>
133 </a>
134 </div>
134 </div>
135 {% endif %}
135 {% endif %}
136 {% endwith %}
136 {% endwith %}
137 <div class="last-replies">
137 <div class="last-replies">
138 {% for post in last_replies %}
138 {% for post in last_replies %}
139 {% post_view post truncated=True %}
139 {% post_view post truncated=True %}
140 {% endfor %}
140 {% endfor %}
141 </div>
141 </div>
142 {% endif %}
142 {% endif %}
143 {% endwith %}
143 {% endwith %}
144 {% endif %}
144 {% endif %}
145 </div>
145 </div>
146 {% endfor %}
146 {% endfor %}
147
147
148 {% if next_page_link %}
148 {% if next_page_link %}
149 <div class="page_link">
149 <div class="page_link">
150 <a href="{{ next_page_link }}">&gt;&gt; {% trans "Next page" %} &gt;&gt;</a>
150 <a href="{{ next_page_link }}">&gt;&gt; {% trans "Next page" %} &gt;&gt;</a>
151 </div>
151 </div>
152 {% endif %}
152 {% endif %}
153 {% else %}
153 {% else %}
154 <div class="post">
154 <div class="post">
155 {% trans 'No threads exist. Create the first one!' %}</div>
155 {% trans 'No threads exist. Create the first one!' %}</div>
156 {% endif %}
156 {% endif %}
157
157
158 {% form_view form=form new_thread=True %}
158 {% form_view form=form new_thread=True %}
159
159
160 <script src="{% static 'js/thread_create.js' %}"></script>
160 <script src="{% static 'js/thread_create.js' %}"></script>
161
161
162 {% endblock %}
162 {% endblock %}
163
164 {% block metapanel %}
165
166 <span class="metapanel">
167 {% trans "Pages:" %}
168 [
169 {% with dividers=paginator.get_dividers %}
170 {% for page in paginator.get_divided_range %}
171 {% if page in dividers %}
172 …,
173 {% endif %}
174 <a
175 {% ifequal page current_page.number %}
176 class="current_page"
177 {% endifequal %}
178 href="{% page_url paginator page %}">{{ page }}</a>{% if not forloop.last %},{% endif %}
179 {% endfor %}
180 {% endwith %}
181 ]
182 </span>
183
184 {% endblock %}
@@ -1,35 +1,35 b''
1 {% extends 'boards/base.html' %}
1 {% extends 'boards/paginated.html' %}
2
2
3 {% load board %}
3 {% load board %}
4 {% load i18n %}
4 {% load i18n %}
5
5
6 {% block head %}
6 {% block head %}
7 <meta name="robots" content="noindex">
7 <meta name="robots" content="noindex">
8 <title>{{ site_name }} - {% trans 'Notifications' %} - {{ notification_usernames|join:', ' }}</title>
8 <title>{{ site_name }} - {% trans 'Notifications' %} - {{ notification_usernames|join:', ' }}</title>
9 {% endblock %}
9 {% endblock %}
10
10
11 {% block content %}
11 {% block content %}
12 <div class="tag_info">
12 <div class="tag_info">
13 {% for username in notification_usernames %}
13 {% for username in notification_usernames %}
14 <a href="{% url 'notifications' username %}" class="user-cast">@{{ username }}</a>
14 <a href="{% url 'notifications' username %}" class="user-cast">@{{ username }}</a>
15 {% endfor %}
15 {% endfor %}
16 </div>
16 </div>
17
17
18 {% if page %}
18 {% if page %}
19 {% if page.has_previous %}
19 {% if page.has_previous %}
20 <div class="page_link">
20 <div class="page_link">
21 <a href="?page={{ page.previous_page_number }}">{% trans "Previous page" %}</a>
21 <a href="?page={{ page.previous_page_number }}">{% trans "Previous page" %}</a>
22 </div>
22 </div>
23 {% endif %}
23 {% endif %}
24
24
25 {% for post in page.object_list %}
25 {% for post in page.object_list %}
26 {% post_view post need_op_data=True %}
26 {% post_view post need_op_data=True %}
27 {% endfor %}
27 {% endfor %}
28
28
29 {% if page.has_next %}
29 {% if page.has_next %}
30 <div class="page_link">
30 <div class="page_link">
31 <a href="?page={{ page.next_page_number }}">{% trans "Next page" %}</a>
31 <a href="?page={{ page.next_page_number }}">{% trans "Next page" %}</a>
32 </div>
32 </div>
33 {% endif %}
33 {% endif %}
34 {% endif %}
34 {% endif %}
35 {% endblock %}
35 {% endblock %}
@@ -1,26 +1,12 b''
1 {% extends 'boards/base.html' %}
1 {% extends 'boards/base.html' %}
2
2
3 {% load i18n %}
3 {% load i18n %}
4 {% load board %}
4 {% load board %}
5
5
6 {% block metapanel %}
6 {% block metapanel %}
7
7
8 <span class="metapanel">
8 <span class="metapanel">
9 {% trans "Pages:" %}
9 {% trans "Pages:" %} [{{ paginator.get_page_url_list|safe }}]
10 [
11 {% with dividers=paginator.get_dividers %}
12 {% for page in paginator.get_divided_range %}
13 {% if page in dividers %}
14 …,
15 {% endif %}
16 <a
17 {% ifequal page current_page.number %}
18 class="current_page"
19 {% endifequal %}
20 href="{% page_url paginator page %}">{{ page }}</a>{% if not forloop.last %},{% endif %}
21 {% endfor %}
22 {% endwith %}
23 ]
24 </span>
10 </span>
25
11
26 {% endblock %}
12 {% endblock %}
@@ -1,114 +1,90 b''
1 {% extends "boards/base.html" %}
1 {% extends "boards/paginated.html" %}
2
2
3 {% load i18n %}
3 {% load i18n %}
4 {% load board %}
4 {% load board %}
5 {% load static %}
5 {% load static %}
6 {% load tz %}
6 {% load tz %}
7
7
8 {% block head %}
8 {% block head %}
9 <meta name="robots" content="noindex">
9 <meta name="robots" content="noindex">
10
10
11 <title>{{ tag.name }} - {% trans 'Gallery' %} - {{ site_name }}</title>
11 <title>{{ tag.name }} - {% trans 'Gallery' %} - {{ site_name }}</title>
12
12
13 {% if prev_page_link %}
13 {% if prev_page_link %}
14 <link rel="prev" href="{{ prev_page_link }}" />
14 <link rel="prev" href="{{ prev_page_link }}" />
15 {% endif %}
15 {% endif %}
16 {% if next_page_link %}
16 {% if next_page_link %}
17 <link rel="next" href="{{ next_page_link }}" />
17 <link rel="next" href="{{ next_page_link }}" />
18 {% endif %}
18 {% endif %}
19
19
20 {% endblock %}
20 {% endblock %}
21
21
22 {% block content %}
22 {% block content %}
23
23
24 {% get_current_language as LANGUAGE_CODE %}
24 {% get_current_language as LANGUAGE_CODE %}
25 {% get_current_timezone as TIME_ZONE %}
25 {% get_current_timezone as TIME_ZONE %}
26
26
27 {% for banner in banners %}
27 {% for banner in banners %}
28 <div class="post">
28 <div class="post">
29 <div class="title">{{ banner.title }}</div>
29 <div class="title">{{ banner.title }}</div>
30 <div>{{ banner.get_text }}</div>
30 <div>{{ banner.get_text }}</div>
31 <div>{% trans 'Details' %}: <a href="{{ banner.post.get_absolute_url }}">>>{{ banner.post.id }}</a></div>
31 <div>{% trans 'Details' %}: <a href="{{ banner.post.get_absolute_url }}">>>{{ banner.post.id }}</a></div>
32 </div>
32 </div>
33 {% endfor %}
33 {% endfor %}
34
34
35 <div class="tag_info" style="border-bottom: solid .5ex #{{ tag.get_color }}">
35 <div class="tag_info" style="border-bottom: solid .5ex #{{ tag.get_color }}">
36 {% if random_image_post %}
36 {% if random_image_post %}
37 <div class="tag-image">
37 <div class="tag-image">
38 {% with image=random_image_post.get_first_image %}
38 {% with image=random_image_post.get_first_image %}
39 <a href="{{ random_image_post.get_absolute_url }}"><img
39 <a href="{{ random_image_post.get_absolute_url }}"><img
40 src="{{ image.get_thumb_url }}"
40 src="{{ image.get_thumb_url }}"
41 width="{{ image.get_preview_size.0 }}"
41 width="{{ image.get_preview_size.0 }}"
42 height="{{ image.get_preview_size.1 }}"
42 height="{{ image.get_preview_size.1 }}"
43 alt="{{ random_image_post.id }}"/></a>
43 alt="{{ random_image_post.id }}"/></a>
44 {% endwith %}
44 {% endwith %}
45 </div>
45 </div>
46 {% endif %}
46 {% endif %}
47 <div class="tag-text-data">
47 <div class="tag-text-data">
48 <h2>
48 <h2>
49 /{{ tag.get_view|safe }}/
49 /{{ tag.get_view|safe }}/
50 {% if perms.change_tag %}
50 {% if perms.change_tag %}
51 <span class="moderator_info">| <a href="{% url 'admin:boards_tag_change' tag.id %}">{% trans 'Edit tag' %}</a></span>
51 <span class="moderator_info">| <a href="{% url 'admin:boards_tag_change' tag.id %}">{% trans 'Edit tag' %}</a></span>
52 {% endif %}
52 {% endif %}
53 </h2>
53 </h2>
54 {% if tag.get_description %}
54 {% if tag.get_description %}
55 <p>{{ tag.get_description|safe }}</p>
55 <p>{{ tag.get_description|safe }}</p>
56 {% endif %}
56 {% endif %}
57 {% if tag.get_all_parents %}
57 {% if tag.get_all_parents %}
58 <p>
58 <p>
59 {% for parent in tag.get_all_parents %}
59 {% for parent in tag.get_all_parents %}
60 {{ parent.get_view|safe }} &gt;
60 {{ parent.get_view|safe }} &gt;
61 {% endfor %}
61 {% endfor %}
62 {{ tag.get_view|safe }}
62 {{ tag.get_view|safe }}
63 </p>
63 </p>
64 {% endif %}
64 {% endif %}
65 </div>
65 </div>
66 </div>
66 </div>
67
67
68 {% if prev_page_link %}
68 {% if prev_page_link %}
69 <div class="page_link">
69 <div class="page_link">
70 <a href="{{ prev_page_link }}">{% trans "Previous page" %}</a>
70 <a href="{{ prev_page_link }}">{% trans "Previous page" %}</a>
71 </div>
71 </div>
72 {% endif %}
72 {% endif %}
73
73
74 <div id="posts-table">
74 <div id="posts-table">
75 {% for image in images %}
75 {% for image in images %}
76 <div class="gallery_image">
76 <div class="gallery_image">
77 {{ image.get_view|safe }}
77 {{ image.get_view|safe }}
78 <div class="gallery_image_metadata">
78 <div class="gallery_image_metadata">
79 {{ image.get_size.0 }}x{{ image.get_size.1 }}
79 {{ image.get_size.0 }}x{{ image.get_size.1 }}
80 </div>
80 </div>
81 </div>
81 </div>
82 {% endfor %}
82 {% endfor %}
83 </div>
83 </div>
84
84
85 {% if next_page_link %}
85 {% if next_page_link %}
86 <div class="page_link">
86 <div class="page_link">
87 <a href="{{ next_page_link }}">{% trans "Next page" %}</a>
87 <a href="{{ next_page_link }}">{% trans "Next page" %}</a>
88 </div>
88 </div>
89 {% endif %}
89 {% endif %}
90 {% endblock %}
90 {% endblock %}
91
92 {% block metapanel %}
93
94 <span class="metapanel">
95 <b><a href="{% url "authors" %}">{{ site_name }}</a> {{ version }}</b>
96 {% trans "Pages:" %}
97 [
98 {% with dividers=paginator.get_dividers %}
99 {% for page in paginator.get_divided_range %}
100 {% if page in dividers %}
101 …,
102 {% endif %}
103 <a
104 {% ifequal page paginator.current_page %}
105 class="current_page"
106 {% endifequal %}
107 href="{% page_url paginator page %}">{{ page }}</a>
108 {% if not forloop.last %},{% endif %}
109 {% endfor %}
110 {% endwith %}
111 ]
112 </span>
113
114 {% endblock %}
@@ -1,132 +1,133 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, redirect
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 link=self.get_reverse_url(),
75 params=request.GET.dict())
74 paginator.current_page = int(page)
76 paginator.current_page = int(page)
75
77
76 try:
78 try:
77 threads = paginator.page(page).object_list
79 threads = paginator.page(page).object_list
78 except EmptyPage:
80 except EmptyPage:
79 raise Http404()
81 raise Http404()
80
82
81 params[PARAMETER_THREADS] = threads
83 params[PARAMETER_THREADS] = threads
82 params[CONTEXT_FORM] = form
84 params[CONTEXT_FORM] = form
83 params[PARAMETER_RSS_URL] = self.get_rss_url()
85 params[PARAMETER_RSS_URL] = self.get_rss_url()
84
86
85 paginator.set_url(self.get_reverse_url(), request.GET.dict())
86 params.update(self.get_page_context(paginator, page))
87 params.update(self.get_page_context(paginator, page))
87
88
88 return render(request, TEMPLATE, params)
89 return render(request, TEMPLATE, params)
89
90
90 @method_decorator(csrf_protect)
91 @method_decorator(csrf_protect)
91 def post(self, request):
92 def post(self, request):
92 if PARAMETER_METHOD in request.POST:
93 if PARAMETER_METHOD in request.POST:
93 self.dispatch_method(request)
94 self.dispatch_method(request)
94
95
95 return redirect(self.get_reverse_url())
96 return redirect(self.get_reverse_url())
96
97
97 form = ThreadForm(request.POST, request.FILES,
98 form = ThreadForm(request.POST, request.FILES,
98 error_class=PlainErrorList)
99 error_class=PlainErrorList)
99 form.session = request.session
100 form.session = request.session
100
101
101 if form.is_valid():
102 if form.is_valid():
102 return Post.objects.create_from_form(request, form, None)
103 return Post.objects.create_from_form(request, form, None)
103 if form.need_to_ban:
104 if form.need_to_ban:
104 # Ban user because he is suspected to be a bot
105 # Ban user because he is suspected to be a bot
105 self._ban_current_user(request)
106 self._ban_current_user(request)
106
107
107 return self.get(request, form)
108 return self.get(request, form)
108
109
109 def get_reverse_url(self):
110 def get_reverse_url(self):
110 return reverse('index')
111 return reverse('index')
111
112
112 def get_threads(self):
113 def get_threads(self):
113 """
114 """
114 Gets list of threads that will be shown on a page.
115 Gets list of threads that will be shown on a page.
115 """
116 """
116
117
117 threads = Thread.objects\
118 threads = Thread.objects\
118 .exclude(tags__in=self.settings_manager.get_hidden_tags())
119 .exclude(tags__in=self.settings_manager.get_hidden_tags())
119 if self.settings_manager.get_setting(SETTING_ONLY_FAVORITES):
120 if self.settings_manager.get_setting(SETTING_ONLY_FAVORITES):
120 fav_tags = self.settings_manager.get_fav_tags()
121 fav_tags = self.settings_manager.get_fav_tags()
121 if len(fav_tags) > 0:
122 if len(fav_tags) > 0:
122 threads = threads.filter(tags__in=fav_tags)
123 threads = threads.filter(tags__in=fav_tags)
123
124
124 return threads
125 return threads
125
126
126 def get_rss_url(self):
127 def get_rss_url(self):
127 return self.get_reverse_url() + 'rss/'
128 return self.get_reverse_url() + 'rss/'
128
129
129 def toggle_fav(self, request):
130 def toggle_fav(self, request):
130 settings_manager = get_settings_manager(request)
131 settings_manager = get_settings_manager(request)
131 settings_manager.set_setting(SETTING_ONLY_FAVORITES,
132 settings_manager.set_setting(SETTING_ONLY_FAVORITES,
132 not settings_manager.get_setting(SETTING_ONLY_FAVORITES, False))
133 not settings_manager.get_setting(SETTING_ONLY_FAVORITES, False))
@@ -1,132 +1,132 b''
1 from django.urls import reverse
1 from django.urls import reverse
2 from django.shortcuts import render
2 from django.shortcuts import render
3
3
4 from boards import settings
4 from boards import settings
5 from boards.abstracts.constants import PARAM_PAGE
5 from boards.abstracts.constants import PARAM_PAGE
6 from boards.abstracts.paginator import get_paginator
6 from boards.abstracts.paginator import get_paginator
7 from boards.abstracts.settingsmanager import get_settings_manager
7 from boards.abstracts.settingsmanager import get_settings_manager
8 from boards.models import Post
8 from boards.models import Post
9 from boards.settings import SECTION_VIEW
9 from boards.settings import SECTION_VIEW
10 from boards.views.base import BaseBoardView
10 from boards.views.base import BaseBoardView
11 from boards.views.mixins import PaginatedMixin
11 from boards.views.mixins import PaginatedMixin
12
12
13 POSTS_PER_PAGE = settings.get_int(SECTION_VIEW, 'PostsPerPage')
13 POSTS_PER_PAGE = settings.get_int(SECTION_VIEW, 'PostsPerPage')
14
14
15 PARAMETER_POSTS = 'posts'
15 PARAMETER_POSTS = 'posts'
16 PARAMETER_QUERIES = 'queries'
16 PARAMETER_QUERIES = 'queries'
17
17
18 TEMPLATE = 'boards/feed.html'
18 TEMPLATE = 'boards/feed.html'
19 DEFAULT_PAGE = 1
19 DEFAULT_PAGE = 1
20
20
21
21
22 class FeedFilter:
22 class FeedFilter:
23 @staticmethod
23 @staticmethod
24 def get_filtered_posts(request, posts):
24 def get_filtered_posts(request, posts):
25 return posts
25 return posts
26
26
27 @staticmethod
27 @staticmethod
28 def get_query(request):
28 def get_query(request):
29 return None
29 return None
30
30
31
31
32 class TripcodeFilter(FeedFilter):
32 class TripcodeFilter(FeedFilter):
33 @staticmethod
33 @staticmethod
34 def get_filtered_posts(request, posts):
34 def get_filtered_posts(request, posts):
35 filtered_posts = posts
35 filtered_posts = posts
36 tripcode = request.GET.get('tripcode', None)
36 tripcode = request.GET.get('tripcode', None)
37 if tripcode:
37 if tripcode:
38 filtered_posts = filtered_posts.filter(tripcode=tripcode)
38 filtered_posts = filtered_posts.filter(tripcode=tripcode)
39 return filtered_posts
39 return filtered_posts
40
40
41 @staticmethod
41 @staticmethod
42 def get_query(request):
42 def get_query(request):
43 tripcode = request.GET.get('tripcode', None)
43 tripcode = request.GET.get('tripcode', None)
44 if tripcode:
44 if tripcode:
45 return 'Tripcode: {}'.format(tripcode)
45 return 'Tripcode: {}'.format(tripcode)
46
46
47
47
48 class FavoritesFilter(FeedFilter):
48 class FavoritesFilter(FeedFilter):
49 @staticmethod
49 @staticmethod
50 def get_filtered_posts(request, posts):
50 def get_filtered_posts(request, posts):
51 filtered_posts = posts
51 filtered_posts = posts
52
52
53 favorites = 'favorites' in request.GET
53 favorites = 'favorites' in request.GET
54 if favorites:
54 if favorites:
55 settings_manager = get_settings_manager(request)
55 settings_manager = get_settings_manager(request)
56 last_posts = settings_manager.get_last_posts()
56 last_posts = settings_manager.get_last_posts()
57 threads = [post.get_thread() for post in last_posts]
57 threads = [post.get_thread() for post in last_posts]
58 filtered_posts = filtered_posts.filter(thread__in=threads)
58 filtered_posts = filtered_posts.filter(thread__in=threads)
59 return filtered_posts
59 return filtered_posts
60
60
61
61
62 class IpFilter(FeedFilter):
62 class IpFilter(FeedFilter):
63 @staticmethod
63 @staticmethod
64 def get_filtered_posts(request, posts):
64 def get_filtered_posts(request, posts):
65 filtered_posts = posts
65 filtered_posts = posts
66
66
67 ip = request.GET.get('ip', None)
67 ip = request.GET.get('ip', None)
68 if ip and request.user.has_perm('post_delete'):
68 if ip and request.user.has_perm('post_delete'):
69 filtered_posts = filtered_posts.filter(poster_ip=ip)
69 filtered_posts = filtered_posts.filter(poster_ip=ip)
70 return filtered_posts
70 return filtered_posts
71
71
72 @staticmethod
72 @staticmethod
73 def get_query(request):
73 def get_query(request):
74 ip = request.GET.get('ip', None)
74 ip = request.GET.get('ip', None)
75 if ip:
75 if ip:
76 return 'IP: {}'.format(ip)
76 return 'IP: {}'.format(ip)
77
77
78
78
79 class ImageFilter(FeedFilter):
79 class ImageFilter(FeedFilter):
80 @staticmethod
80 @staticmethod
81 def get_filtered_posts(request, posts):
81 def get_filtered_posts(request, posts):
82 filtered_posts = posts
82 filtered_posts = posts
83
83
84 image = request.GET.get('image', None)
84 image = request.GET.get('image', None)
85 if image:
85 if image:
86 filtered_posts = filtered_posts.filter(attachments__file=image)
86 filtered_posts = filtered_posts.filter(attachments__file=image)
87 return filtered_posts
87 return filtered_posts
88
88
89 @staticmethod
89 @staticmethod
90 def get_query(request):
90 def get_query(request):
91 image = request.GET.get('image', None)
91 image = request.GET.get('image', None)
92 if image:
92 if image:
93 return 'File: {}'.format(image)
93 return 'File: {}'.format(image)
94
94
95
95
96 class FeedView(PaginatedMixin, BaseBoardView):
96 class FeedView(PaginatedMixin, BaseBoardView):
97 filters = (
97 filters = (
98 TripcodeFilter,
98 TripcodeFilter,
99 FavoritesFilter,
99 FavoritesFilter,
100 IpFilter,
100 IpFilter,
101 ImageFilter,
101 ImageFilter,
102 )
102 )
103
103
104 def get(self, request):
104 def get(self, request):
105 page = request.GET.get(PARAM_PAGE, DEFAULT_PAGE)
105 page = request.GET.get(PARAM_PAGE, DEFAULT_PAGE)
106
106
107 params = self.get_context_data(request=request)
107 params = self.get_context_data(request=request)
108
108
109 settings_manager = get_settings_manager(request)
109 settings_manager = get_settings_manager(request)
110
110
111 posts = Post.objects.exclude(
111 posts = Post.objects.exclude(
112 thread__tags__in=settings_manager.get_hidden_tags()).order_by(
112 thread__tags__in=settings_manager.get_hidden_tags()).order_by(
113 '-pub_time').prefetch_related('attachments', 'thread')
113 '-pub_time').prefetch_related('attachments', 'thread')
114 queries = []
114 queries = []
115 for filter in self.filters:
115 for filter in self.filters:
116 posts = filter.get_filtered_posts(request, posts)
116 posts = filter.get_filtered_posts(request, posts)
117 query = filter.get_query(request)
117 query = filter.get_query(request)
118 if query:
118 if query:
119 queries.append(query)
119 queries.append(query)
120 params[PARAMETER_QUERIES] = queries
120 params[PARAMETER_QUERIES] = queries
121
121
122 paginator = get_paginator(posts, POSTS_PER_PAGE)
122 paginator = get_paginator(posts, POSTS_PER_PAGE,
123 link=reverse('feed'),
124 params=request.GET.dict())
123 paginator.current_page = int(page)
125 paginator.current_page = int(page)
124
126
125 params[PARAMETER_POSTS] = paginator.page(page).object_list
127 params[PARAMETER_POSTS] = paginator.page(page).object_list
126
128
127 paginator.set_url(reverse('feed'), request.GET.dict())
128
129 params.update(self.get_page_context(paginator, page))
129 params.update(self.get_page_context(paginator, page))
130
130
131 return render(request, TEMPLATE, params)
131 return render(request, TEMPLATE, params)
132
132
@@ -1,48 +1,51 b''
1 from django.shortcuts import render
1 from django.shortcuts import render
2 from django.urls import reverse
2
3
3 from boards.abstracts.constants import PARAM_PAGE
4 from boards.abstracts.constants import PARAM_PAGE
4 from boards.abstracts.paginator import get_paginator
5 from boards.abstracts.paginator import get_paginator
5 from boards.abstracts.settingsmanager import get_settings_manager, \
6 from boards.abstracts.settingsmanager import get_settings_manager, \
6 SETTING_LAST_NOTIFICATION_ID
7 SETTING_LAST_NOTIFICATION_ID
7 from boards.models.user import Notification
8 from boards.models.user import Notification
8 from boards.views.base import BaseBoardView
9 from boards.views.base import BaseBoardView
10 from boards.views.mixins import PaginatedMixin
9
11
10 DEFAULT_PAGE = '1'
12 DEFAULT_PAGE = '1'
11
13
12 TEMPLATE = 'boards/notifications.html'
14 TEMPLATE = 'boards/notifications.html'
13 PARAM_USERNAMES = 'notification_usernames'
15 PARAM_USERNAMES = 'notification_usernames'
14 RESULTS_PER_PAGE = 10
16 RESULTS_PER_PAGE = 10
15
17
16
18
17 class NotificationView(BaseBoardView):
19 class NotificationView(BaseBoardView, PaginatedMixin):
18
20
19 def get(self, request, username=None):
21 def get(self, request, username=None):
20 params = self.get_context_data()
22 params = self.get_context_data()
21
23
22 settings_manager = get_settings_manager(request)
24 settings_manager = get_settings_manager(request)
23
25
24 # If we open our notifications, reset the "new" count
26 # If we open our notifications, reset the "new" count
25 if username is None:
27 if username is None:
26 notification_usernames = settings_manager.get_notification_usernames()
28 notification_usernames = settings_manager.get_notification_usernames()
27 else:
29 else:
28 notification_usernames = [username]
30 notification_usernames = [username]
29
31
30 posts = Notification.objects.get_notification_posts(
32 posts = Notification.objects.get_notification_posts(
31 usernames=notification_usernames, user_settings=settings_manager.get_user_settings())
33 usernames=notification_usernames, user_settings=settings_manager.get_user_settings())
32
34
33 if username is None:
35 if username is None:
34 last = posts.first()
36 last = posts.first()
35 if last is not None:
37 if last is not None:
36 last_id = last.id
38 last_id = last.id
37 settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID,
39 settings_manager.set_setting(SETTING_LAST_NOTIFICATION_ID,
38 last_id)
40 last_id)
39
41
40
41 paginator = get_paginator(posts, RESULTS_PER_PAGE)
42
43 page = int(request.GET.get(PARAM_PAGE, DEFAULT_PAGE))
42 page = int(request.GET.get(PARAM_PAGE, DEFAULT_PAGE))
43 paginator = get_paginator(posts, RESULTS_PER_PAGE,
44 link=reverse('notifications'),
45 params=request.GET.dict())
44
46
45 params[PARAM_PAGE] = paginator.page(page)
47 params[PARAM_PAGE] = paginator.page(page)
46 params[PARAM_USERNAMES] = notification_usernames
48 params[PARAM_USERNAMES] = notification_usernames
49 params.update(self.get_page_context(paginator, page))
47
50
48 return render(request, TEMPLATE, params)
51 return render(request, TEMPLATE, params)
@@ -1,54 +1,55 b''
1 from django.shortcuts import render
1 from django.shortcuts import render
2 from django.views.generic import View
2 from django.views.generic import View
3 from django.db.models import Q
3 from django.db.models import Q
4 from django.urls import reverse
4 from django.urls import reverse
5
5
6 from boards.abstracts.paginator import get_paginator
6 from boards.abstracts.paginator import get_paginator
7 from boards.forms import SearchForm, PlainErrorList
7 from boards.forms import SearchForm, PlainErrorList
8 from boards.models import Post, Tag
8 from boards.models import Post, Tag
9 from boards.views.mixins import PaginatedMixin
9 from boards.views.mixins import PaginatedMixin
10
10
11
11
12 MIN_QUERY_LENGTH = 3
12 MIN_QUERY_LENGTH = 3
13 RESULTS_PER_PAGE = 10
13 RESULTS_PER_PAGE = 10
14
14
15 FORM_QUERY = 'query'
15 FORM_QUERY = 'query'
16
16
17 CONTEXT_QUERY = 'query'
17 CONTEXT_QUERY = 'query'
18 CONTEXT_FORM = 'form'
18 CONTEXT_FORM = 'form'
19 CONTEXT_PAGE = 'page'
19 CONTEXT_PAGE = 'page'
20 CONTEXT_TAGS = 'tags'
20 CONTEXT_TAGS = 'tags'
21
21
22 REQUEST_PAGE = 'page'
22 REQUEST_PAGE = 'page'
23
23
24 __author__ = 'neko259'
24 __author__ = 'neko259'
25
25
26 TEMPLATE = 'search/search.html'
26 TEMPLATE = 'search/search.html'
27
27
28
28
29 class BoardSearchView(View, PaginatedMixin):
29 class BoardSearchView(View, PaginatedMixin):
30 def get(self, request):
30 def get(self, request):
31 params = dict()
31 params = dict()
32
32
33 form = SearchForm(request.GET, error_class=PlainErrorList)
33 form = SearchForm(request.GET, error_class=PlainErrorList)
34 params[CONTEXT_FORM] = form
34 params[CONTEXT_FORM] = form
35
35
36 if form.is_valid():
36 if form.is_valid():
37 query = form.cleaned_data[FORM_QUERY]
37 query = form.cleaned_data[FORM_QUERY]
38 if len(query) >= MIN_QUERY_LENGTH:
38 if len(query) >= MIN_QUERY_LENGTH:
39 results = Post.objects.filter(Q(text__icontains=query)
39 results = Post.objects.filter(Q(text__icontains=query)
40 | Q(title__icontains=query) | Q(opening=True,
40 | Q(title__icontains=query) | Q(opening=True,
41 thread__tags__aliases__name__icontains=query)
41 thread__tags__aliases__name__icontains=query)
42 | Q(attachments__url__icontains=query)).order_by('-id').distinct()
42 | Q(attachments__url__icontains=query)).order_by('-id').distinct()
43 paginator = get_paginator(results, RESULTS_PER_PAGE)
43 paginator = get_paginator(results, RESULTS_PER_PAGE,
44 paginator.set_url(reverse('search'), request.GET.dict())
44 link=reverse('search'),
45 params=request.GET.dict())
45
46
46 page = int(request.GET.get(REQUEST_PAGE, '1'))
47 page = int(request.GET.get(REQUEST_PAGE, '1'))
47
48
48 params[CONTEXT_QUERY] = query
49 params[CONTEXT_QUERY] = query
49 params.update(self.get_page_context(paginator, page))
50 params.update(self.get_page_context(paginator, page))
50
51
51 tags = Tag.objects.get_tag_url_list(Tag.objects.filter(aliases__name__icontains=query))
52 tags = Tag.objects.get_tag_url_list(Tag.objects.filter(aliases__name__icontains=query))
52 params[CONTEXT_TAGS] = tags
53 params[CONTEXT_TAGS] = tags
53
54
54 return render(request, TEMPLATE, params)
55 return render(request, TEMPLATE, params)
@@ -1,34 +1,36 b''
1 from django.shortcuts import get_object_or_404, render
1 from django.shortcuts import get_object_or_404, render
2 from django.urls import reverse
2 from django.urls import reverse
3
3
4 from boards import settings
4 from boards import settings
5 from boards.abstracts.constants import PARAM_PAGE
5 from boards.abstracts.constants import PARAM_PAGE
6 from boards.abstracts.paginator import get_paginator
6 from boards.abstracts.paginator import get_paginator
7 from boards.models import TagAlias
7 from boards.models import TagAlias
8 from boards.settings import SECTION_VIEW
8 from boards.settings import SECTION_VIEW
9 from boards.views.base import BaseBoardView
9 from boards.views.base import BaseBoardView
10 from boards.views.mixins import PaginatedMixin
10 from boards.views.mixins import PaginatedMixin
11
11
12 IMAGES_PER_PAGE = settings.get_int(SECTION_VIEW, 'ImagesPerPageGallery')
12 IMAGES_PER_PAGE = settings.get_int(SECTION_VIEW, 'ImagesPerPageGallery')
13
13
14 TEMPLATE = 'boards/tag_gallery.html'
14 TEMPLATE = 'boards/tag_gallery.html'
15
15
16
16
17 class TagGalleryView(BaseBoardView, PaginatedMixin):
17 class TagGalleryView(BaseBoardView, PaginatedMixin):
18
18
19 def get(self, request, tag_name):
19 def get(self, request, tag_name):
20 page = int(request.GET.get(PARAM_PAGE, 1))
20 page = int(request.GET.get(PARAM_PAGE, 1))
21
21
22 params = dict()
22 params = dict()
23 tag_alias = get_object_or_404(TagAlias, name=tag_name)
23 tag_alias = get_object_or_404(TagAlias, name=tag_name)
24 tag = tag_alias.parent
24 tag = tag_alias.parent
25 params['tag'] = tag
25 params['tag'] = tag
26 paginator = get_paginator(tag.get_images(), IMAGES_PER_PAGE,
26 paginator = get_paginator(
27 current_page=page)
27 tag.get_images(),
28 IMAGES_PER_PAGE,
29 current_page=page,
30 link=reverse('tag_gallery', kwargs={'tag_name': tag_name}),
31 params=request.GET.dict())
28 params['paginator'] = paginator
32 params['paginator'] = paginator
29 params['images'] = paginator.page(page).object_list
33 params['images'] = paginator.page(page).object_list
30 paginator.set_url(reverse('tag_gallery', kwargs={'tag_name': tag_name}),
31 request.GET.dict())
32 params.update(self.get_page_context(paginator, page))
34 params.update(self.get_page_context(paginator, page))
33
35
34 return render(request, TEMPLATE, params)
36 return render(request, TEMPLATE, params)
General Comments 0
You need to be logged in to leave comments. Login now