Projet

Général

Profil

0001-add-generic-pagination-template-14939.patch

Frédéric Péters, 26 décembre 2017 20:53

Télécharger (5,57 ko)

Voir les différences:

Subject: [PATCH] add generic pagination template (#14939)

 README.txt                            |  5 +++
 gadjo/templates/gadjo/pagination.html | 42 ++++++++++++++++++++
 gadjo/templatetags/gadjo.py           | 74 +++++++++++++++++++++++++++++++++++
 3 files changed, 121 insertions(+)
 create mode 100644 gadjo/templates/gadjo/pagination.html
README.txt
80 80
gadjo/static/css/header-03.jpeg is copied from let's encrypt website, licensed
81 81
under Mozilla Public License Version 2.0, see their repository at
82 82
https://github.com/letsencrypt/website.git.
83

  
84
The querystring template tag is originally from django-tables2, copyright (c)
85
2011, Bradley Ayers, copyright (c) 2008, Michael Elsdörfer, published under
86
the MIT license.
87
https://raw.githubusercontent.com/jieter/django-tables2/master/LICENSE
gadjo/templates/gadjo/pagination.html
1
{% load gadjo %}
2
{% comment %}
3
Pagination bar
4

  
5
Expected context variables:
6
 - request: django request object
7
 - page_obj: Paginator page object
8
 - page_key (optional): name of page parameter (default: page)
9

  
10
{% endcomment %}
11
{% if page_obj.paginator.num_pages > 1 %}
12
{% with page_key=page_key|default:"page" %}
13
{% spaceless %}
14
<p class="paginator">
15
  {% if page_obj.number > 1 %}
16
    {% if page_obj.previous_page_number != 1 %}
17
      <a href="{% querystring page_key=1 %}">1</a>
18
19
    {% endif %}
20
  {% endif %}
21

  
22
  {% if page_obj.has_previous %}
23
    <a href="{% querystring page_key=page_obj.previous_page_number %}">{{ page_obj.previous_page_number }}</a>
24
  {% endif %}
25

  
26
  <span class="this-page">{{ page_obj.number }}</span>
27

  
28
  {% if page_obj.has_next %}
29
    <a href="{% querystring page_key=page_obj.next_page_number %}">{{ page_obj.next_page_number }}</a>
30
  {% endif %}
31
  {% if page_obj.number != page_obj.paginator.num_pages %}
32
    {% if page_obj.paginator.num_pages > 1 %}
33
      {% if page_obj.next_page_number != page_obj.paginator.num_pages %}
34
35
        <a href="{% querystring page_key=page_obj.paginator.num_pages %}">{{ page_obj.paginator.num_pages }}</a>
36
      {% endif %}
37
    {% endif %}
38
  {% endif %}
39
</p>
40
{% endspaceless %}
41
{% endwith %}
42
{% endif %}
gadjo/templatetags/gadjo.py
1
from collections import OrderedDict
2
import re
3

  
1 4
from xstatic.main import XStatic
2 5

  
3 6
from django import template
4 7
from django.conf import settings
8
from django.core.exceptions import ImproperlyConfigured
9
from django.utils.html import escape
10
from django.utils.http import urlencode
5 11

  
6 12
register = template.Library()
7 13

  
......
34 40
                    return base_url.get(filename)
35 41

  
36 42
    return settings.STATIC_URL + 'xstatic/' + filename
43

  
44

  
45
# {% querystring %} bits originally from django-tables2.
46
kwarg_re = re.compile(r"(?:(.+)=)?(.+)")
47

  
48
def token_kwargs(bits, parser):
49
    """
50
    Based on Django's `~django.template.defaulttags.token_kwargs`, but with a
51
    few changes:
52

  
53
    - No legacy mode.
54
    - Both keys and values are compiled as a filter
55
    """
56
    if not bits:
57
        return {}
58
    kwargs = OrderedDict()
59
    while bits:
60
        match = kwarg_re.match(bits[0])
61
        if not match or not match.group(1):
62
            return kwargs
63
        key, value = match.groups()
64
        del bits[:1]
65
        kwargs[parser.compile_filter(key)] = parser.compile_filter(value)
66
    return kwargs
67

  
68
class QuerystringNode(template.Node):
69
    def __init__(self, updates, removals):
70
        super(QuerystringNode, self).__init__()
71
        self.updates = updates
72
        self.removals = removals
73

  
74
    def render(self, context):
75
        if not 'request' in context:
76
            raise ImproperlyConfigured('Missing django.core.context_processors.request')
77
        params = dict(context['request'].GET)
78
        for key, value in self.updates.items():
79
            key = key.resolve(context)
80
            value = value.resolve(context)
81
            if key not in ("", None):
82
                params[key] = value
83
        for removal in self.removals:
84
            params.pop(removal.resolve(context), None)
85
        return escape("?" + urlencode(params, doseq=True))
86

  
87
@register.tag
88
def querystring(parser, token):
89
    """
90
    Creates a URL (containing only the querystring [including "?"]) derived
91
    from the current URL's querystring, by updating it with the provided
92
    keyword arguments.
93

  
94
    Example (imagine URL is ``/abc/?gender=male&name=Brad``)::
95

  
96
        {% querystring "name"="Ayers" "age"=20 %}
97
        ?name=Ayers&gender=male&age=20
98
        {% querystring "name"="Ayers" without "gender" %}
99
        ?name=Ayers
100

  
101
    """
102
    bits = token.split_contents()
103
    tag = bits.pop(0)
104
    updates = token_kwargs(bits, parser)
105
    # ``bits`` should now be empty of a=b pairs, it should either be empty, or
106
    # have ``without`` arguments.
107
    if bits and bits.pop(0) != "without":
108
        raise TemplateSyntaxError("Malformed arguments to '%s'" % tag)
109
    removals = [parser.compile_filter(bit) for bit in bits]
110
    return QuerystringNode(updates, removals)
37
-