appobject.py
changeset 2799 b703639614e7
parent 2798 9c650701cb17
child 2802 2251b4aee54a
equal deleted inserted replaced
2798:9c650701cb17 2799:b703639614e7
   275         the right hook to create an instance for example). By default the
   275         the right hook to create an instance for example). By default the
   276         appobject is returned without any transformation.
   276         appobject is returned without any transformation.
   277         """
   277         """
   278         cls.build___select__()
   278         cls.build___select__()
   279         cls.vreg = registry.vreg
   279         cls.vreg = registry.vreg
   280         cls.register_properties()
   280         pdefs = getattr(cls, 'cw_property_defs', {})
   281         return cls
   281         for propid, pdef in pdefs:
   282 
       
   283     @classmethod
       
   284     def vreg_initialization_completed(cls):
       
   285         pass
       
   286 
       
   287     # properties definition:
       
   288     # key: id of the property (the actual CWProperty key is build using
       
   289     #      <registry name>.<obj id>.<property id>
       
   290     # value: tuple (property type, vocabfunc, default value, property description)
       
   291     #        possible types are those used by `logilab.common.configuration`
       
   292     #
       
   293     # notice that when it exists multiple objects with the same id (adaptation,
       
   294     # overriding) only the first encountered definition is considered, so those
       
   295     # objects can't try to have different default values for instance.
       
   296 
       
   297     property_defs = {}
       
   298 
       
   299     @classmethod
       
   300     def register_properties(cls):
       
   301         for propid, pdef in cls.property_defs.items():
       
   302             pdef = pdef.copy() # may be shared
   282             pdef = pdef.copy() # may be shared
   303             pdef['default'] = getattr(cls, propid, pdef['default'])
   283             pdef['default'] = getattr(cls, propid, pdef['default'])
   304             pdef['sitewide'] = getattr(cls, 'site_wide', pdef.get('sitewide'))
   284             pdef['sitewide'] = getattr(cls, 'site_wide', pdef.get('sitewide'))
   305             cls.vreg.register_property(cls.propkey(propid), **pdef)
   285             registry.vreg.register_property(cls._cwpropkey(propid), **pdef)
   306 
   286         return cls
   307     @classmethod
   287 
   308     def propkey(cls, propid):
   288     @classmethod
   309         return '%s.%s.%s' % (cls.__registry__, cls.id, propid)
   289     def vreg_initialization_completed(cls):
   310 
   290         pass
   311 
   291 
   312     def __init__(self, req=None, rset=None, row=None, col=None, **extra):
   292     def __init__(self, req=None, rset=None, row=None, col=None, **extra):
   313         super(AppObject, self).__init__()
   293         super(AppObject, self).__init__()
   314         self.req = req
   294         self.req = req
   315         self.rset = rset
   295         self.rset = rset
   316         self.row = row
   296         self.row = row
   317         self.col = col
   297         self.col = col
   318         self.extra_kwargs = extra
   298         self.extra_kwargs = extra
   319 
   299 
   320     def propval(self, propid):
       
   321         assert self.req
       
   322         return self.req.property_value(self.propkey(propid))
       
   323 
       
   324     def view(self, __vid, rset=None, __fallback_oid=None, __registry='views',
   300     def view(self, __vid, rset=None, __fallback_oid=None, __registry='views',
   325              **kwargs):
   301              **kwargs):
   326         """shortcut to self.vreg.view method avoiding to pass self.req"""
   302         """shortcut to self.vreg.view method avoiding to pass self.req"""
   327         return self.vreg[__registry].render(__vid, self.req, __fallback_oid,
   303         return self.vreg[__registry].render(__vid, self.req, __fallback_oid,
   328                                             rset=rset, **kwargs)
   304                                             rset=rset, **kwargs)
       
   305 
       
   306     # persistent class properties ##############################################
       
   307     #
       
   308     # optional `cw_property_defs` dict on a class defines available persistent
       
   309     # properties for this class:
       
   310     #
       
   311     # * key: id of the property (the actual CWProperty key is build using
       
   312     #        <registry name>.<obj id>.<property id>
       
   313     # * value: tuple (property type, vocabfunc, default value, property description)
       
   314     #         possible types are those used by `logilab.common.configuration`
       
   315     #
       
   316     # notice that when it exists multiple objects with the same id (adaptation,
       
   317     # overriding) only the first encountered definition is considered, so those
       
   318     # objects can't try to have different default values for instance.
       
   319     #
       
   320     # you can then access to a property value using self.propval, where self is
       
   321     # an instance of class
       
   322 
       
   323     @classmethod
       
   324     def _cwpropkey(cls, propid):
       
   325         """return cw property key for the property of the given id for this
       
   326         class
       
   327         """
       
   328         return '%s.%s.%s' % (cls.__registry__, cls.id, propid)
       
   329 
       
   330     def cw_propval(self, propid):
       
   331         """return cw property value associated to key
       
   332 
       
   333         <cls.__registry__>.<cls.id>.<propid>
       
   334         """
       
   335         return self.req.property_value(self._cwpropkey(propid))
   329 
   336 
   330     # deprecated ###############################################################
   337     # deprecated ###############################################################
   331 
   338 
   332     @classproperty
   339     @classproperty
   333     @deprecated('[3.4] use __select__ and & or | operators')
   340     @deprecated('[3.4] use __select__ and & or | operators')
   395 
   402 
   396     @deprecated('[3.5] use req.parse_datetime')
   403     @deprecated('[3.5] use req.parse_datetime')
   397     def parse_datetime(self, value, etype='Datetime'):
   404     def parse_datetime(self, value, etype='Datetime'):
   398         return self.req.parse_datetime(value, etype)
   405         return self.req.parse_datetime(value, etype)
   399 
   406 
       
   407     @deprecated('[3.5] use self.cw_propval')
       
   408     def propval(self, propid):
       
   409         return self.req.property_value(self._cwpropkey(propid))
       
   410 
   400 set_log_methods(AppObject, getLogger('cubicweb.appobject'))
   411 set_log_methods(AppObject, getLogger('cubicweb.appobject'))