633 def rest_path(self, use_ext_eid=False): # XXX cw_rest_path |
633 def rest_path(self, use_ext_eid=False): # XXX cw_rest_path |
634 """returns a REST-like (relative) path for this entity""" |
634 """returns a REST-like (relative) path for this entity""" |
635 mainattr, needcheck = self.cw_rest_attr_info() |
635 mainattr, needcheck = self.cw_rest_attr_info() |
636 etype = str(self.e_schema) |
636 etype = str(self.e_schema) |
637 path = etype.lower() |
637 path = etype.lower() |
|
638 fallback = False |
638 if mainattr != 'eid': |
639 if mainattr != 'eid': |
639 value = getattr(self, mainattr) |
640 value = getattr(self, mainattr) |
640 if not can_use_rest_path(value): |
641 if not can_use_rest_path(value): |
641 mainattr = 'eid' |
642 mainattr = 'eid' |
642 path += '/eid' |
643 path = None |
643 elif needcheck: |
644 elif needcheck: |
644 # make sure url is not ambiguous |
645 # make sure url is not ambiguous |
645 try: |
646 try: |
646 nbresults = self.__unique |
647 nbresults = self.__unique |
647 except AttributeError: |
648 except AttributeError: |
648 rql = 'Any COUNT(X) WHERE X is %s, X %s %%(value)s' % ( |
649 rql = 'Any COUNT(X) WHERE X is %s, X %s %%(value)s' % ( |
649 etype, mainattr) |
650 etype, mainattr) |
650 nbresults = self.__unique = self._cw.execute(rql, {'value' : value})[0][0] |
651 nbresults = self.__unique = self._cw.execute(rql, {'value' : value})[0][0] |
651 if nbresults != 1: # ambiguity? |
652 if nbresults != 1: # ambiguity? |
652 mainattr = 'eid' |
653 mainattr = 'eid' |
653 path += '/eid' |
654 path = None |
654 if mainattr == 'eid': |
655 if mainattr == 'eid': |
655 if use_ext_eid: |
656 if use_ext_eid: |
656 value = self.cw_metainformation()['extid'] |
657 value = self.cw_metainformation()['extid'] |
657 else: |
658 else: |
658 value = self.eid |
659 value = self.eid |
|
660 if path is None: |
|
661 # fallback url: <base-url>/<eid> url is used as cw entities uri, |
|
662 # prefer it to <base-url>/<etype>/eid/<eid> |
|
663 return unicode(value) |
659 return '%s/%s' % (path, self._cw.url_quote(value)) |
664 return '%s/%s' % (path, self._cw.url_quote(value)) |
660 |
665 |
661 def cw_attr_metadata(self, attr, metadata): |
666 def cw_attr_metadata(self, attr, metadata): |
662 """return a metadata for an attribute (None if unspecified)""" |
667 """return a metadata for an attribute (None if unspecified)""" |
663 value = getattr(self, '%s_%s' % (attr, metadata), None) |
668 value = getattr(self, '%s_%s' % (attr, metadata), None) |