From b4b6ae44fde3ea291231d47a496537b21e12d38d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 8 Jun 2017 14:14:40 +0200 Subject: [PATCH] [MOV] website, portal: move pager support from website to portal As customer portal will be moved to portal module support of pager has to be moved previously to the various portal moves. This commit moves the pager code and template to portal. Compatibility using website-based pager is provided to avoid having to update all website addons currently. Future commits will make addons use the portal pager instead of website pager. --- addons/http_routing/models/ir_http.py | 2 + addons/portal/controllers/portal.py | 67 ++++++++++++++++++++++ addons/portal/views/portal_templates.xml | 14 +++++ addons/website/models/website.py | 59 +------------------ addons/website/views/website_templates.xml | 13 +---- 5 files changed, 86 insertions(+), 69 deletions(-) diff --git a/addons/http_routing/models/ir_http.py b/addons/http_routing/models/ir_http.py index 93015232b54..4f8b50bb2f7 100644 --- a/addons/http_routing/models/ir_http.py +++ b/addons/http_routing/models/ir_http.py @@ -15,6 +15,7 @@ except ImportError: import odoo from odoo import api, models from odoo.addons.base.ir.ir_http import RequestUID, ModelConverter +from odoo.addons.portal.controllers.portal import pager from odoo.http import request from odoo.tools import config, ustr @@ -279,6 +280,7 @@ class IrHttp(models.AbstractModel): set on request will be created with uid=None (and it is a lazy property) """ request.routing_iteration = getattr(request, 'routing_iteration', 0) + 1 + request.pager = pager func = None # locate the controller method diff --git a/addons/portal/controllers/portal.py b/addons/portal/controllers/portal.py index 1e7d97f7eb8..1fd7c9ee759 100644 --- a/addons/portal/controllers/portal.py +++ b/addons/portal/controllers/portal.py @@ -1,9 +1,76 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +import math + +from werkzeug import urls + from odoo.http import Controller, request, route +# -------------------------------------------------- +# Misc tools +# -------------------------------------------------- + +def pager(url, total, page=1, step=30, scope=5, url_args=None): + """ Generate a dict with required value to render `website.pager` template. This method compute + url, page range to display, ... in the pager. + :param url : base url of the page link + :param total : number total of item to be splitted into pages + :param page : current page + :param step : item per page + :param scope : number of page to display on pager + :param url_args : additionnal parameters to add as query params to page url + :type url_args : dict + :returns dict + """ + # Compute Pager + page_count = int(math.ceil(float(total) / step)) + + page = max(1, min(int(page if str(page).isdigit() else 1), page_count)) + scope -= 1 + + pmin = max(page - int(math.floor(scope/2)), 1) + pmax = min(pmin + scope, page_count) + + if pmax - pmin < scope: + pmin = pmax - scope if pmax - scope > 0 else 1 + + def get_url(page): + _url = "%s/page/%s" % (url, page) if page > 1 else url + if url_args: + _url = "%s?%s" % (_url, urls.url_encode(url_args)) + return _url + + return { + "page_count": page_count, + "offset": (page - 1) * step, + "page": { + 'url': get_url(page), + 'num': page + }, + "page_start": { + 'url': get_url(pmin), + 'num': pmin + }, + "page_previous": { + 'url': get_url(max(pmin, page - 1)), + 'num': max(pmin, page - 1) + }, + "page_next": { + 'url': get_url(min(pmax, page + 1)), + 'num': min(pmax, page + 1) + }, + "page_end": { + 'url': get_url(pmax), + 'num': pmax + }, + "pages": [ + {'url': get_url(page_num), 'num': page_num} for page_num in range(pmin, pmax+1) + ] + } + + class CustomerPortal(Controller): def _prepare_portal_layout_values(self): diff --git a/addons/portal/views/portal_templates.xml b/addons/portal/views/portal_templates.xml index 715cb3edc57..bd89945c81f 100644 --- a/addons/portal/views/portal_templates.xml +++ b/addons/portal/views/portal_templates.xml @@ -69,4 +69,18 @@ + + diff --git a/addons/website/models/website.py b/addons/website/models/website.py index 45a733f7d5e..bfd5e3e5fc5 100644 --- a/addons/website/models/website.py +++ b/addons/website/models/website.py @@ -3,7 +3,6 @@ import inspect import logging -import math import hashlib import re @@ -12,6 +11,7 @@ from werkzeug.exceptions import NotFound from odoo import api, fields, models, tools from odoo.addons.http_routing.models.ir_http import slugify +from odoo.addons.portal.controllers.portal import pager from odoo.tools import pycompat from odoo.http import request from odoo.tools.translate import _ @@ -316,62 +316,7 @@ class Website(models.Model): @api.model def pager(self, url, total, page=1, step=30, scope=5, url_args=None): - """ Generate a dict with required value to render `website.pager` template. This method compute - url, page range to display, ... in the pager. - :param url : base url of the page link - :param total : number total of item to be splitted into pages - :param page : current page - :param step : item per page - :param scope : number of page to display on pager - :param url_args : additionnal parameters to add as query params to page url - :type url_args : dict - :returns dict - """ - # Compute Pager - page_count = int(math.ceil(float(total) / step)) - - page = max(1, min(int(page if str(page).isdigit() else 1), page_count)) - scope -= 1 - - pmin = max(page - int(math.floor(scope/2)), 1) - pmax = min(pmin + scope, page_count) - - if pmax - pmin < scope: - pmin = pmax - scope if pmax - scope > 0 else 1 - - def get_url(page): - _url = "%s/page/%s" % (url, page) if page > 1 else url - if url_args: - _url = "%s?%s" % (_url, urls.url_encode(url_args)) - return _url - - return { - "page_count": page_count, - "offset": (page - 1) * step, - "page": { - 'url': get_url(page), - 'num': page - }, - "page_start": { - 'url': get_url(pmin), - 'num': pmin - }, - "page_previous": { - 'url': get_url(max(pmin, page - 1)), - 'num': max(pmin, page - 1) - }, - "page_next": { - 'url': get_url(min(pmax, page + 1)), - 'num': min(pmax, page + 1) - }, - "page_end": { - 'url': get_url(pmax), - 'num': pmax - }, - "pages": [ - {'url': get_url(page), 'num': page} for page in range(pmin, pmax+1) - ] - } + return pager(url, total, page=page, step=step, scope=scope, url_args=url_args) def rule_is_enumerable(self, rule): """ Checks that it is possible to generate sensible GET queries for diff --git a/addons/website/views/website_templates.xml b/addons/website/views/website_templates.xml index 417b0e4017a..732cfb44db3 100644 --- a/addons/website/views/website_templates.xml +++ b/addons/website/views/website_templates.xml @@ -379,18 +379,7 @@ -