[selectors] fix match_transition & introduce new edited_attribute selector stable
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Thu, 12 Aug 2010 16:50:52 +0200
branchstable
changeset 6106 1e6d93f70d14
parent 6105 a648c832da1f
child 6107 05061eda1fa7
[selectors] fix match_transition & introduce new edited_attribute selector
selectors.py
uilib.py
web/__init__.py
--- a/selectors.py	Thu Aug 12 15:58:42 2010 +0200
+++ b/selectors.py	Thu Aug 12 16:50:52 2010 +0200
@@ -201,12 +201,13 @@
 from logilab.common.compat import all, any
 from logilab.common.interface import implements as implements_iface
 
-from yams import BASE_TYPES
+from yams.schema import BASE_TYPES, role_name
 from rql.nodes import Function
 
 from cubicweb import (Unauthorized, NoSelectableObject, NotAnEntity,
                       CW_EVENT_MANAGER, role)
 # even if not used, let yes here so it's importable through this module
+from cubicweb.uilib import eid_param
 from cubicweb.appobject import Selector, objectify_selector, lltrace, yes
 from cubicweb.schema import split_expression
 
@@ -792,6 +793,21 @@
             return 1
         self.score_entity = intscore
 
+class attribute_edited(EntitySelector):
+    """Scores if the specified attribute has been edited
+    This is useful for selection of forms by the edit controller.
+    The initial use case is on a form, in conjunction with match_transition,
+    which will not score at edit time::
+
+     is_instance('Version') & (match_transition('ready') |
+                               attribute_edited('publication_date'))
+    """
+    def __init__(self, attribute, once_is_enough=False):
+        super(attribute_edited, self).__init__(once_is_enough)
+        self._attribute = attribute
+
+    def score_entity(self, entity):
+        return eid_param(role_name(self._attribute, 'subject'), entity.eid) in entity._cw.form
 
 class has_mimetype(EntitySelector):
     """Return 1 if the entity adapt to IDownloadable and has the given MIME type.
@@ -1324,19 +1340,13 @@
 
 
 class match_transition(ExpectedValueSelector):
-    """Return 1 if:
-
-    * a `transition` argument is found in the input context which
-      has a `.name` attribute matching one of the expected names given to the
-      initializer
-
-    * no transition specified.
+    """Return 1 if `transition` argument is found in the input context
+      which has a `.name` attribute matching one of the expected names
+      given to the initializer
     """
     @lltrace
     def __call__(self, cls, req, transition=None, **kwargs):
         # XXX check this is a transition that apply to the object?
-        if transition is None:
-            return 1
         if transition is not None and getattr(transition, 'name', None) in self.expected:
             return 1
         return 0
--- a/uilib.py	Thu Aug 12 15:58:42 2010 +0200
+++ b/uilib.py	Thu Aug 12 16:50:52 2010 +0200
@@ -46,6 +46,11 @@
     """
     return 'Any X WHERE X eid %s' % eid
 
+def eid_param(name, eid):
+    assert eid is not None
+    if eid is None:
+        eid = ''
+    return '%s:%s' % (name, eid)
 
 def printable_value(req, attrtype, value, props=None, displaytime=True):
     """return a displayable value (i.e. unicode string)"""
--- a/web/__init__.py	Thu Aug 12 15:58:42 2010 +0200
+++ b/web/__init__.py	Thu Aug 12 16:50:52 2010 +0200
@@ -28,6 +28,7 @@
 
 from cubicweb.web._exceptions import *
 from cubicweb.utils import json_dumps
+from cubicweb.uilib import eid_param
 
 dumps = deprecated('[3.9] use cubicweb.utils.json_dumps instead of dumps')(json_dumps)
 
@@ -44,13 +45,6 @@
     NO  = (_('no'), None)
 
 
-def eid_param(name, eid):
-    assert eid is not None
-    if eid is None:
-        eid = ''
-    return '%s:%s' % (name, eid)
-
-
 from logging import getLogger
 LOGGER = getLogger('cubicweb.web')