entity.py
changeset 2829 054a8805da52
parent 2827 d1a89d165045
child 2830 b0b26b029d08
equal deleted inserted replaced
2828:306fe72bfd07 2829:054a8805da52
    62     """
    62     """
    63     __registry__ = 'etypes'
    63     __registry__ = 'etypes'
    64     __select__ = yes()
    64     __select__ = yes()
    65 
    65 
    66     # class attributes that must be set in class definition
    66     # class attributes that must be set in class definition
    67     id = None
       
    68     rest_attr = None
    67     rest_attr = None
    69     fetch_attrs = None
    68     fetch_attrs = None
    70     skip_copy_for = ()
    69     skip_copy_for = ()
    71     # class attributes set automatically at registration time
    70     # class attributes set automatically at registration time
    72     e_schema = None
    71     e_schema = None
    74     @classmethod
    73     @classmethod
    75     def __initialize__(cls, schema):
    74     def __initialize__(cls, schema):
    76         """initialize a specific entity class by adding descriptors to access
    75         """initialize a specific entity class by adding descriptors to access
    77         entity type's attributes and relations
    76         entity type's attributes and relations
    78         """
    77         """
    79         etype = cls.id
    78         etype = cls.__id__
    80         assert etype != 'Any', etype
    79         assert etype != 'Any', etype
    81         cls.e_schema = eschema = schema.eschema(etype)
    80         cls.e_schema = eschema = schema.eschema(etype)
    82         for rschema, _ in eschema.attribute_definitions():
    81         for rschema, _ in eschema.attribute_definitions():
    83             if rschema.type == 'eid':
    82             if rschema.type == 'eid':
    84                 continue
    83                 continue
   105     def fetch_rql(cls, user, restriction=None, fetchattrs=None, mainvar='X',
   104     def fetch_rql(cls, user, restriction=None, fetchattrs=None, mainvar='X',
   106                   settype=True, ordermethod='fetch_order'):
   105                   settype=True, ordermethod='fetch_order'):
   107         """return a rql to fetch all entities of the class type"""
   106         """return a rql to fetch all entities of the class type"""
   108         restrictions = restriction or []
   107         restrictions = restriction or []
   109         if settype:
   108         if settype:
   110             restrictions.append('%s is %s' % (mainvar, cls.id))
   109             restrictions.append('%s is %s' % (mainvar, cls.__id__))
   111         if fetchattrs is None:
   110         if fetchattrs is None:
   112             fetchattrs = cls.fetch_attrs
   111             fetchattrs = cls.fetch_attrs
   113         selection = [mainvar]
   112         selection = [mainvar]
   114         orderby = []
   113         orderby = []
   115         # start from 26 to avoid possible conflicts with X
   114         # start from 26 to avoid possible conflicts with X
   138         for attr in fetchattrs:
   137         for attr in fetchattrs:
   139             try:
   138             try:
   140                 rschema = eschema.subject_relation(attr)
   139                 rschema = eschema.subject_relation(attr)
   141             except KeyError:
   140             except KeyError:
   142                 cls.warning('skipping fetch_attr %s defined in %s (not found in schema)',
   141                 cls.warning('skipping fetch_attr %s defined in %s (not found in schema)',
   143                             attr, cls.id)
   142                             attr, cls.__id__)
   144                 continue
   143                 continue
   145             if not user.matching_groups(rschema.get_groups('read')):
   144             if not user.matching_groups(rschema.get_groups('read')):
   146                 continue
   145                 continue
   147             var = varmaker.next()
   146             var = varmaker.next()
   148             selection.append(var)
   147             selection.append(var)
   272         if method in (None, 'view'):
   271         if method in (None, 'view'):
   273             try:
   272             try:
   274                 kwargs['_restpath'] = self.rest_path(kwargs.get('base_url'))
   273                 kwargs['_restpath'] = self.rest_path(kwargs.get('base_url'))
   275             except TypeError:
   274             except TypeError:
   276                 warn('%s: rest_path() now take use_ext_eid argument, '
   275                 warn('%s: rest_path() now take use_ext_eid argument, '
   277                      'please update' % self.id, DeprecationWarning)
   276                      'please update' % self.__id__, DeprecationWarning)
   278                 kwargs['_restpath'] = self.rest_path()
   277                 kwargs['_restpath'] = self.rest_path()
   279         else:
   278         else:
   280             kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid
   279             kwargs['rql'] = 'Any X WHERE X eid %s' % self.eid
   281         return self.req.build_url(method, **kwargs)
   280         return self.req.build_url(method, **kwargs)
   282 
   281 
   414 
   413 
   415     @cached
   414     @cached
   416     def as_rset(self):
   415     def as_rset(self):
   417         """returns a resultset containing `self` information"""
   416         """returns a resultset containing `self` information"""
   418         rset = ResultSet([(self.eid,)], 'Any X WHERE X eid %(x)s',
   417         rset = ResultSet([(self.eid,)], 'Any X WHERE X eid %(x)s',
   419                          {'x': self.eid}, [(self.id,)])
   418                          {'x': self.eid}, [(self.__id__,)])
   420         return self.req.decorate_rset(rset)
   419         return self.req.decorate_rset(rset)
   421 
   420 
   422     def to_complete_relations(self):
   421     def to_complete_relations(self):
   423         """by default complete final relations to when calling .complete()"""
   422         """by default complete final relations to when calling .complete()"""
   424         for rschema in self.e_schema.subject_relations():
   423         for rschema in self.e_schema.subject_relations():