entities/__init__.py
branchtls-sprint
changeset 1493 8270580b65a0
parent 1417 06af20e663f2
child 1498 2c6eec0b46b9
equal deleted inserted replaced
1492:53d28ac02868 1493:8270580b65a0
    21 
    21 
    22 class AnyEntity(Entity):
    22 class AnyEntity(Entity):
    23     """an entity instance has e_schema automagically set on the class and
    23     """an entity instance has e_schema automagically set on the class and
    24     instances have access to their issuing cursor
    24     instances have access to their issuing cursor
    25     """
    25     """
    26     id = 'Any'                    
    26     id = 'Any'
    27     __implements__ = (IBreadCrumbs, IFeed)
    27     __implements__ = (IBreadCrumbs, IFeed)
    28 
    28 
    29     @classmethod
    29     @classmethod
    30     def selected(cls, etype):
    30     def selected(cls, etype):
    31         """the special Any entity is used as the default factory, so
    31         """the special Any entity is used as the default factory, so
    36             return cls
    36             return cls
    37         usercls = dump_class(cls, etype)
    37         usercls = dump_class(cls, etype)
    38         usercls.id = etype
    38         usercls.id = etype
    39         usercls.__initialize__()
    39         usercls.__initialize__()
    40         return usercls
    40         return usercls
    41     
    41 
    42     fetch_attrs = ('modification_date',)
    42     fetch_attrs = ('modification_date',)
    43     @classmethod
    43     @classmethod
    44     def fetch_order(cls, attr, var):
    44     def fetch_order(cls, attr, var):
    45         """class method used to control sort order when multiple entities of
    45         """class method used to control sort order when multiple entities of
    46         this type are fetched
    46         this type are fetched
    47         """
    47         """
    48         return cls.fetch_unrelated_order(attr, var)
    48         return cls.fetch_unrelated_order(attr, var)
    49     
    49 
    50     @classmethod
    50     @classmethod
    51     def fetch_unrelated_order(cls, attr, var):
    51     def fetch_unrelated_order(cls, attr, var):
    52         """class method used to control sort order when multiple entities of
    52         """class method used to control sort order when multiple entities of
    53         this type are fetched to use in edition (eg propose them to create a
    53         this type are fetched to use in edition (eg propose them to create a
    54         new relation on an edited entity).
    54         new relation on an edited entity).
    56         if attr == 'modification_date':
    56         if attr == 'modification_date':
    57             return '%s DESC' % var
    57             return '%s DESC' % var
    58         return None
    58         return None
    59 
    59 
    60     @classmethod
    60     @classmethod
    61     def __initialize__(cls): 
    61     def __initialize__(cls):
    62         super(ANYENTITY, cls).__initialize__() # XXX
    62         super(ANYENTITY, cls).__initialize__() # XXX
    63         # set a default_ATTR method for rich text format fields
    63         # set a default_ATTR method for rich text format fields
    64         # XXX move this away once the old widgets have been dropped!
    64         # XXX move this away once the old widgets have been dropped!
    65         eschema = cls.e_schema
    65         eschema = cls.e_schema
    66         for metaattr, (metadata, attr) in eschema.meta_attributes().iteritems():
    66         for metaattr, (metadata, attr) in eschema.meta_attributes().iteritems():
    67             if metadata == 'format' and not hasattr(cls, 'default_%s' % metaattr):
    67             if metadata == 'format' and not hasattr(cls, 'default_%s' % metaattr):
    68                 setattr(cls, 'default_%s' % metaattr, cls._default_format)
    68                 setattr(cls, 'default_%s' % metaattr, cls._default_format)
    69     
    69 
    70     # meta data api ###########################################################
    70     # meta data api ###########################################################
    71 
    71 
    72     def dc_title(self):
    72     def dc_title(self):
    73         """return a suitable *unicode* title for this entity"""
    73         """return a suitable *unicode* title for this entity"""
    74         for rschema, attrschema in self.e_schema.attribute_definitions():
    74         for rschema, attrschema in self.e_schema.attribute_definitions():
    82         return u'%s #%s' % (self.dc_type(), self.eid)
    82         return u'%s #%s' % (self.dc_type(), self.eid)
    83 
    83 
    84     def dc_long_title(self):
    84     def dc_long_title(self):
    85         """return a more detailled title for this entity"""
    85         """return a more detailled title for this entity"""
    86         return self.dc_title()
    86         return self.dc_title()
    87     
    87 
    88     def dc_description(self, format='text/plain'):
    88     def dc_description(self, format='text/plain'):
    89         """return a suitable description for this entity"""
    89         """return a suitable description for this entity"""
    90         if self.e_schema.has_subject_relation('description'):
    90         if self.e_schema.has_subject_relation('description'):
    91             return self.printable_value('description', format=format)
    91             return self.printable_value('description', format=format)
    92         return u''
    92         return u''
   119         for rschema, attrschema in self.e_schema.attribute_definitions():
   119         for rschema, attrschema in self.e_schema.attribute_definitions():
   120             if rschema.rproperty(self.e_schema, attrschema,
   120             if rschema.rproperty(self.e_schema, attrschema,
   121                                  'internationalizable'):
   121                                  'internationalizable'):
   122                 return self.req._(self.req.user.property_value('ui.language'))
   122                 return self.req._(self.req.user.property_value('ui.language'))
   123         return self.req._(self.vreg.property_value('ui.language'))
   123         return self.req._(self.vreg.property_value('ui.language'))
   124         
   124 
   125     @property
   125     @property
   126     def creator(self):
   126     def creator(self):
   127         """return the CWUser entity which has created this entity, or None if
   127         """return the CWUser entity which has created this entity, or None if
   128         unknown or if the curent user doesn't has access to this euser
   128         unknown or if the curent user doesn't has access to this euser
   129         """
   129         """
   155 
   155 
   156     ## IFeed interface ########################################################
   156     ## IFeed interface ########################################################
   157 
   157 
   158     def rss_feed_url(self):
   158     def rss_feed_url(self):
   159         return self.absolute_url(vid='rss')
   159         return self.absolute_url(vid='rss')
   160     
   160 
   161     # abstractions making the whole things (well, some at least) working ######
   161     # abstractions making the whole things (well, some at least) working ######
   162         
   162 
   163     def sortvalue(self, rtype=None):
   163     def sortvalue(self, rtype=None):
   164         """return a value which can be used to sort this entity or given
   164         """return a value which can be used to sort this entity or given
   165         entity's attribute
   165         entity's attribute
   166         """
   166         """
   167         if rtype is None:
   167         if rtype is None:
   170         # do not restrict to `unicode` because Bytes will return a `str` value
   170         # do not restrict to `unicode` because Bytes will return a `str` value
   171         if isinstance(value, basestring):
   171         if isinstance(value, basestring):
   172             return self.printable_value(rtype, format='text/plain').lower()
   172             return self.printable_value(rtype, format='text/plain').lower()
   173         return value
   173         return value
   174 
   174 
   175     # edition helper functions ################################################    
   175     # edition helper functions ################################################
   176 
   176 
   177     def linked_to(self, rtype, target, remove=True):
   177     def linked_to(self, rtype, target, remove=True):
   178         """if entity should be linked to another using __linkto form param for
   178         """if entity should be linked to another using __linkto form param for
   179         the given relation/target, return eids of related entities
   179         the given relation/target, return eids of related entities
   180 
   180 
   198                     linktos.remove(linkto)
   198                     linktos.remove(linkto)
   199                     self.req.form['__linkto'] = linktos
   199                     self.req.form['__linkto'] = linktos
   200                 linkedto.append(typed_eid(eid))
   200                 linkedto.append(typed_eid(eid))
   201         self.__linkto[(rtype, target)] = linkedto
   201         self.__linkto[(rtype, target)] = linkedto
   202         return linkedto
   202         return linkedto
   203     
   203 
   204     # edit controller callbacks ###############################################
   204     # edit controller callbacks ###############################################
   205     
   205 
   206     def after_deletion_path(self):
   206     def after_deletion_path(self):
   207         """return (path, parameters) which should be used as redirect
   207         """return (path, parameters) which should be used as redirect
   208         information when this entity is being deleted
   208         information when this entity is being deleted
   209         """
   209         """
   210         return str(self.e_schema).lower(), {}
   210         return str(self.e_schema).lower(), {}
   214         created/modified, to let a chance to do some entity specific stuff.
   214         created/modified, to let a chance to do some entity specific stuff.
   215 
   215 
   216         Do nothing by default.
   216         Do nothing by default.
   217         """
   217         """
   218         pass
   218         pass
   219     
   219 
   220     # server side helpers #####################################################
   220     # server side helpers #####################################################
   221     
   221 
   222     def notification_references(self, view):
   222     def notification_references(self, view):
   223         """used to control References field of email send on notification
   223         """used to control References field of email send on notification
   224         for this entity. `view` is the notification view.
   224         for this entity. `view` is the notification view.
   225         
   225 
   226         Should return a list of eids which can be used to generate message ids
   226         Should return a list of eids which can be used to generate message ids
   227         of previously sent email
   227         of previously sent email
   228         """
   228         """
   229         return ()
   229         return ()
   230             
   230 
   231     # XXX deprecates, may be killed once old widgets system is gone ###########
   231     # XXX deprecates, may be killed once old widgets system is gone ###########
   232     
   232 
   233     @classmethod
   233     @classmethod
   234     def get_widget(cls, rschema, x='subject'):
   234     def get_widget(cls, rschema, x='subject'):
   235         """return a widget to view or edit a relation
   235         """return a widget to view or edit a relation
   236 
   236 
   237         notice that when the relation support multiple target types, the widget
   237         notice that when the relation support multiple target types, the widget
   256 
   256 
   257     @obsolete('use EntityFieldsForm.object_relation_vocabulary')
   257     @obsolete('use EntityFieldsForm.object_relation_vocabulary')
   258     def object_relation_vocabulary(self, rtype, limit):
   258     def object_relation_vocabulary(self, rtype, limit):
   259         from cubicweb.web.form import EntityFieldsForm
   259         from cubicweb.web.form import EntityFieldsForm
   260         return EntityFieldsForm(self.req, None, entity=self).object_relation_vocabulary(rtype, limit)
   260         return EntityFieldsForm(self.req, None, entity=self).object_relation_vocabulary(rtype, limit)
   261     
   261 
   262     @obsolete('use AutomaticEntityForm.[e]relations_by_category')
   262     @obsolete('use AutomaticEntityForm.[e]relations_by_category')
   263     def relations_by_category(self, categories=None, permission=None):
   263     def relations_by_category(self, categories=None, permission=None):
   264         from cubicweb.web.views.editforms import AutomaticEntityForm
   264         from cubicweb.web.views.editforms import AutomaticEntityForm
   265         return AutomaticEntityForm.erelations_by_category(self, categories, permission)
   265         return AutomaticEntityForm.erelations_by_category(self, categories, permission)
   266 
   266 
   267     @obsolete('use AutomaticEntityForm.[e]srelations_by_category')
   267     @obsolete('use AutomaticEntityForm.[e]srelations_by_category')
   268     def srelations_by_category(self, categories=None, permission=None):
   268     def srelations_by_category(self, categories=None, permission=None):
   269         from cubicweb.web.views.editforms import AutomaticEntityForm
   269         from cubicweb.web.views.editforms import AutomaticEntityForm
   270         return AutomaticEntityForm.esrelations_by_category(self, categories, permission)
   270         return AutomaticEntityForm.esrelations_by_category(self, categories, permission)
   271     
   271 
   272     def _default_format(self):
   272     def _default_format(self):
   273         return self.req.property_value('ui.default-text-format')
   273         return self.req.property_value('ui.default-text-format')
   274                 
   274 
   275     def attribute_values(self, attrname):
   275     def attribute_values(self, attrname):
   276         if self.has_eid() or attrname in self:
   276         if self.has_eid() or attrname in self:
   277             try:
   277             try:
   278                 values = self[attrname]
   278                 values = self[attrname]
   279             except KeyError:
   279             except KeyError: