From 83a6dea5c9979daf00d1dfa7d801d41ff3a17404 Mon Sep 17 00:00:00 2001 From: Jean-Roman Garnier Date: Fri, 6 Dec 2019 00:01:13 +0000 Subject: [PATCH 1/2] Add pagination in search results --- re2o/base.py | 4 +- re2o/templatetags/pagination_extra.py | 89 +++++++++++++++++++++++++++ search/templates/search/index.html | 45 +++++++++++--- search/views.py | 10 +-- templates/pagination.html | 12 ++-- 5 files changed, 139 insertions(+), 21 deletions(-) create mode 100644 re2o/templatetags/pagination_extra.py diff --git a/re2o/base.py b/re2o/base.py index 6b12a4a6..d9e3efa5 100644 --- a/re2o/base.py +++ b/re2o/base.py @@ -242,13 +242,13 @@ class SortTable: 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. :request: :query_set: Query_set to paginate :pagination_number: Number of entries to display""" paginator = Paginator(query_set, pagination_number) - page = request.GET.get("page") + page = request.GET.get(page_arg) try: results = paginator.page(page) except PageNotAnInteger: diff --git a/re2o/templatetags/pagination_extra.py b/re2o/templatetags/pagination_extra.py new file mode 100644 index 00000000..41491731 --- /dev/null +++ b/re2o/templatetags/pagination_extra.py @@ -0,0 +1,89 @@ +# -*- 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 diff --git a/search/templates/search/index.html b/search/templates/search/index.html index e7f05a88..2b05dbb4 100644 --- a/search/templates/search/index.html +++ b/search/templates/search/index.html @@ -30,40 +30,66 @@ with this program; if not, write to the Free Software Foundation, Inc., {% block content %} {% if users %} +

{% trans "Results among users:" %}

- {% 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 %} +
{% endif%} {% if clubs %} +

{% trans "Results among clubs:" %}

- {% 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 %} +
{% endif%} {% if machines %} +

{% trans "Results among machines:" %}

- {% 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 %} +
{% endif %} {% if factures %} +

{% trans "Results among invoices:" %}

- {% 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 %} +
{% endif %} {% if whitelists %} +

{% trans "Results among whitelists:" %}

- {% 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 %} +
{% endif %} {% if bans %} +

{% trans "Results among bans:" %}

- {% 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 %} +
{% endif %} {% if rooms %} +

{% trans "Results among rooms:" %}

- {% 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 %} {% endif %} {% if ports %} +

{% trans "Results among ports:" %}

- {% 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 %} +
{% endif %} {% if switches %} +

{% trans "Results among switches:" %}

- {% 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 %} +
{% 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 %}

{% trans "No result" %}

@@ -74,4 +100,3 @@ with this program; if not, write to the Free Software Foundation, Inc.,

{% endblock %} - diff --git a/search/views.py b/search/views.py index bcfdaeee..2a6810a8 100644 --- a/search/views.py +++ b/search/views.py @@ -46,7 +46,7 @@ from search.forms import ( CHOICES_AFF, initial_choices, ) -from re2o.base import SortTable +from re2o.base import SortTable, re2o_paginator from re2o.acl import can_view_all @@ -61,7 +61,7 @@ def is_int(variable): 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 number of max results. Finally add the info of the nmax number of results to the dict""" @@ -93,7 +93,9 @@ def finish_results(results, col, order): max_result = GeneralOption.get_cached_value("search_display_page") 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}) return results @@ -316,7 +318,7 @@ def get_results(query, request, params): "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}) return results diff --git a/templates/pagination.html b/templates/pagination.html index a0cc905a..402f61be 100644 --- a/templates/pagination.html +++ b/templates/pagination.html @@ -23,19 +23,20 @@ with this program; if not, write to the Free Software Foundation, Inc., {% endcomment %} {% load url_insert_param %} +{% load pagination_extra %} {% load i18n %} {% if list.paginator.num_pages > 1 %} + {% endif %} From 83123d9dc164b3213bc1ce0526f638d5f3010b86 Mon Sep 17 00:00:00 2001 From: Jean-Roman Garnier Date: Fri, 6 Dec 2019 00:15:06 +0000 Subject: [PATCH 2/2] Make search-results pagination more coherent with rest of pagination style --- search/templates/search/index.html | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/search/templates/search/index.html b/search/templates/search/index.html index 2b05dbb4..a93547cc 100644 --- a/search/templates/search/index.html +++ b/search/templates/search/index.html @@ -34,62 +34,90 @@ with this program; if not, write to the Free Software Foundation, Inc.,

{% trans "Results among 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' %}
+
+
{% endif%} {% if clubs %}

{% trans "Results among 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' %}
+
+
{% endif%} {% if machines %}

{% trans "Results among 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' %}
+
+
{% endif %} {% if factures %}

{% trans "Results among invoices:" %}

{% 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' %}
+
+
{% endif %} {% if whitelists %}

{% trans "Results among 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' %}
+
+
{% endif %} {% if bans %}

{% trans "Results among 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' %}
+
+
{% endif %} {% if rooms %}

{% trans "Results among 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' %} +
+
+
{% endif %} {% if ports %}

{% trans "Results among ports:" %}

{% 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' %}
+
+
{% endif %} {% if switches %}

{% trans "Results among 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' %}
+
+
{% 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 %}

{% trans "No result" %}