entity.py
branchtls-sprint
changeset 902 e4de959c76af
parent 717 54b873918b48
child 985 6a25c58a1c23
equal deleted inserted replaced
901:0dcf01eb97a0 902:e4de959c76af
   803         #       important because `vocabfunc` might return a list with
   803         #       important because `vocabfunc` might return a list with
   804         #       couples (label, None) which act as separators. In these
   804         #       couples (label, None) which act as separators. In these
   805         #       cases, it doesn't make sense to sort results afterwards.
   805         #       cases, it doesn't make sense to sort results afterwards.
   806         return vocabfunc(rtype, limit)
   806         return vocabfunc(rtype, limit)
   807             
   807             
   808     def subject_relation_vocabulary(self, rtype, limit=None):
       
   809         """defaut vocabulary method for the given relation, looking for
       
   810         relation's object entities (i.e. self is the subject)
       
   811         """
       
   812         if isinstance(rtype, basestring):
       
   813             rtype = self.schema.rschema(rtype)
       
   814         done = None
       
   815         assert not rtype.is_final(), rtype
       
   816         if self.has_eid():
       
   817             done = set(e.eid for e in getattr(self, str(rtype)))
       
   818         result = []
       
   819         rsetsize = None
       
   820         for objtype in rtype.objects(self.e_schema):
       
   821             if limit is not None:
       
   822                 rsetsize = limit - len(result)
       
   823             result += self.relation_vocabulary(rtype, objtype, 'subject',
       
   824                                                rsetsize, done)
       
   825             if limit is not None and len(result) >= limit:
       
   826                 break
       
   827         return result
       
   828 
       
   829     def object_relation_vocabulary(self, rtype, limit=None):
       
   830         """defaut vocabulary method for the given relation, looking for
       
   831         relation's subject entities (i.e. self is the object)
       
   832         """
       
   833         if isinstance(rtype, basestring):
       
   834             rtype = self.schema.rschema(rtype)
       
   835         done = None
       
   836         if self.has_eid():
       
   837             done = set(e.eid for e in getattr(self, 'reverse_%s' % rtype))
       
   838         result = []
       
   839         rsetsize = None
       
   840         for subjtype in rtype.subjects(self.e_schema):
       
   841             if limit is not None:
       
   842                 rsetsize = limit - len(result)
       
   843             result += self.relation_vocabulary(rtype, subjtype, 'object',
       
   844                                                rsetsize, done)
       
   845             if limit is not None and len(result) >= limit:
       
   846                 break
       
   847         return result
       
   848 
       
   849     def relation_vocabulary(self, rtype, targettype, role,
       
   850                             limit=None, done=None):
       
   851         if done is None:
       
   852             done = set()
       
   853         req = self.req
       
   854         rset = self.unrelated(rtype, targettype, role, limit)
       
   855         res = []
       
   856         for entity in rset.entities():
       
   857             if entity.eid in done:
       
   858                 continue
       
   859             done.add(entity.eid)
       
   860             res.append((entity.view('combobox'), entity.eid))
       
   861         return res
       
   862 
       
   863     def unrelated_rql(self, rtype, targettype, role, ordermethod=None,
   808     def unrelated_rql(self, rtype, targettype, role, ordermethod=None,
   864                       vocabconstraints=True):
   809                       vocabconstraints=True):
   865         """build a rql to fetch `targettype` entities unrelated to this entity
   810         """build a rql to fetch `targettype` entities unrelated to this entity
   866         using (rtype, role) relation
   811         using (rtype, role) relation
   867         """
   812         """