[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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -69,4 +69,18 @@
|
||||
<t t-call="portal.portal_layout">
|
||||
</t>
|
||||
</template>
|
||||
|
||||
<template id="pager" name="Pager">
|
||||
<ul t-if="pager['page_count'] > 1" t-attf-class="#{ classname or '' } pagination" t-att-style="style or None">
|
||||
<li t-att-class=" 'disabled' if pager['page']['num'] == 1 else None ">
|
||||
<a t-att-href=" pager['page_previous']['url'] if pager['page']['num'] != 1 else None">Prev</a>
|
||||
</li>
|
||||
<t t-foreach="pager['pages']" t-as="page">
|
||||
<li t-att-class=" 'active' if page['num'] == pager['page']['num'] else None "> <a t-att-href="page['url']" t-raw="page['num']"></a></li>
|
||||
</t>
|
||||
<li t-att-class=" 'disabled' if pager['page']['num'] == pager['page_count'] else None ">
|
||||
<a t-att-href="pager['page_next']['url'] if pager['page']['num'] != pager['page_count'] else None">Next</a>
|
||||
</li>
|
||||
</ul>
|
||||
</template>
|
||||
</odoo>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -379,18 +379,7 @@
|
||||
</t>
|
||||
</template>
|
||||
|
||||
<template id="pager" name="Pager">
|
||||
<ul t-if="pager['page_count'] > 1" t-attf-class="#{ classname or '' } pagination" t-att-style="style or None">
|
||||
<li t-att-class=" 'disabled' if pager['page']['num'] == 1 else None ">
|
||||
<a t-att-href=" pager['page_previous']['url'] if pager['page']['num'] != 1 else None">Prev</a>
|
||||
</li>
|
||||
<t t-foreach="pager['pages']" t-as="page">
|
||||
<li t-att-class=" 'active' if page['num'] == pager['page']['num'] else None "> <a t-att-href="page['url']" t-raw="page['num']"></a></li>
|
||||
</t>
|
||||
<li t-att-class=" 'disabled' if pager['page']['num'] == pager['page_count'] else None ">
|
||||
<a t-att-href="pager['page_next']['url'] if pager['page']['num'] != pager['page_count'] else None">Next</a>
|
||||
</li>
|
||||
</ul>
|
||||
<template id="pager" name="Pager" inherit_id="portal.pager">
|
||||
</template>
|
||||
|
||||
<!--
|
||||
|
||||
Reference in New Issue
Block a user