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..a93547cc 100644 --- a/search/templates/search/index.html +++ b/search/templates/search/index.html @@ -30,40 +30,94 @@ 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 %} + {% include 'pagination.html' with list=users page_arg='users_page' go_to_id='users_results' %} +
+
+
{% 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 %} + {% include 'pagination.html' with list=clubs page_arg='clubs_page' go_to_id='clubs_results' %} +
+
+
{% 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 %} + {% include 'pagination.html' with list=machines page_arg='machines_page' go_to_id='machines_results' %} +
+
+
{% 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 %} + {% include 'pagination.html' with list=factures page_arg='factures_page' go_to_id='invoices_results' %} +
+
+
{% 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 %} + {% include 'pagination.html' with list=whitelists page_arg='whitelists_page' go_to_id='whitelists_results' %} +
+
+
{% 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 %} + {% include 'pagination.html' with list=bans page_arg='bans_page' go_to_id='bans_results' %} +
+
+
{% 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 %} + {% include 'pagination.html' with list=rooms page_arg='rooms_page' go_to_id='rooms_results' %} +
+
+
{% 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 %} + {% include 'pagination.html' with list=ports page_arg='ports_page' go_to_id='ports_results' %} +
+
+
{% 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 %} + {% 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" %}

@@ -74,4 +128,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 %}