[entity] introduce a new 'adapters' registry
This changeset introduces the notion in adapters (as in Zope Component Architecture)
in a cubicweb way, eg using a specific registry of appobjects.
This allows nicer code structure, by avoid clutering entity classes and moving
code usually specific to a place of the ui (or something else) together with the
code that use the interface.
We don't use actual interface anymore, they are implied by adapters (which
may be abstract), whose reg id is an interface name.
Appobjects that used to 'implements(IFace)' should now be rewritten by:
* coding an IFaceAdapter(EntityAdapter) defining (implementing if desired)
the interface, usually with __regid__ = 'IFace'
* use "adaptable('IFace')" as selector instead
Also, the implements_adapter_compat decorator eases backward compatibility
with adapter's methods that may still be found on entities implementing
the interface.
Notice that unlike ZCA, we don't support automatic adapters chain (yagni?).
All interfaces defined in cubicweb have been turned into adapters, also
some new ones have been introduced to cleanup Entity / AnyEntity classes
namespace. At the end, the pluggable mixins mecanism should disappear in
favor of adapters as well.
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
"""abstract action classes for CubicWeb web client"""
__docformat__ = "restructuredtext en"
_ = unicode
from cubicweb import target
from cubicweb.selectors import (partial_relation_possible, match_search_state,
one_line_rset)
from cubicweb.appobject import AppObject
class Action(AppObject):
"""abstract action. Handle the .search_states attribute to match
request search state.
"""
__registry__ = 'actions'
__select__ = match_search_state('normal')
order = 99
category = 'moreactions'
# actions in category 'moreactions' can specify a sub-menu in which they should be filed
submenu = None
def actual_actions(self):
yield self
def fill_menu(self, box, menu):
"""add action(s) to the given submenu of the given box"""
for action in self.actual_actions():
menu.append(box.box_action(action))
def url(self):
"""return the url associated with this action"""
raise NotImplementedError
def html_class(self):
if self._cw.selected(self.url()):
return 'selected'
if self.category:
return 'box' + self.category.capitalize()
def build_action(self, title, path, **kwargs):
return UnregisteredAction(self._cw, self.cw_rset, title, path, **kwargs)
class UnregisteredAction(Action):
"""non registered action used to build boxes. Unless you set them
explicitly, .vreg and .schema attributes at least are None.
"""
category = None
id = None
def __init__(self, req, rset, title, path, **kwargs):
Action.__init__(self, req, rset=rset)
self.title = req._(title)
self._path = path
self.__dict__.update(kwargs)
def url(self):
return self._path
class LinkToEntityAction(Action):
"""base class for actions consisting to create a new object with an initial
relation set to an entity.
Additionaly to EntityAction behaviour, this class is parametrized using
.rtype, .role and .target_etype attributes to check if the action apply and
if the logged user has access to it (see
:class:`~cubicweb.selectors.partial_relation_possible` selector
documentation for more information).
"""
__select__ = (match_search_state('normal') & one_line_rset()
& partial_relation_possible(action='add', strict=True))
submenu = 'addrelated'
def url(self):
try:
ttype = self.etype # deprecated in 3.6, already warned by the selector
except AttributeError:
ttype = self.target_etype
entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
linkto = '%s:%s:%s' % (self.rtype, entity.eid, target(self))
return self._cw.build_url('add/%s' % ttype, __linkto=linkto,
__redirectpath=entity.rest_path(),
__redirectvid=self._cw.form.get('__redirectvid', ''))