selectors.py
branchstable
changeset 6106 1e6d93f70d14
parent 6046 3fd4a34c4a09
child 6120 c000e41316ec
equal deleted inserted replaced
6105:a648c832da1f 6106:1e6d93f70d14
   199 
   199 
   200 from logilab.common.deprecation import class_renamed
   200 from logilab.common.deprecation import class_renamed
   201 from logilab.common.compat import all, any
   201 from logilab.common.compat import all, any
   202 from logilab.common.interface import implements as implements_iface
   202 from logilab.common.interface import implements as implements_iface
   203 
   203 
   204 from yams import BASE_TYPES
   204 from yams.schema import BASE_TYPES, role_name
   205 from rql.nodes import Function
   205 from rql.nodes import Function
   206 
   206 
   207 from cubicweb import (Unauthorized, NoSelectableObject, NotAnEntity,
   207 from cubicweb import (Unauthorized, NoSelectableObject, NotAnEntity,
   208                       CW_EVENT_MANAGER, role)
   208                       CW_EVENT_MANAGER, role)
   209 # even if not used, let yes here so it's importable through this module
   209 # even if not used, let yes here so it's importable through this module
       
   210 from cubicweb.uilib import eid_param
   210 from cubicweb.appobject import Selector, objectify_selector, lltrace, yes
   211 from cubicweb.appobject import Selector, objectify_selector, lltrace, yes
   211 from cubicweb.schema import split_expression
   212 from cubicweb.schema import split_expression
   212 
   213 
   213 from cubicweb.appobject import traced_selection # XXX for bw compat
   214 from cubicweb.appobject import traced_selection # XXX for bw compat
   214 
   215 
   790             if isinstance(score, (int, long)):
   791             if isinstance(score, (int, long)):
   791                 return score
   792                 return score
   792             return 1
   793             return 1
   793         self.score_entity = intscore
   794         self.score_entity = intscore
   794 
   795 
       
   796 class attribute_edited(EntitySelector):
       
   797     """Scores if the specified attribute has been edited
       
   798     This is useful for selection of forms by the edit controller.
       
   799     The initial use case is on a form, in conjunction with match_transition,
       
   800     which will not score at edit time::
       
   801 
       
   802      is_instance('Version') & (match_transition('ready') |
       
   803                                attribute_edited('publication_date'))
       
   804     """
       
   805     def __init__(self, attribute, once_is_enough=False):
       
   806         super(attribute_edited, self).__init__(once_is_enough)
       
   807         self._attribute = attribute
       
   808 
       
   809     def score_entity(self, entity):
       
   810         return eid_param(role_name(self._attribute, 'subject'), entity.eid) in entity._cw.form
   795 
   811 
   796 class has_mimetype(EntitySelector):
   812 class has_mimetype(EntitySelector):
   797     """Return 1 if the entity adapt to IDownloadable and has the given MIME type.
   813     """Return 1 if the entity adapt to IDownloadable and has the given MIME type.
   798 
   814 
   799     You can give 'image/' to match any image for instance, or 'image/png' to match
   815     You can give 'image/' to match any image for instance, or 'image/png' to match
  1322 
  1338 
  1323 # Other selectors ##############################################################
  1339 # Other selectors ##############################################################
  1324 
  1340 
  1325 
  1341 
  1326 class match_transition(ExpectedValueSelector):
  1342 class match_transition(ExpectedValueSelector):
  1327     """Return 1 if:
  1343     """Return 1 if `transition` argument is found in the input context
  1328 
  1344       which has a `.name` attribute matching one of the expected names
  1329     * a `transition` argument is found in the input context which
  1345       given to the initializer
  1330       has a `.name` attribute matching one of the expected names given to the
       
  1331       initializer
       
  1332 
       
  1333     * no transition specified.
       
  1334     """
  1346     """
  1335     @lltrace
  1347     @lltrace
  1336     def __call__(self, cls, req, transition=None, **kwargs):
  1348     def __call__(self, cls, req, transition=None, **kwargs):
  1337         # XXX check this is a transition that apply to the object?
  1349         # XXX check this is a transition that apply to the object?
  1338         if transition is None:
       
  1339             return 1
       
  1340         if transition is not None and getattr(transition, 'name', None) in self.expected:
  1350         if transition is not None and getattr(transition, 'name', None) in self.expected:
  1341             return 1
  1351             return 1
  1342         return 0
  1352         return 0
  1343 
  1353 
  1344 class is_in_state(score_entity):
  1354 class is_in_state(score_entity):