selectors.py
changeset 4234 0adf45697422
parent 4181 c79135c217df
child 4252 6c4f109c2b03
equal deleted inserted replaced
4233:94ffaecd8e8c 4234:0adf45697422
   716     :param rtype: a relation type (`basestring`)
   716     :param rtype: a relation type (`basestring`)
   717     :param role: the role of the result set entity in the relation. 'subject' or
   717     :param role: the role of the result set entity in the relation. 'subject' or
   718                  'object', default to 'subject'.
   718                  'object', default to 'subject'.
   719     """
   719     """
   720 
   720 
   721     def __init__(self, rtype, role='subject', once_is_enough=False):
   721     def __init__(self, rtype, role='subject', target_etype=None,
       
   722                  once_is_enough=False):
   722         super(may_add_relation, self).__init__(once_is_enough)
   723         super(may_add_relation, self).__init__(once_is_enough)
   723         self.rtype = rtype
   724         self.rtype = rtype
   724         self.role = role
   725         self.role = role
       
   726         self.target_etype = target_etype
   725 
   727 
   726     def score_entity(self, entity):
   728     def score_entity(self, entity):
   727         rschema = entity.schema.rschema(self.rtype)
   729         rschema = entity.schema.rschema(self.rtype)
       
   730         if self.target_etype is not None:
       
   731             rschema = rschema.role_rdef(entity.e_schema, self.target_etype, self.role)
   728         if self.role == 'subject':
   732         if self.role == 'subject':
   729             if not rschema.has_perm(entity._cw, 'add', fromeid=entity.eid):
   733             if not rschema.has_perm(entity._cw, 'add', fromeid=entity.eid):
   730                 return 0
   734                 return 0
   731         elif not rschema.has_perm(entity._cw, 'add', toeid=entity.eid):
   735         elif not rschema.has_perm(entity._cw, 'add', toeid=entity.eid):
   732             return 0
   736             return 0
   748     :param action: a relation schema action (one of 'read', 'add', 'delete')
   752     :param action: a relation schema action (one of 'read', 'add', 'delete')
   749                    which must be granted to the logged user, else a 0 score will
   753                    which must be granted to the logged user, else a 0 score will
   750                    be returned
   754                    be returned
   751     """
   755     """
   752     def __init__(self, once_is_enough=False):
   756     def __init__(self, once_is_enough=False):
   753         super(partial_may_add_relation, self).__init__(None, None, once_is_enough)
   757         super(partial_may_add_relation, self).__init__(None, once_is_enough=once_is_enough)
   754 
   758 
   755     def complete(self, cls):
   759     def complete(self, cls):
   756         self.rtype = cls.rtype
   760         self.rtype = cls.rtype
   757         self.role = role(cls)
   761         self.role = role(cls)
       
   762         self.target_etype = getattr(cls, 'etype', None)
   758 
   763 
   759 
   764 
   760 class has_related_entities(EntitySelector):
   765 class has_related_entities(EntitySelector):
   761     """accept if entity found in the result set has some linked entities using
   766     """accept if entity found in the result set has some linked entities using
   762     the specified relation (optionaly filtered according to the specified target
   767     the specified relation (optionaly filtered according to the specified target