[IMP] website: create method pager and template pager. Used by website_sale and website_event
bzr revid: chm@openerp.com-20130812134312-jmy2je288b4ofazy
This commit is contained in:
parent
a82f0fa1df
commit
8523e66bc4
|
@ -434,5 +434,23 @@
|
|||
</t>
|
||||
</template>
|
||||
|
||||
<template id="pager">
|
||||
<div t-if="pager['page_count'] != 1" t-attf-class="#{ classname or '' } pagination">
|
||||
<ul>
|
||||
<li t-att-class=" 'disabled' if pager['page']['num'] == 1 else '' " >
|
||||
<a t-att-href=" pager['page_start']['url'] if pager['page']['num'] != 1 else '' ">Prev</a>
|
||||
</li>
|
||||
<t t-foreach="pager['pages']">
|
||||
<li t-att-class=" 'active' if num == pager['page']['num'] else '' ">
|
||||
<a t-att-href="url" t-raw="num"></a>
|
||||
</li>
|
||||
</t>
|
||||
<li t-att-class=" 'disabled' if pager['page']['num'] == pager['page_count'] else '' " >
|
||||
<a t-att-href=" pager['page_end']['url'] if pager['page']['num'] != pager['page_count'] else '' ">Next</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
</data>
|
||||
</openerp>
|
||||
|
|
|
@ -5,6 +5,8 @@ from openerp.osv import osv
|
|||
from openerp.addons.web import http
|
||||
from openerp.addons.web.controllers import main
|
||||
from openerp.addons.web.http import request
|
||||
import urllib
|
||||
import math
|
||||
|
||||
|
||||
def auth_method_public():
|
||||
|
@ -16,6 +18,14 @@ def auth_method_public():
|
|||
http.auth_methods['public'] = auth_method_public
|
||||
|
||||
|
||||
def urlplus(url, params):
|
||||
if not params:
|
||||
return url
|
||||
url += "?"
|
||||
for k,v in params.items():
|
||||
url += "%s=%s&" % (k, urllib.quote_plus(v))
|
||||
return url
|
||||
|
||||
class website(osv.osv):
|
||||
_name = "website" # Avoid website.website convention for conciseness (for new api). Got a special authorization from xmo and rco
|
||||
_description = "Website"
|
||||
|
@ -56,3 +66,29 @@ class website(osv.osv):
|
|||
'inherit_branding': values.get('editable', False),
|
||||
}
|
||||
return request.registry.get("ir.ui.view").render(request.cr, request.uid, template, values, context=context)
|
||||
|
||||
def pager(self, url, total, page=1, step=30, scope=5):
|
||||
# Compute Pager
|
||||
d = {}
|
||||
d["page_count"] = int(math.ceil(total / step))
|
||||
|
||||
page = max(1, min(int(page), d["page_count"]))
|
||||
|
||||
d["offset"] = (page-1) * step
|
||||
scope -= 1
|
||||
|
||||
pmin = max(page - int(math.floor(scope/2)), 1)
|
||||
pmax = min(pmin + scope, d["page_count"])
|
||||
|
||||
if pmax - pmin < scope:
|
||||
pmin = pmax - scope > 0 and pmax - scope or 1
|
||||
|
||||
|
||||
d["page"] = {'url': "%spage/%s/" % (url, page), 'num': page}
|
||||
d["page_start"] = {'url': "%spage/%s/" % (url, pmin), 'num': pmin}
|
||||
d["page_end"] = {'url': "%spage/%s/" % (url, min(pmax, page+1)), 'num': min(pmax, page+1)}
|
||||
d["pages"] = []
|
||||
for page in range(pmin, pmax+1):
|
||||
d["pages"].append({'url': "%spage/%s/" % (url, page), 'num': page})
|
||||
|
||||
return d
|
|
@ -14,8 +14,8 @@ import werkzeug
|
|||
|
||||
class website_event(http.Controller):
|
||||
|
||||
@http.route(['/event'], type='http', auth="public")
|
||||
def events(self, **searches):
|
||||
@http.route(['/event/', '/event/page/<int:page>/'], type='http', auth="public")
|
||||
def events(self, page=1, **searches):
|
||||
website = request.registry['website']
|
||||
event_obj = request.registry['event.event']
|
||||
|
||||
|
@ -80,13 +80,17 @@ class website_event(http.Controller):
|
|||
countries = event_obj.read_group(request.cr, request.uid, domain, ["id", "country_id"], groupby="country_id", orderby="country_id")
|
||||
countries.insert(0, {'country_id_count': event_obj.search(request.cr, request.uid, domain, count=True), 'country_id': ("all", _("All Countries"))})
|
||||
|
||||
step = 5
|
||||
event_count = event_obj.search(request.cr, request.uid, dom_without("none"), count=True)
|
||||
pager = website.pager(url="/event/", total=event_count, page=page, step=step, scope=5)
|
||||
obj_ids = event_obj.search(request.cr, request.uid, dom_without("none"), limit=step, offset=pager['offset'], order="date_begin DESC")
|
||||
|
||||
obj_ids = event_obj.search(request.cr, request.uid, dom_without("none"), order="date_begin DESC")
|
||||
values = website.get_rendering_context({
|
||||
'event_ids': event_obj.browse(request.cr, request.uid, obj_ids),
|
||||
'dates': dates,
|
||||
'types': types,
|
||||
'countries': countries,
|
||||
'pager': pager,
|
||||
'searches': searches,
|
||||
'search_path': "?%s" % urllib.urlencode(searches),
|
||||
})
|
||||
|
|
|
@ -50,8 +50,15 @@
|
|||
</t>
|
||||
</ul>
|
||||
</div>
|
||||
<div class='span8 navbar navbar-inverse'>
|
||||
<div class='navbar-inner'>
|
||||
<t t-call="website.pager" >
|
||||
<t t-set="classname">pull-left</t>
|
||||
</t>
|
||||
</div>
|
||||
</div>
|
||||
<div class="span8">
|
||||
<ul class="media-list mt32">
|
||||
<ul class="media-list">
|
||||
<li t-foreach="event_ids" t-as="event_id" class="media thumbnail">
|
||||
<div class="media-body">
|
||||
<span t-if="not event_id.event_ticket_ids" class="label pull-right">No tickets needed.</span>
|
||||
|
@ -87,6 +94,9 @@
|
|||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="span8 offset4 text-center">
|
||||
<t t-call="website.pager" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</t>
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import math
|
||||
import openerp
|
||||
from openerp.osv import osv
|
||||
from openerp.addons.web import http
|
||||
from openerp.addons.web.http import request
|
||||
import werkzeug
|
||||
from urllib import urlencode
|
||||
|
||||
def get_order(order_id=None):
|
||||
order_obj = request.registry.get('sale.order')
|
||||
|
@ -50,7 +48,6 @@ class Ecommerce(http.Controller):
|
|||
category_obj = request.registry.get('pos.category')
|
||||
category_ids = category_obj.search(request.cr, openerp.SUPERUSER_ID, [('parent_id', '=', False)])
|
||||
categories = category_obj.browse(request.cr, openerp.SUPERUSER_ID, category_ids)
|
||||
print categories
|
||||
return categories
|
||||
|
||||
def render(self, template, values={}):
|
||||
|
@ -87,7 +84,7 @@ class Ecommerce(http.Controller):
|
|||
product_ids = product_obj.search(request.cr, request.uid, [("id", "in", product_ids)])
|
||||
return product_obj.browse(request.cr, request.uid, product_ids)
|
||||
|
||||
@http.route(['/shop/', '/shop/category/<cat_id>/', '/shop/category/<cat_id>/page/<page>/', '/shop/page/<page>/'], type='http', auth="public")
|
||||
@http.route(['/shop/', '/shop/category/<cat_id>/', '/shop/category/<cat_id>/page/<int:page>/', '/shop/page/<int:page>/'], type='http', auth="public")
|
||||
def category(self, cat_id=0, page=0, **post):
|
||||
|
||||
website = request.registry['website']
|
||||
|
@ -101,36 +98,17 @@ class Ecommerce(http.Controller):
|
|||
cat_id = int(cat_id)
|
||||
domain = [('pos_categ_id.id', 'child_of', cat_id)] + domain
|
||||
|
||||
step = 20
|
||||
product_count = len(product_obj.search(request.cr, request.uid, domain))
|
||||
page_count = int(math.ceil(product_count / 20.0))
|
||||
pager = website.pager(url="/shop/category/%s/" % cat_id, total=product_count, page=page, step=step, scope=7)
|
||||
|
||||
#if post.get("search"):
|
||||
# domain += ['|', '|', ('name', 'ilike', "%%%s%%" % post.get("search")), ('description', 'ilike', "%%%s%%" % post.get("search")), ('pos_categ_id.name', 'ilike', "%%%s%%" % post.get("search"))]
|
||||
|
||||
page = max(1,min(int(page),page_count))
|
||||
offset = (page-1) * 20
|
||||
|
||||
if page_count <= 5 or page <= 3:
|
||||
pmin = 1
|
||||
pmax = min(page_count,5)
|
||||
elif page >= page_count - 2:
|
||||
pmin = page_count - 4
|
||||
pmax = page_count
|
||||
else:
|
||||
pmin = page - 2
|
||||
pmax = page + 2
|
||||
|
||||
pages = range(pmin, pmax+1)
|
||||
|
||||
product_ids = product_obj.search(request.cr, request.uid, domain, limit=20, offset=offset)
|
||||
product_ids = product_obj.search(request.cr, request.uid, domain, limit=step, offset=pager['offset'])
|
||||
|
||||
values = website.get_rendering_context({
|
||||
'current_category': cat_id,
|
||||
'products': product_obj.browse(request.cr, request.uid, product_ids),
|
||||
'search': post.get("search"),
|
||||
'page_count': page_count,
|
||||
'pages': pages,
|
||||
'page': page,
|
||||
'pager': pager,
|
||||
})
|
||||
return self.render("website_sale.products", values)
|
||||
|
||||
|
|
|
@ -87,33 +87,13 @@
|
|||
|
||||
<!-- Product list -->
|
||||
|
||||
<template id="product_pager">
|
||||
<div t-if="page_count != 1" t-attf-class="#{ classname or '' } pagination">
|
||||
<ul>
|
||||
<li t-att-class=" 'disabled' if page == 1 else '' " >
|
||||
<t t-set="alt_url" t-valuef="/shop/category/#{ current_category }/page/#{ max(0, page - 1) }/"/>
|
||||
<a t-att-href=" '#' if page == 1 else alt_url ">Prev</a>
|
||||
</li>
|
||||
<t t-foreach="pages" t-as="p">
|
||||
<li t-att-class=" 'active' if p == page else '' ">
|
||||
<a t-attf-href="/shop/category/#{ current_category }/page/#{ p }/" t-raw='p'></a>
|
||||
</li>
|
||||
</t>
|
||||
<li t-att-class=" 'disabled' if page == page_count else '' ">
|
||||
<t t-set="alt_url" t-valuef="/shop/category/#{ current_category }/page/#{ min(page_count, page + 1) }/"/>
|
||||
<a t-att-href=" '#' if page == page_count else alt_url ">Next</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template id="products">
|
||||
<t t-call="website_sale.page">
|
||||
<t t-set="title">Product</t>
|
||||
<t t-set="shop_content">
|
||||
<div class='span8 navbar navbar-inverse'>
|
||||
<div class='navbar-inner'>
|
||||
<t t-call="website_sale.product_pager" >
|
||||
<t t-call="website.pager" >
|
||||
<t t-set="classname">pull-left</t>
|
||||
</t>
|
||||
<form action="/shop/" method="get" class="navbar-search pull-right">
|
||||
|
@ -132,7 +112,7 @@
|
|||
</div>
|
||||
</div>
|
||||
<div class="span8 offset4 text-center">
|
||||
<t t-call="website_sale.product_pager" />
|
||||
<t t-call="website.pager" />
|
||||
</div>
|
||||
</t>
|
||||
</t>
|
||||
|
|
Loading…
Reference in New Issue