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