Commit 5bde6bf4 authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

3.8 to 3.10 cw api update

parent 379ee1b67d49
BASKET_ICON = DATADIR/icon_basket.gif
BASKET_ICON = data('icon_basket.gif')
......@@ -11,9 +11,8 @@ _ = unicode
from logilab.mtconverter import xml_escape
from cubicweb import Unauthorized
from cubicweb.selectors import is_instance
from cubicweb.web import uicfg, box, action
from cubicweb.web.htmlwidgets import BoxWidget
from cubicweb.selectors import is_instance, none_rset, non_final_entity
from cubicweb.web import uicfg, box, action, component
from cubicweb.web.views import primary
# displayed by the basket box
......@@ -67,89 +66,96 @@ def insert_eids(actions, eids):
class BasketBox(box.UserRQLBoxTemplate):
class BasketBox(component.RQLCtxComponent):
"""display a box containing all user's basketitems"""
__regid__ = 'basket_box'
visible = False # disabled by default
__select__ = component.RQLCtxComponent.__select__ & (
none_rset() | non_final_entity())
# XXX disabled by default. One should remove the basket cube if he doesn't
# want the basket box, or deactivate it explicitly
visible = False
order = 30
title = _('basket')
rql = 'Any B,N where B is Basket,B owned_by U, U eid %(x)s, B name N'
etype = 'Basket'
rtype = 'in_basket'
def to_display_rql(self):
return (self.rql, {'x': self._cw.user.eid})
def init_rendering(self):
super(BasketBox, self).init_rendering()
req = self._cw
try:
baskets = req.execute(*self.to_display_rql())
except Unauthorized:
# can't access to something in the query, forget this box
raise component.EmptyComponent()
rschema = req.vreg.schema.rschema(self.rtype)
selectedeids = self.selected_eids()
for basket in baskets.entities():
rset, inbasketeids = self.build_inbasket_link(basket)
if selectedeids and rschema.has_perm(req, 'add'):
addable = [str(eid) for eid in selectedeids if not eid in inbasketeids]
if addable:
self.build_add_link(basket, addable)
if not rset:
continue
if rschema.has_perm(req, 'delete'):
self.build_delete_link(basket, inbasketeids)
eschema = req.vreg.schema.eschema(self.etype)
if eschema.has_perm(req, 'add'):
url = req.vreg['etypes'].etype_class(self.etype).cw_create_url(req)
self.append(self.build_link(req._('create basket'), url,
klass='manage'))
if not self.items:
raise component.EmptyComponent()
def render_body(self, w):
self.render_items(w)
def selected_eids(self):
selectedeids = ()
if self.cw_rset:
# something is being displayed. If the first column contains eids,
# fetch them so we can propose to add the selection to the basket
etype = iter(self.cw_rset.column_types(0)).next()
if not self._cw.vreg.schema.eschema(etype).final:
selectedeids = set(row[0] for row in self.cw_rset.rows)
selectedeids = set(row[0] for row in self.cw_rset.rows)
return selectedeids
def build_inbasket_link(self, box, basket):
def build_inbasket_link(self, basket):
rset = basket.related('in_basket', 'object')
title = u'%s <span class="basketName">%s</span> (%s)' % (
_('view basket'), xml_escape(basket.name), len(rset))
box.append(self.mk_action(title, basket.absolute_url(),
escape=False))
self.append(self.build_link(title, basket.absolute_url(),
escapecontent=False))
return rset, [row[0] for row in rset]
def build_add_link(self, box, basket, addable, rql, vid):
def build_add_link(self, basket, addable):
title = u'%s <span class="basketName">%s</span>' % (
_('add to basket'), xml_escape(basket.name))
linkto = u'in_basket:%s:object' % '_'.join(addable)
msg = _('selection added to basket')
msg = self._cw._('selection added to basket')
rql = self._cw.form.get('rql') or kwargs.get('rql') or ''
vid = self._cw.form.get('vid', '')
url = self._cw.build_url('edit', eid=basket.eid, rql=rql,
__linkto=linkto, __message=msg,
__redirectrql=rql, __redirect_vid=vid)
box.append(self.mk_action(title, url, category='manage',
escape=False))
self.append(self.build_link(title, url, klass='manage',
escapecontent=False))
def build_delete_link(self, box, basket, inbasketeids, rql, vid):
def build_delete_link(self, basket, inbasketeids):
title = '%s <span class="basketName">%s</span>' % (
_('reset basket'), xml_escape(basket.name))
delete = '%s:in_basket:%s' % ('_'.join(str(eid) for eid in inbasketeids),
basket.eid)
msg = _('Basket %s emptied') % basket.name
msg = self._cw._('Basket %s emptied') % basket.name
rql = self._cw.form.get('rql') or kwargs.get('rql') or ''
vid = self._cw.form.get('vid', '')
url = self._cw.build_url('edit', rql=rql, __delete=delete,
__message=msg, __redirectrql=rql,
__redirect_vid=vid)
box.append(self.mk_action(title, url, category='manage'))
def call(self, **kwargs):
try:
baskets = self._cw.execute(*self.to_display_rql())
except Unauthorized:
# can't access to something in the query, forget this box
return
_ = self._cw._
req = self._cw
box = BoxWidget(_(self.title), self.__regid__)
rschema = self._cw.vreg.schema.rschema(self.rtype)
actions = []
rql = req.form.get('rql') or kwargs.get('rql') or ''
vid = req.form.get('vid', '')
selectedeids = self.selected_eids()
itemsrql = 'Any X WHERE X in_basket B, B eid %(x)s'
for basket in baskets.entities():
rset, inbasketeids = self.build_inbasket_link(box, basket)
if selectedeids and rschema.has_perm(req, 'add'):
addable = [str(eid) for eid in selectedeids if not eid in inbasketeids]
if addable:
self.build_add_link(box, basket, addable, rql, vid)
if not rset:
continue
if rschema.has_perm(req, 'delete'):
self.build_delete_link(box, basket, inbasketeids, rql, vid)
eschema = self._cw.vreg.schema.eschema(self.etype)
if eschema.has_perm(req, 'add'):
actions.append(self.mk_action(_('create basket'),
self.create_url(self.etype),
category='manage'))
if not box.is_empty():
box.render(self.w)
self.append(self.build_link(title, url, klass='manage',
escapecontent=False))
class SelectBasketContentAction(action.Action):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment