Skip to content

Commit c5b5b65

Browse files
committed
Refactor convert pagination to django-components structure.
1 parent 228c32f commit c5b5b65

26 files changed

+393
-228
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ cython_debug/
198198
# $GIT_DIR/info/exclude or the core.excludesFile configuration variable as
199199
# described in https://git-scm.com/docs/gitignore
200200
*.pyc
201-
src
202201
*DS_Store
203202
*~
204203
*.db

.idea/djangosnippets.org.iml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/watcherTasks.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 138 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

base/components/components.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from django_components import Component, register
2+
from django.utils.html import format_html, format_html_join
3+
from base.pagination import PAGE_VAR, Pagination
4+
from base.templatetags.base_templatetags import querystring
5+
6+
7+
@register("pagination")
8+
class PaginationComponent(Component):
9+
template_file = "pagination.html"
10+
11+
def pagination_number(self, pagination, num):
12+
"""
13+
Generate an individual page index link in a paginated list.
14+
"""
15+
page_link_css = "py-[5px] px-[10px] border-1 border-transparent rounded-lg no-underline hover:border-base-orange-400"
16+
if num == pagination.paginator.ELLIPSIS:
17+
return format_html("{} ", pagination.paginator.ELLIPSIS)
18+
elif num == pagination.page_num:
19+
return format_html(
20+
'<a href="" class="{} bg-base-orange-400 text-base-white-400" aria-current="page">{}</a> ',
21+
page_link_css, num
22+
)
23+
else:
24+
link = querystring(None, {**pagination.params, PAGE_VAR: num})
25+
return format_html(
26+
'<a class="{}" href="{}">{}</a> ',
27+
page_link_css,
28+
link,
29+
num,
30+
)
31+
32+
def get_template_data(self, args, kwargs, slots, context):
33+
pagination = kwargs.get("pagination_obj")
34+
if pagination is None or not isinstance(pagination, Pagination):
35+
raise ValueError("pagination_obj is required and must be an instance of Pagination")
36+
page_elements = format_html_join(
37+
"\n",
38+
'<li class="inline-block">{page_element}</li>',
39+
({"page_element": self.pagination_number(pagination, page_num)} for page_num in pagination.page_range)
40+
)
41+
previous_page_link = f"?{PAGE_VAR}={ pagination.page_num - 1}" if pagination.page.has_previous() else ""
42+
next_page_link = f"?{PAGE_VAR}={pagination.page_num + 1}" if pagination.page.has_next() else ""
43+
44+
return {
45+
"pagination": pagination,
46+
"previous_page_link": previous_page_link,
47+
"next_page_link": next_page_link,
48+
"page_elements": page_elements,
49+
}

base/components/pagination.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{% if pagination.multi_page %}
2+
<nav class="justify-center my-8" aria-labelledby="pagination">
3+
<h2 id="pagination" class="sr-only">Pagination {{ pagination.opts.verbose_name_plural }}</h2>
4+
<ul class="flex justify-center gap-2 transition-colors duration-200 ease-out ml-0">
5+
<li>
6+
<a class="py-[5px] px-[10px] mr-2 no-underline rounded-lg border-1 border-transparent text-base hover:border-base-orange-400 {% if previous_page_link == '' %}text-base-gray-400 hover:border-transparent cursor-default{% endif %}" href="{{ previous_page_link }}" rel="prev" aria-label="Previous page">
7+
<span class="arrow-left inline-flex items-center">Previous</span>
8+
</a>
9+
</li>
10+
{{ page_elements }}
11+
<li>
12+
<a class="py-[5px] px-[10px] ml-2 no-underline rounded-lg border-1 border-transparent text-base hover:border-base-orange-400 {% if next_page_link == '' %}text-base-gray-400 hover:border-transparent cursor-default{% endif %}" href="{{ next_page_link }}" rel="next" aria-label="Next page">
13+
<span class="arrow-right inline-flex items-center">Next</span>
14+
</a>
15+
</li>
16+
</ul>
17+
</nav>
18+
{% endif %}

base/templatetags/components.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)