382 warn('%s: rest_path() now take use_ext_eid argument, ' |
382 warn('%s: rest_path() now take use_ext_eid argument, ' |
383 'please update' % self.id, DeprecationWarning) |
383 'please update' % self.id, DeprecationWarning) |
384 kwargs['_restpath'] = self.rest_path() |
384 kwargs['_restpath'] = self.rest_path() |
385 else: |
385 else: |
386 kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid |
386 kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid |
387 return self.build_url(method, **kwargs) |
387 return self.req.build_url(method, **kwargs) |
388 |
388 |
389 def rest_path(self, use_ext_eid=False): |
389 def rest_path(self, use_ext_eid=False): |
390 """returns a REST-like (relative) path for this entity""" |
390 """returns a REST-like (relative) path for this entity""" |
391 mainattr, needcheck = self._rest_attr_info() |
391 mainattr, needcheck = self._rest_attr_info() |
392 etype = str(self.e_schema) |
392 etype = str(self.e_schema) |
680 rset = execute(rql, {'x': self.eid}, 'x') |
680 rset = execute(rql, {'x': self.eid}, 'x') |
681 self.set_related_cache(rtype, role, rset) |
681 self.set_related_cache(rtype, role, rset) |
682 return self.related(rtype, role, limit, entities) |
682 return self.related(rtype, role, limit, entities) |
683 |
683 |
684 def related_rql(self, rtype, role='subject'): |
684 def related_rql(self, rtype, role='subject'): |
685 rschema = self.schema[rtype] |
685 rschema = self.req.vreg.schema[rtype] |
686 if role == 'subject': |
686 if role == 'subject': |
687 targettypes = rschema.objects(self.e_schema) |
687 targettypes = rschema.objects(self.e_schema) |
688 restriction = 'E eid %%(x)s, E %s X' % rtype |
688 restriction = 'E eid %%(x)s, E %s X' % rtype |
689 card = greater_card(rschema, (self.e_schema,), targettypes, 0) |
689 card = greater_card(rschema, (self.e_schema,), targettypes, 0) |
690 else: |
690 else: |
734 """build a rql to fetch `targettype` entities unrelated to this entity |
734 """build a rql to fetch `targettype` entities unrelated to this entity |
735 using (rtype, role) relation |
735 using (rtype, role) relation |
736 """ |
736 """ |
737 ordermethod = ordermethod or 'fetch_unrelated_order' |
737 ordermethod = ordermethod or 'fetch_unrelated_order' |
738 if isinstance(rtype, basestring): |
738 if isinstance(rtype, basestring): |
739 rtype = self.schema.rschema(rtype) |
739 rtype = self.req.vreg.schema.rschema(rtype) |
740 if role == 'subject': |
740 if role == 'subject': |
741 evar, searchedvar = 'S', 'O' |
741 evar, searchedvar = 'S', 'O' |
742 subjtype, objtype = self.e_schema, targettype |
742 subjtype, objtype = self.e_schema, targettype |
743 else: |
743 else: |
744 searchedvar, evar = 'S', 'O' |
744 searchedvar, evar = 'S', 'O' |
799 |
799 |
800 def set_related_cache(self, rtype, role, rset, col=0): |
800 def set_related_cache(self, rtype, role, rset, col=0): |
801 """set cached values for the given relation""" |
801 """set cached values for the given relation""" |
802 if rset: |
802 if rset: |
803 related = list(rset.entities(col)) |
803 related = list(rset.entities(col)) |
804 rschema = self.schema.rschema(rtype) |
804 rschema = self.req.vreg.schema.rschema(rtype) |
805 if role == 'subject': |
805 if role == 'subject': |
806 rcard = rschema.rproperty(self.e_schema, related[0].e_schema, |
806 rcard = rschema.rproperty(self.e_schema, related[0].e_schema, |
807 'cardinality')[1] |
807 'cardinality')[1] |
808 target = 'object' |
808 target = 'object' |
809 else: |
809 else: |