8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-23 23:43:47 +00:00

Le sort est compatible avec les autres parametres de l'URL ( 'page=...' )

Ajoute un ta qui permet d'insérer des paramètres dans une URL sans
modifier les autres paramètres.
This commit is contained in:
Maël Kervella 2017-10-21 19:31:45 +00:00
parent f88c65c388
commit 95ad603ab5
3 changed files with 111 additions and 7 deletions

View file

@ -0,0 +1,100 @@
# -*- mode: python; coding: utf-8 -*-
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017 Maël Kervella
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Templatetag used to write a URL (specified or current one) and adding
or inserting specific parameters into the query part without deleting
the other parameters.
"""
from django import template
register = template.Library()
@register.simple_tag
def url_insert_param(url="", **kwargs):
"""
Return the URL with some specific parameters inserted into the query
part. If a URL has already some parameters, those requested will be
modified if already exisiting or will be added and the other parameters
will stay unmodified.
**Tag name**::
url_insert_param
**Parameters**:
url (optional)
The URL to use as a base. The parameters will be added to this URL.
If not specified, it will only return the query part of the URL
("?a=foo&b=bar" for example).
Example : "https://example.com/bar?foo=0&thing=abc"
other arguments
Any other key-value argument will be used. The key is considered as
the name of the parameter to insert/modify and the value is the one
used.
Example : q="foo" search="bar" name="johnDoe"
will return as ?<existing_param>&q=foo&search=bar&name=johnDoe
**Usage**::
{% url_insert_param [URL] [param1=val1 [param2=val2 [...]]] %}
**Example**::
{% url_insert_param a=0 b="bar" %}
return "?a=0&b=bar"
{% url_insert_param "url.net/foo.html" a=0 b="bar" %}
return "url.net/foo.html?a=0&b=bar"
{% url_insert_param "url.net/foo.html?c=keep" a=0 b="bar" %}
return "url.net/foo.html?c=keep&a=0&b=bar"
{% url_insert_param "url.net/foo.html?a=del" a=0 b="bar" %}
return "url.net/foo.html?a=0&b=bar"
{% url_insert_param "url.net/foo.html?a=del&c=keep" a=0 b="bar" %}
return "url.net/foo.hmtl?a=0&c=keep&b=bar"
"""
# Get existing parameters in the url
params = {}
if '?' in url:
url, params = url.split('?', maxsplit=1)
params = {
p[:p.find('=')]: p[p.find('=')+1:] for p in params.split('&')
}
# Add the request parameters to the list of parameters
for key, value in kwargs.items():
params[key] = value
# Write the url
url += '?'
for param, value in params.items():
url += str(param) + '=' + str(value) + '&'
# Remove the last '&' (or '?' if no parameters)
return url[:-1]

View file

@ -22,14 +22,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
{% endcomment %}
{% load url_insert_param %}
{% spaceless %}
<div style="display: flex; padding: 0;">
{{ text }} &nbsp;
<div style="display: grid; font-size: 9px; line-height: 1;">
<a role="button" href="?col={{ col }}" title="{{ desc|default:"Tri croissant" }}">
<a role="button" href="{% url_insert_param request.get_full_path col=col order='asc' %}" title="{{ desc|default:"Tri croissant" }}">
<span class="glyphicon glyphicon-triangle-top"></span>
</a>
<a role="button" href="?col={{ col }}&order=desc" title="{{ desc|default:"Tri décroissant" }}">
<a role="button" href="{% url_insert_param request.get_full_path col=col order='desc' %}" title="{{ desc|default:"Tri décroissant" }}">
<span class="glyphicon glyphicon-triangle-bottom"></span>
</a>
</div>

View file

@ -22,20 +22,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
{% endcomment %}
{% load url_insert_param %}
<ul class="pagination nav navbar-nav">
{% if list.has_previous %}
<li><a href="?page=1"> << </a></li>
<li><a href="?page={{ list.previous_page_number }}"> < </a></li>
<li><a href="{% url_insert_param request.get_full_path page=1 %}"> << </a></li>
<li><a href="{% url_insert_param request.get_full_path page=list.previous_page_number %}"> < </a></li>
{% endif %}
{% for page in list.paginator.page_range %}
{% if list.number <= page|add:"3" and list.number >= page|add:"-3" %}
<li class="{% if list.number == page %}active{% endif %}"><a href="?page={{page }}">{{ page }}</a></li>
<li class="{% if list.number == page %}active{% endif %}"><a href="{% url_insert_param request.get_full_path page=page %}">{{ page }}</a></li>
{% endif %}
{% endfor %}
{% if list.has_next %}
<li><a href="?page={{ list.next_page_number }}"> > </a></li>
<li><a href="?page={{ list.paginator.page_range|length }}"> >> </a></li>
<li><a href="{% url_insert_param request.get_full_path page=list.next_page_number %}"> > </a></li>
<li><a href="{% url_insert_param request.get_full_path page=list.paginator.page_range|length %}"> >> </a></li>
{% endif %}
</ul>