cubicweb/entity.py
changeset 11759 7b7108eb8178
parent 11729 809407cbff11
child 11762 7518cb58ab4c
equal deleted inserted replaced
11758:3f81636a75db 11759:7b7108eb8178
   653         if args:
   653         if args:
   654             assert len(args) == 1, 'only 0 or 1 non-named-argument expected'
   654             assert len(args) == 1, 'only 0 or 1 non-named-argument expected'
   655             method = args[0]
   655             method = args[0]
   656         else:
   656         else:
   657             method = None
   657             method = None
   658         # in linksearch mode, we don't want external urls else selecting
       
   659         # the object for use in the relation is tricky
       
   660         # XXX search_state is web specific
       
   661         use_ext_id = False
       
   662         if 'base_url' not in kwargs and \
       
   663                getattr(self._cw, 'search_state', ('normal',))[0] == 'normal':
       
   664             sourcemeta = self.cw_metainformation()['source']
       
   665             if sourcemeta.get('use-cwuri-as-url'):
       
   666                 return self.cwuri # XXX consider kwargs?
       
   667             if sourcemeta.get('base-url'):
       
   668                 kwargs['base_url'] = sourcemeta['base-url']
       
   669                 use_ext_id = True
       
   670         if method in (None, 'view'):
   658         if method in (None, 'view'):
   671             kwargs['_restpath'] = self.rest_path(use_ext_id)
   659             kwargs['_restpath'] = self.rest_path()
   672         else:
   660         else:
   673             kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid
   661             kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid
   674         return self._cw.build_url(method, **kwargs)
   662         return self._cw.build_url(method, **kwargs)
   675 
   663 
   676     def rest_path(self, use_ext_eid=False): # XXX cw_rest_path
   664     def rest_path(self, *args, **kwargs): # XXX cw_rest_path
   677         """returns a REST-like (relative) path for this entity"""
   665         """returns a REST-like (relative) path for this entity"""
       
   666         if args or kwargs:
       
   667             warn("[3.24] rest_path doesn't take parameters anymore", DeprecationWarning)
   678         mainattr, needcheck = self.cw_rest_attr_info()
   668         mainattr, needcheck = self.cw_rest_attr_info()
   679         etype = str(self.e_schema)
   669         etype = str(self.e_schema)
   680         path = etype.lower()
   670         path = etype.lower()
   681         fallback = False
   671         fallback = False
   682         if mainattr != 'eid':
   672         if mainattr != 'eid':
   694                     nbresults = self.__unique = self._cw.execute(rql, {'value' : value})[0][0]
   684                     nbresults = self.__unique = self._cw.execute(rql, {'value' : value})[0][0]
   695                 if nbresults != 1: # ambiguity?
   685                 if nbresults != 1: # ambiguity?
   696                     mainattr = 'eid'
   686                     mainattr = 'eid'
   697                     path = None
   687                     path = None
   698         if mainattr == 'eid':
   688         if mainattr == 'eid':
   699             if use_ext_eid:
   689             value = self.eid
   700                 value = self.cw_metainformation()['extid']
       
   701             else:
       
   702                 value = self.eid
       
   703         if path is None:
   690         if path is None:
   704             # fallback url: <base-url>/<eid> url is used as cw entities uri,
   691             # fallback url: <base-url>/<eid> url is used as cw entities uri,
   705             # prefer it to <base-url>/<etype>/eid/<eid>
   692             # prefer it to <base-url>/<etype>/eid/<eid>
   706             return text_type(value)
   693             return text_type(value)
   707         return u'%s/%s' % (path, self._cw.url_quote(value))
   694         return u'%s/%s' % (path, self._cw.url_quote(value))
   887             var = next(varmaker)
   874             var = next(varmaker)
   888             rql.append('%s %s %s' % (V, attr, var))
   875             rql.append('%s %s %s' % (V, attr, var))
   889             selected.append((attr, var))
   876             selected.append((attr, var))
   890         # +1 since this doesn't include the main variable
   877         # +1 since this doesn't include the main variable
   891         lastattr = len(selected) + 1
   878         lastattr = len(selected) + 1
   892         # don't fetch extra relation if attributes specified or of the entity is
   879         if attributes is None:
   893         # coming from an external source (may lead to error)
       
   894         if attributes is None and self.cw_metainformation()['source']['uri'] == 'system':
       
   895             # fetch additional relations (restricted to 0..1 relations)
   880             # fetch additional relations (restricted to 0..1 relations)
   896             for rschema, role in self._cw_to_complete_relations():
   881             for rschema, role in self._cw_to_complete_relations():
   897                 rtype = rschema.type
   882                 rtype = rschema.type
   898                 if self.cw_relation_cached(rtype, role):
   883                 if self.cw_relation_cached(rtype, role):
   899                     continue
   884                     continue