replace adapter by simple selector to ease edit controller overloading; fixes #2042349
authorFlorent Cayré <florent.cayre@gmail.com>
Fri, 28 Oct 2011 11:31:11 +0200
changeset 8034 b07d61090706
parent 8033 2a9764674a74
child 8035 f98012ec7c53
replace adapter by simple selector to ease edit controller overloading; fixes #2042349
doc/3.14.rst
selectors.py
web/views/editcontroller.py
--- a/doc/3.14.rst	Fri Oct 28 10:34:12 2011 +0200
+++ b/doc/3.14.rst	Fri Oct 28 11:31:11 2011 +0200
@@ -76,6 +76,10 @@
   rset, *kwargs)` will be routed to the new `RsetTableView` or to the old
   `TableView` depending on given extra arguments. See #1986413.
 
+* `IEditControlAdapter` has been deprecated in favor of `EditController`
+  overloading, which was made easier by adding dedicated selectors called
+  `match_edited_type` and `match_form_id`
+
 
 Unintrusive API changes
 -----------------------
--- a/selectors.py	Fri Oct 28 10:34:12 2011 +0200
+++ b/selectors.py	Fri Oct 28 11:31:11 2011 +0200
@@ -406,11 +406,11 @@
 class ExpectedValueSelector(Selector):
     """Take a list of expected values as initializer argument and store them
     into the :attr:`expected` set attribute. You may also give a set as single
-    argument, which will be then be referenced as set of expected values,
+    argument, which will then be referenced as set of expected values,
     allowing modifications to the given set to be considered.
 
     You should implement one of :meth:`_values_set(cls, req, **kwargs)` or
-    :meth:`_get_value(cls, req, **kwargs)` method which should respectivly
+    :meth:`_get_value(cls, req, **kwargs)` method which should respectively
     return the set of values or the unique possible value for the given context.
 
     You may also specify a `mode` behaviour as argument, as explained below.
@@ -1505,6 +1505,30 @@
         return frozenset(req.form)
 
 
+class match_edited_type(ExpectedValueSelector):
+    """return non-zero if main edited entity type is the one specified as
+    initializer argument, or is among initializer arguments if `mode` == 'any'.
+    """
+
+    def _values_set(self, cls, req, **kwargs):
+        try:
+            return frozenset((req.form['__type:%s' % req.form['__maineid']],))
+        except KeyError:
+            return frozenset()
+
+
+class match_form_id(ExpectedValueSelector):
+    """return non-zero if request form identifier is the one specified as
+    initializer argument, or is among initializer arguments if `mode` == 'any'.
+    """
+
+    def _values_set(self, cls, req, **kwargs):
+        try:
+            return frozenset((req.form['__form_id'],))
+        except KeyError:
+            return frozenset()
+
+
 class specified_etype_implements(is_instance):
     """Return non-zero score if the entity type specified by an 'etype' key
     searched in (by priority) input context kwargs and request form parameters
--- a/web/views/editcontroller.py	Fri Oct 28 10:34:12 2011 +0200
+++ b/web/views/editcontroller.py	Fri Oct 28 11:31:11 2011 +0200
@@ -21,6 +21,8 @@
 
 from warnings import warn
 
+from logilab.common.deprecation import deprecated
+
 from rql.utils import rqlvar_maker
 
 from cubicweb import Binary, ValidationError, typed_eid
@@ -36,6 +38,13 @@
     __regid__ = 'IEditControl'
     __select__ = is_instance('Any')
 
+    @deprecated()
+    def __init__(self, _cw, **kwargs):
+        warn('[3.14] IEditControlAdapter is deprecated, override EditController'
+             ' using match_edited_type or match_form_id selectors for example.',
+             DeprecationWarning)
+        super(IEditControlAdapter, self).__init__(_cw, **kwargs)
+
     @implements_adapter_compat('IEditControl')
     def after_deletion_path(self):
         """return (path, parameters) which should be used as redirect
@@ -47,7 +56,7 @@
         return str(self.entity.e_schema).lower(), {}
 
     @implements_adapter_compat('IEditControl')
-    def pre_web_edit(self, form):
+    def pre_web_edit(self):
         """callback called by the web editcontroller when an entity will be
         created/modified, to let a chance to do some entity specific stuff.
 
@@ -167,6 +176,13 @@
         entity = self._cw.vreg['etypes'].etype_class(etype)(self._cw)
         entity.eid = valerror_eid(formparams['eid'])
         is_main_entity = self._cw.form.get('__maineid') == formparams['eid']
+        # let a chance to do some entity specific stuff
+        entity.cw_adapt_to('IEditControl').pre_web_edit()
+        # create a rql query from parameters
+        rqlquery = RqlQuery()
+        # process inlined relations at the same time as attributes
+        # this will generate less rql queries and might be useful in
+        # a few dark corners
         if is_main_entity:
             formid = self._cw.form.get('__form_id', 'edition')
         else:
@@ -174,13 +190,6 @@
             # inbetween, use cubicweb standard formid for inlined forms
             formid = 'edition'
         form = self._cw.vreg['forms'].select(formid, self._cw, entity=entity)
-        # let a chance to do some entity specific stuff
-        entity.cw_adapt_to('IEditControl').pre_web_edit(form)
-        # create a rql query from parameters
-        rqlquery = RqlQuery()
-        # process inlined relations at the same time as attributes
-        # this will generate less rql queries and might be useful in
-        # a few dark corners
         eid = form.actual_eid(entity.eid)
         try:
             editedfields = formparams['_cw_entity_fields']