selectors.py
branchtls-sprint
changeset 784 33db07c66789
parent 779 8510e14335e1
parent 782 01801a10c567
child 788 d62fb3e9797d
--- a/selectors.py	Wed Feb 18 13:44:23 2009 +0100
+++ b/selectors.py	Wed Feb 18 13:44:35 2009 +0100
@@ -113,6 +113,13 @@
 
 
 # abstract selectors ##########################################################
+class AbstractSelectorMixIn(object):
+    """convenience mix-in for selectors that depends on class attributes
+    cf. `cubicweb.web.action.LinkToEntityAction` for instance
+    """
+    def __call__(self, cls, *args, **kwargs):
+        self.concretize(cls)
+        super(AbstractSelectorMixIn, self).__call__(cls, *args, **kwargs)
 
 class EClassSelector(Selector):
     """abstract class for selectors working on the entity classes of the result
@@ -309,6 +316,8 @@
     """accept result set where entities in the specified column (or 0) are all
     of the same type
     """
+    if rset is None:
+        return 0
     if len(rset.column_types(col)) != 1:
         return 0
     return 1
@@ -612,6 +621,15 @@
                 return 0
         return 1
 
+class abstract_relation_possible(AbstractSelectorMixIn, relation_possible):
+    def __init__(self, action='read', once_is_enough=False):
+        super(abstract_relation_possible, self).__init__(None, None, None,
+                                                         action, once_is_enough)
+
+    def concretize(self, cls):
+        self.rtype = cls.rtype
+        self.role = role(cls)
+        self.target_etype = getattr(cls, 'etype', None)
 
 class has_editable_relation(EntitySelector):
     """accept if some relations for an entity found in the result set is
@@ -658,7 +676,15 @@
             return 0
         return 1
 
+class abstract_may_add_relation(AbstractSelectorMixIn, may_add_relation):
+    def __init__(self, once_is_enough=False):
+        super(abstract_may_add_relation, self).__init__(None, None, once_is_enough)
 
+    def concretize(self, cls):
+        self.rtype = cls.rtype
+        self.role = role(cls)
+
+    
 class has_related_entities(EntitySelector):
     """accept if entity found in the result set has some linked entities using
     the specified relation (optionaly filtered according to the specified target
@@ -685,7 +711,16 @@
             return any(x for x, in rset.description if x == self.target_etype)
         return bool(rset)
 
-        
+class abstract_has_related_entities(AbstractSelectorMixIn, has_related_entities):
+    def __init__(self, once_is_enough=False):
+        super(abstract_has_related_entities, self).__init__(None, None,
+                                                            None, once_is_enough)
+    def concretize(self, cls):
+        self.rtype = cls.rtype
+        self.role = role(cls)
+        self.target_etype = getattr(cls, 'etype', None)
+
+
 class has_permission(EntitySelector):
     """accept if user has the permission to do the requested action on a result
     set entity.