entity.py
changeset 7828 749e1a67987f
parent 7827 9bbf83f68bcc
child 7876 df15d194a134
equal deleted inserted replaced
7827:9bbf83f68bcc 7828:749e1a67987f
   343                             select.add_sort_var(select.get_variable(var),
   343                             select.add_sort_var(select.get_variable(var),
   344                                                 order=='ASC')
   344                                                 order=='ASC')
   345 
   345 
   346     @classmethod
   346     @classmethod
   347     @cached
   347     @cached
   348     def _rest_attr_info(cls):
   348     def cw_rest_attr_info(cls):
       
   349         """this class method return an attribute name to be used in URL for
       
   350         entities of this type and a boolean flag telling if its value should be
       
   351         checked for uniqness.
       
   352 
       
   353         The attribute returned is, in order of priority:
       
   354 
       
   355         * class's `rest_attr` class attribute
       
   356         * an attribute defined as unique in the class'schema
       
   357         * 'eid'
       
   358         """
   349         mainattr, needcheck = 'eid', True
   359         mainattr, needcheck = 'eid', True
   350         if cls.rest_attr:
   360         if cls.rest_attr:
   351             mainattr = cls.rest_attr
   361             mainattr = cls.rest_attr
   352             needcheck = not cls.e_schema.has_unique_values(mainattr)
   362             needcheck = not cls.e_schema.has_unique_values(mainattr)
   353         else:
   363         else:
   354             for rschema in cls.e_schema.subject_relations():
   364             for rschema in cls.e_schema.subject_relations():
   355                 if rschema.final and rschema != 'eid' and cls.e_schema.has_unique_values(rschema):
   365                 if rschema.final and rschema != 'eid' \
       
   366                         and cls.e_schema.has_unique_values(rschema):
   356                     mainattr = str(rschema)
   367                     mainattr = str(rschema)
   357                     needcheck = False
   368                     needcheck = False
   358                     break
   369                     break
   359         if mainattr == 'eid':
   370         if mainattr == 'eid':
   360             needcheck = False
   371             needcheck = False
   542             kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid
   553             kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid
   543         return self._cw.build_url(method, **kwargs)
   554         return self._cw.build_url(method, **kwargs)
   544 
   555 
   545     def rest_path(self, use_ext_eid=False): # XXX cw_rest_path
   556     def rest_path(self, use_ext_eid=False): # XXX cw_rest_path
   546         """returns a REST-like (relative) path for this entity"""
   557         """returns a REST-like (relative) path for this entity"""
   547         mainattr, needcheck = self._rest_attr_info()
   558         mainattr, needcheck = self.cw_rest_attr_info()
   548         etype = str(self.e_schema)
   559         etype = str(self.e_schema)
   549         path = etype.lower()
   560         path = etype.lower()
   550         if mainattr != 'eid':
   561         if mainattr != 'eid':
   551             value = getattr(self, mainattr)
   562             value = getattr(self, mainattr)
   552             if not can_use_rest_path(value):
   563             if not can_use_rest_path(value):