[IMP] website: refactoring + create method pager and template pager. Used by website_sale and website_event
bzr revid: chm@openerp.com-20130812134826-tij95o38mf3r7tts
This commit is contained in:
commit
f6c27fda01
|
@ -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>
|
||||
|
|
|
@ -6,6 +6,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():
|
||||
|
@ -17,6 +19,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"
|
||||
|
@ -58,3 +68,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>
|
||||
|
|
|
@ -25,32 +25,20 @@
|
|||
|
||||
<!-- Page -->
|
||||
|
||||
<template id="index">
|
||||
<template id="index" name="Blogs">
|
||||
<t t-call="website.layout">
|
||||
<t t-set="head">
|
||||
<script type="text/javascript" src="/website_mail/static/src/js/blog.js"></script>
|
||||
<link rel='stylesheet' href='/website_mail/static/src/css/blog.css'/>
|
||||
<t t-raw="head"/>
|
||||
<t t-raw="head or ''"/>
|
||||
</t>
|
||||
<t t-set="title">Blog</t>
|
||||
<div class="container mt48 js_website_mail">
|
||||
<div class="row">
|
||||
<div class="span4">
|
||||
<ul class="nav nav-list">
|
||||
<a t-if="mail_group_id and editable" t-att-href="'/blog/%%s/new' %% mail_group_id" class="btn">Add a new Blog</a>
|
||||
<li class="nav-header"><a t-att-href="'/blog/%%s/' %% mail_group_id">BLOG ARCHIVE</a></li>
|
||||
<!-- TODO: check qweb iteration -->
|
||||
<li t-foreach="nav_list" t-as="year" class="js_nav_year">
|
||||
<t t-set="year" t-value="nav_list[year]"/>
|
||||
<a href="#"><t t-esc="year['name']"/> <small>(<t t-esc="year['date_count']"/>)</small></a>
|
||||
<ul class="nav nav-list css_nav_month">
|
||||
<t t-foreach="year['months']">
|
||||
<li class="js_nav_month"><a href="#" t-att-data-domain="__domain"><t t-esc="date"/> <small>(<t t-esc="date_count"/>)</small></a>
|
||||
<ul class="nav nav-list"/>
|
||||
</li>
|
||||
</t>
|
||||
</ul>
|
||||
</li>
|
||||
<div class="span4" id="left_column">
|
||||
<a t-if="mail_group_id and editable" t-att-href="'/blog/%%s/new' %% mail_group_id" class="btn">Add a new Blog</a>
|
||||
|
||||
<div id="blog_subscribe">
|
||||
<form action="./subscribe" method="POST" class="form-inline" t-if="not subscribe">
|
||||
<input placeholder="Email Address" type="email" name="email" class="input-medium" t-if="is_public_user"/>
|
||||
<button type="submit" class="btn btn-primary" name="subscribe">Subscribe</button>
|
||||
|
@ -59,14 +47,14 @@
|
|||
<input type="hidden" name="email" t-att-value="subscribe"/>
|
||||
<button type="submit" class="btn" name="unsubscribe">Unsubscribe</button>
|
||||
</form>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="span8" t-if="blog_id">
|
||||
<div class="media">
|
||||
<div class="media-body well">
|
||||
<a href="#" t-att-data-id="blog_id.id" class="pull-right" t-if="editable">
|
||||
<span t-att-class="'label label-success js_unpublish %%s' %% (not blog_id.website_published and 'hidden' or '')">Click to Unpublish</span>
|
||||
<span t-att-class="'label label-important js_publish %%s' %% (blog_id.website_published and 'hidden' or '')">Click to Publish</span>
|
||||
<span t-att-class="'label label-success js_unpublish %%s' %% (not blog_id.website_published and 'hidden' or '')">Unpublish</span>
|
||||
<span t-att-class="'label label-important js_publish %%s' %% (blog_id.website_published and 'hidden' or '')">Publish</span>
|
||||
</a>
|
||||
<h3 t-field="blog_id.subject"/>
|
||||
<div t-field="blog_id.body"/>
|
||||
|
@ -132,5 +120,25 @@
|
|||
</div>
|
||||
</t>
|
||||
</template>
|
||||
|
||||
<template id="blog_history" inherit_id="website_mail.index" inherit_option_id="website_mail.index" name="History">
|
||||
<xpath expr="//div[@id='left_column']" position="inside">
|
||||
<ul class="nav nav-list">
|
||||
<li class="nav-header"><a t-att-href="'/blog/%%s/' %% mail_group_id">BLOG ARCHIVE</a></li>
|
||||
<!-- TODO: check qweb iteration -->
|
||||
<li t-foreach="nav_list" t-as="year" class="js_nav_year">
|
||||
<t t-set="year" t-value="nav_list[year]"/>
|
||||
<a href="#"><t t-esc="year['name']"/> <small>(<t t-esc="year['date_count']"/>)</small></a>
|
||||
<ul class="nav nav-list css_nav_month">
|
||||
<t t-foreach="year['months']">
|
||||
<li class="js_nav_month"><a href="#" t-att-data-domain="__domain"><t t-esc="date"/> <small>(<t t-esc="date_count"/>)</small></a>
|
||||
<ul class="nav nav-list"/>
|
||||
</li>
|
||||
</t>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</xpath>
|
||||
</template>
|
||||
</data>
|
||||
</openerp>
|
||||
|
|
|
@ -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