Commit a72ce9c9 authored by chirac's avatar chirac

Merge branch 'search_results_pagination' into 'dev'

Search results pagination

See merge request !474
parents a7d871e2 83123d9d
Pipeline #2337 passed with stage
in 3 minutes and 30 seconds
...@@ -242,13 +242,13 @@ class SortTable: ...@@ -242,13 +242,13 @@ class SortTable:
return request return request
def re2o_paginator(request, query_set, pagination_number): def re2o_paginator(request, query_set, pagination_number, page_arg="page"):
"""Paginator script for list display in re2o. """Paginator script for list display in re2o.
:request: :request:
:query_set: Query_set to paginate :query_set: Query_set to paginate
:pagination_number: Number of entries to display""" :pagination_number: Number of entries to display"""
paginator = Paginator(query_set, pagination_number) paginator = Paginator(query_set, pagination_number)
page = request.GET.get("page") page = request.GET.get(page_arg)
try: try:
results = paginator.page(page) results = paginator.page(page)
except PageNotAnInteger: except PageNotAnInteger:
......
# -*- 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 © 2019 Jean-Romain Garnier
#
# 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.
from django import template
from .url_insert_param import url_insert_param
register = template.Library()
@register.simple_tag
def pagination_insert_page_and_id(url, page=1, id=None, **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. If parameters with the same name are already in the
URL and a value is specified for this parameter, it will replace all
existing parameters.
**Tag name**::
pagination_insert_page_and_id
**Parameters**:
url
The URL to use as a base. The parameters will be added to this URL.
page (optional)
The page number (greater or equal to 1) to add as a parameter.
If not specified, it will default to 1.
id (optional)
The ID to jump to in the page.
If not specified, it will not be added.
**Other accepted parameters***
page_args (optional)
The name of the parameter used to specify the page number.
It must be specifically set as a keyword.
If not specified, defaults to 1.
Example: {% pagination_insert_page_and_id https://example.com 2 page_args="page_id" %}
returns https://example.com?page_id=2
**Usage**::
{% pagination_insert_page_and_id [URL] [page number] [go to id] %}
**Example**::
{% pagination_insert_page_and_id https://example.com/bar 2 settings %}
return "https://example.com/bar?page=2#settings"
{% pagination_insert_page_and_id https://example.com/bar?foo=0 2 %}
return "https://example.com/bar?foo=0&page=2"
{% pagination_insert_page_and_id https://example.com/bar?foo=0 2 page_arg="page_id" %}
return "https://example.com/bar?foo=0&page_id=2"
"""
page_arg = "page"
if "page_arg" in kwargs and kwargs["page_arg"] is not None and len(kwargs["page_arg"]) > 0:
page_arg = kwargs["page_arg"]
args = { "url": url, page_arg: page}
new_url = url_insert_param(**args)
if id != None:
new_url += "#" + str(id)
return new_url
...@@ -30,40 +30,94 @@ with this program; if not, write to the Free Software Foundation, Inc., ...@@ -30,40 +30,94 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% block content %} {% block content %}
{% if users %} {% if users %}
<div id="users_results">
<h2>{% trans "Results among users:" %}</h2> <h2>{% trans "Results among users:" %}</h2>
{% include 'users/aff_users.html' with users_list=users %} {% include 'pagination.html' with list=users page_arg='users_page' go_to_id='users_results' %}
{% include 'users/aff_users.html' with users_list=users.object_list %}
{% include 'pagination.html' with list=users page_arg='users_page' go_to_id='users_results' %}
</div>
<hr/>
<br/>
{% endif%} {% endif%}
{% if clubs %} {% if clubs %}
<div id="clubs_results">
<h2>{% trans "Results among clubs:" %}</h2> <h2>{% trans "Results among clubs:" %}</h2>
{% include 'users/aff_clubs.html' with clubs_list=clubs %} {% include 'pagination.html' with list=clubs page_arg='clubs_page' go_to_id='clubs_results' %}
{% include 'users/aff_clubs.html' with clubs_list=clubs.object_list %}
{% include 'pagination.html' with list=clubs page_arg='clubs_page' go_to_id='clubs_results' %}
</div>
<hr/>
<br/>
{% endif%} {% endif%}
{% if machines %} {% if machines %}
<div id="machines_results">
<h2>{% trans "Results among machines:" %}</h2> <h2>{% trans "Results among machines:" %}</h2>
{% include 'machines/aff_machines.html' with machines_list=machines %} {% include 'pagination.html' with list=machines page_arg='machines_page' go_to_id='machines_results' %}
{% include 'machines/aff_machines.html' with machines_list=machines.object_list %}
{% include 'pagination.html' with list=machines page_arg='machines_page' go_to_id='machines_results' %}
</div>
<hr/>
<br/>
{% endif %} {% endif %}
{% if factures %} {% if factures %}
<div id="invoices_results">
<h2>{% trans "Results among invoices:" %}</h2> <h2>{% trans "Results among invoices:" %}</h2>
{% include 'cotisations/aff_cotisations.html' with facture_list=factures %} {% include 'pagination.html' with list=factures page_arg='factures_page' go_to_id='invoices_results' %}
{% include 'cotisations/aff_cotisations.html' with facture_list=factures.object_list %}
{% include 'pagination.html' with list=factures page_arg='factures_page' go_to_id='invoices_results' %}
</div>
<hr/>
<br/>
{% endif %} {% endif %}
{% if whitelists %} {% if whitelists %}
<div id="whitelists_results">
<h2>{% trans "Results among whitelists:" %}</h2> <h2>{% trans "Results among whitelists:" %}</h2>
{% include 'users/aff_whitelists.html' with white_list=whitelists %} {% include 'pagination.html' with list=whitelists page_arg='whitelists_page' go_to_id='whitelists_results' %}
{% include 'users/aff_whitelists.html' with white_list=whitelists.object_list %}
{% include 'pagination.html' with list=whitelists page_arg='whitelists_page' go_to_id='whitelists_results' %}
</div>
<hr/>
<br/>
{% endif %} {% endif %}
{% if bans %} {% if bans %}
<div id="bans_results">
<h2>{% trans "Results among bans:" %}</h2> <h2>{% trans "Results among bans:" %}</h2>
{% include 'users/aff_bans.html' with ban_list=bans %} {% include 'pagination.html' with list=bans page_arg='bans_page' go_to_id='bans_results' %}
{% include 'users/aff_bans.html' with ban_list=bans.object_list %}
{% include 'pagination.html' with list=bans page_arg='bans_page' go_to_id='bans_results' %}
</div>
<hr/>
<br/>
{% endif %} {% endif %}
{% if rooms %} {% if rooms %}
<div id="rooms_results">
<h2>{% trans "Results among rooms:" %}</h2> <h2>{% trans "Results among rooms:" %}</h2>
{% include 'topologie/aff_chambres.html' with room_list=rooms %} {% include 'pagination.html' with list=rooms page_arg='rooms_page' go_to_id='rooms_results' %}
{% include 'topologie/aff_chambres.html' with room_list=rooms.object_list %}
{% include 'pagination.html' with list=rooms page_arg='rooms_page' go_to_id='rooms_results' %}
</div>
<hr/>
<br/>
{% endif %} {% endif %}
{% if ports %} {% if ports %}
<div id="ports_results">
<h2>{% trans "Results among ports:" %}</h2> <h2>{% trans "Results among ports:" %}</h2>
{% include 'topologie/aff_port.html' with port_list=ports search=True %} {% include 'pagination.html' with list=ports page_arg='ports_page' go_to_id='ports_results' %}
{% include 'topologie/aff_port.html' with port_list=ports.object_list search=True %}
{% include 'pagination.html' with list=ports page_arg='ports_page' go_to_id='ports_results' %}
</div>
<hr/>
<br/>
{% endif %} {% endif %}
{% if switches %} {% if switches %}
<div id="switches_results">
<h2>{% trans "Results among switches:" %}</h2> <h2>{% trans "Results among switches:" %}</h2>
{% include 'topologie/aff_switch.html' with switch_list=switches %} {% include 'pagination.html' with list=switches page_arg='switches_page' go_to_id='switches_results' %}
{% include 'topologie/aff_switch.html' with switch_list=switches.object_list %}
{% include 'pagination.html' with list=switches page_arg='switches_page' go_to_id='switches_results' %}
</div>
<hr/>
<br/>
{% endif %} {% endif %}
{% if not users and not machines and not factures and not whitelists and not bans and not rooms and not ports and not switches %} {% if not users and not machines and not factures and not whitelists and not bans and not rooms and not ports and not switches %}
<h3>{% trans "No result" %}</h3> <h3>{% trans "No result" %}</h3>
...@@ -74,4 +128,3 @@ with this program; if not, write to the Free Software Foundation, Inc., ...@@ -74,4 +128,3 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<br /> <br />
<br /> <br />
{% endblock %} {% endblock %}
...@@ -46,7 +46,7 @@ from search.forms import ( ...@@ -46,7 +46,7 @@ from search.forms import (
CHOICES_AFF, CHOICES_AFF,
initial_choices, initial_choices,
) )
from re2o.base import SortTable from re2o.base import SortTable, re2o_paginator
from re2o.acl import can_view_all from re2o.acl import can_view_all
...@@ -61,7 +61,7 @@ def is_int(variable): ...@@ -61,7 +61,7 @@ def is_int(variable):
return True return True
def finish_results(results, col, order): def finish_results(request, results, col, order):
"""Sort the results by applying filters and then limit them to the """Sort the results by applying filters and then limit them to the
number of max results. Finally add the info of the nmax number of results number of max results. Finally add the info of the nmax number of results
to the dict""" to the dict"""
...@@ -93,7 +93,9 @@ def finish_results(results, col, order): ...@@ -93,7 +93,9 @@ def finish_results(results, col, order):
max_result = GeneralOption.get_cached_value("search_display_page") max_result = GeneralOption.get_cached_value("search_display_page")
for name, val in results.items(): for name, val in results.items():
results[name] = val.distinct()[:max_result] page_arg = name + "_page"
results[name] = re2o_paginator(request, val.distinct(), max_result, page_arg=page_arg)
results.update({"max_result": max_result}) results.update({"max_result": max_result})
return results return results
...@@ -316,7 +318,7 @@ def get_results(query, request, params): ...@@ -316,7 +318,7 @@ def get_results(query, request, params):
"switches": Switch.objects.filter(filters["switches"]), "switches": Switch.objects.filter(filters["switches"]),
} }
results = finish_results(results, request.GET.get("col"), request.GET.get("order")) results = finish_results(request, results, request.GET.get("col"), request.GET.get("order"))
results.update({"search_term": query}) results.update({"search_term": query})
return results return results
......
...@@ -23,19 +23,20 @@ with this program; if not, write to the Free Software Foundation, Inc., ...@@ -23,19 +23,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% endcomment %} {% endcomment %}
{% load url_insert_param %} {% load url_insert_param %}
{% load pagination_extra %}
{% load i18n %} {% load i18n %}
{% if list.paginator.num_pages > 1 %} {% if list.paginator.num_pages > 1 %}
<ul class="pagination text-center"> <ul class="pagination text-center">
{% if list.has_previous %} {% if list.has_previous %}
<li> <li>
<a href="{% url_insert_param request.get_full_path page=1 %}{% if go_to_id %}#{{ go_to_id }}{% endif %}"> <a href="{% pagination_insert_page_and_id request.get_full_path 1 page_arg=page_arg %}{% if go_to_id %}#{{ go_to_id }}{% endif %}">
<span aria-hidden="true">&laquo;</span> <span aria-hidden="true">&laquo;</span>
<span class="sr-only">{% trans "First" %}</span> <span class="sr-only">{% trans "First" %}</span>
</a> </a>
</li> </li>
<li> <li>
<a href="{% url_insert_param request.get_full_path page=list.previous_page_number %}{% if go_to_id %}#{{ go_to_id }}{% endif %}"> <a href="{% pagination_insert_page_and_id request.get_full_path list.previous_page_number page_arg=page_arg %}{% if go_to_id %}#{{ go_to_id }}{% endif %}">
<span aria-hidden="true">&lsaquo;</span> <span aria-hidden="true">&lsaquo;</span>
<span class="sr-only">{% trans "Previous" %}</span> <span class="sr-only">{% trans "Previous" %}</span>
</a> </a>
...@@ -48,20 +49,20 @@ with this program; if not, write to the Free Software Foundation, Inc., ...@@ -48,20 +49,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% for page in list.paginator.page_range %} {% for page in list.paginator.page_range %}
{% if list.number <= page|add:"3" and list.number >= page|add:"-3" %} {% if list.number <= page|add:"3" and list.number >= page|add:"-3" %}
<li class="{% if list.number == page %}active{% endif %}"> <li class="{% if list.number == page %}active{% endif %}">
<a href="{% url_insert_param request.get_full_path page=page %}{% if go_to_id %}#{{ go_to_id }}{% endif %}">{{ page }}</a> <a href="{% pagination_insert_page_and_id request.get_full_path page page_arg=page_arg %}{% if go_to_id %}#{{ go_to_id }}{% endif %}">{{ page }}</a>
</li> </li>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% if list.has_next %} {% if list.has_next %}
<li> <li>
<a href="{% url_insert_param request.get_full_path page=list.next_page_number %}{% if go_to_id %}#{{ go_to_id }}{% endif %}"> <a href="{% pagination_insert_page_and_id request.get_full_path list.next_page_number page_arg=page_arg %}{% if go_to_id %}#{{ go_to_id }}{% endif %}">
<span aria-hidden="true">&rsaquo;</span> <span aria-hidden="true">&rsaquo;</span>
<span class="sr-only">{% trans "Next" %}</span> <span class="sr-only">{% trans "Next" %}</span>
</a> </a>
</li> </li>
<li> <li>
<a href="{% url_insert_param request.get_full_path page=list.paginator.page_range|length %}{% if go_to_id %}#{{ go_to_id }}{% endif %}"> <a href="{% pagination_insert_page_and_id request.get_full_path list.paginator.page_range|length page_arg=page_arg %}{% if go_to_id %}#{{ go_to_id }}{% endif %}">
<span aria-hidden="true">&raquo;</span> <span aria-hidden="true">&raquo;</span>
<span class="sr-only">{% trans "Last" %}</span> <span class="sr-only">{% trans "Last" %}</span>
</a> </a>
...@@ -71,4 +72,5 @@ with this program; if not, write to the Free Software Foundation, Inc., ...@@ -71,4 +72,5 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<li class="disabled"><span aria-hidden="true">&raquo;</span></li> <li class="disabled"><span aria-hidden="true">&raquo;</span></li>
{% endif %} {% endif %}
</ul> </ul>
{% endif %} {% endif %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment