| 1 |
# Copyright (C) 2016-2023 by the Free Software Foundation, Inc. |
| 2 |
# |
| 3 |
# This file is part of Django-Mailman. |
| 4 |
# |
| 5 |
# Django-Mailman is free software: you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU Lesser General Public License as published by the |
| 7 |
# Free Software Foundation, either version 3 of the License, or (at your |
| 8 |
# option) any later version. |
| 9 |
# |
| 10 |
# Django-Mailman is distributed in the hope that it will be useful, but WITHOUT |
| 11 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 13 |
# for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public License |
| 16 |
# along with Django-Mailman. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
from django import template |
| 19 |
from django.utils.html import conditional_escape |
| 20 |
from django.utils.translation import gettext_lazy as _ |
| 21 |
|
| 22 |
|
| 23 |
register = template.Library() |
| 24 |
|
| 25 |
|
| 26 |
@register.simple_tag(takes_context=True) |
| 27 |
def add_to_query_string(context, *args, **kwargs): |
| 28 |
"""Adds or replaces parameters in the query string""" |
| 29 |
qs = context["request"].GET.copy() |
| 30 |
# create a dict from every args couple |
| 31 |
new_qs_elements = dict(zip(args[::2], args[1::2])) |
| 32 |
new_qs_elements.update(kwargs) |
| 33 |
# don't use the .update() method, it appends instead of overwriting. |
| 34 |
for key, value in new_qs_elements.items(): |
| 35 |
qs[key] = value |
| 36 |
return conditional_escape(qs.urlencode()) |
| 37 |
|
| 38 |
|
| 39 |
@register.inclusion_tag('django_mailman3/paginator/pagination.html', |
| 40 |
takes_context=True) |
| 41 |
def paginator(context, page, qsprefix='', bydate=False): |
| 42 |
if bydate: |
| 43 |
label_previous = _("Newer") |
| 44 |
label_next = _("Older") |
| 45 |
else: |
| 46 |
label_previous = _("Previous") |
| 47 |
label_next = _("Next") |
| 48 |
context.update(dict( |
| 49 |
page=page, |
| 50 |
label_previous=label_previous, |
| 51 |
label_next=label_next, |
| 52 |
page_key="{}page".format(qsprefix), |
| 53 |
count_key="{}count".format(qsprefix), |
| 54 |
per_page_options=[10, 25, 50, 100, 200], # move to settings? |
| 55 |
)) |
| 56 |
return context |