entities/__init__.py
brancholdstable
changeset 4985 02b52bf9f5f8
parent 4252 6c4f109c2b03
child 5421 8167de96c523
equal deleted inserted replaced
4563:c25da7573ebd 4985:02b52bf9f5f8
    20 
    20 
    21 class AnyEntity(Entity):
    21 class AnyEntity(Entity):
    22     """an entity instance has e_schema automagically set on the class and
    22     """an entity instance has e_schema automagically set on the class and
    23     instances have access to their issuing cursor
    23     instances have access to their issuing cursor
    24     """
    24     """
    25     id = 'Any'
    25     __regid__ = 'Any'
    26     __implements__ = (IBreadCrumbs, IFeed)
    26     __implements__ = (IBreadCrumbs, IFeed)
    27 
    27 
    28     fetch_attrs = ('modification_date',)
    28     fetch_attrs = ('modification_date',)
    29     @classmethod
    29     @classmethod
    30     def fetch_order(cls, attr, var):
    30     def fetch_order(cls, attr, var):
    80             return self.creator.name()
    80             return self.creator.name()
    81         return u''
    81         return u''
    82 
    82 
    83     def dc_date(self, date_format=None):# XXX default to ISO 8601 ?
    83     def dc_date(self, date_format=None):# XXX default to ISO 8601 ?
    84         """return latest modification date of this entity"""
    84         """return latest modification date of this entity"""
    85         return self.format_date(self.modification_date, date_format=date_format)
    85         return self._cw.format_date(self.modification_date, date_format=date_format)
    86 
    86 
    87     def dc_type(self, form=''):
    87     def dc_type(self, form=''):
    88         """return the display name for the type of this entity (translated)"""
    88         """return the display name for the type of this entity (translated)"""
    89         return self.e_schema.display_name(self.req, form)
    89         return self.e_schema.display_name(self._cw, form)
    90 
    90 
    91     def dc_language(self):
    91     def dc_language(self):
    92         """return language used by this entity (translated)"""
    92         """return language used by this entity (translated)"""
    93         # check if entities has internationalizable attributes
    93         # check if entities has internationalizable attributes
    94         # XXX one is enough or check if all String attributes are internationalizable?
    94         # XXX one is enough or check if all String attributes are internationalizable?
    95         for rschema, attrschema in self.e_schema.attribute_definitions():
    95         for rschema, attrschema in self.e_schema.attribute_definitions():
    96             if rschema.rproperty(self.e_schema, attrschema,
    96             if rschema.rdef(self.e_schema, attrschema).internationalizable:
    97                                  'internationalizable'):
    97                 return self._cw._(self._cw.user.property_value('ui.language'))
    98                 return self.req._(self.req.user.property_value('ui.language'))
    98         return self._cw._(self._cw.vreg.property_value('ui.language'))
    99         return self.req._(self.vreg.property_value('ui.language'))
       
   100 
    99 
   101     @property
   100     @property
   102     def creator(self):
   101     def creator(self):
   103         """return the CWUser entity which has created this entity, or None if
   102         """return the CWUser entity which has created this entity, or None if
   104         unknown or if the curent user doesn't has access to this euser
   103         unknown or if the curent user doesn't has access to this euser
   120                          "(view=None, recurs=False), please update",
   119                          "(view=None, recurs=False), please update",
   121                          DeprecationWarning)
   120                          DeprecationWarning)
   122                     path = parent.breadcrumbs(view) + [self]
   121                     path = parent.breadcrumbs(view) + [self]
   123         if not recurs:
   122         if not recurs:
   124             if view is None:
   123             if view is None:
   125                 if 'vtitle' in self.req.form:
   124                 if 'vtitle' in self._cw.form:
   126                     # embeding for instance
   125                     # embeding for instance
   127                     path.append( self.req.form['vtitle'] )
   126                     path.append( self._cw.form['vtitle'] )
   128             elif view.id != 'primary' and hasattr(view, 'title'):
   127             elif view.__regid__ != 'primary' and hasattr(view, 'title'):
   129                 path.append( self.req._(view.title) )
   128                 path.append( self._cw._(view.title) )
   130         return path
   129         return path
   131 
   130 
   132     ## IFeed interface ########################################################
   131     ## IFeed interface ########################################################
   133 
   132 
   134     def rss_feed_url(self):
   133     def rss_feed_url(self):
   161             return self.__linkto[(rtype, role)]
   160             return self.__linkto[(rtype, role)]
   162         except AttributeError:
   161         except AttributeError:
   163             self.__linkto = {}
   162             self.__linkto = {}
   164         except KeyError:
   163         except KeyError:
   165             pass
   164             pass
   166         linktos = list(self.req.list_form_param('__linkto'))
   165         linktos = list(self._cw.list_form_param('__linkto'))
   167         linkedto = []
   166         linkedto = []
   168         for linkto in linktos[:]:
   167         for linkto in linktos[:]:
   169             ltrtype, eid, ltrole = linkto.split(':')
   168             ltrtype, eid, ltrole = linkto.split(':')
   170             if rtype == ltrtype and role == ltrole:
   169             if rtype == ltrtype and role == ltrole:
   171                 # delete __linkto from form param to avoid it being added as
   170                 # delete __linkto from form param to avoid it being added as
   172                 # hidden input
   171                 # hidden input
   173                 if remove:
   172                 if remove:
   174                     linktos.remove(linkto)
   173                     linktos.remove(linkto)
   175                     self.req.form['__linkto'] = linktos
   174                     self._cw.form['__linkto'] = linktos
   176                 linkedto.append(typed_eid(eid))
   175                 linkedto.append(typed_eid(eid))
   177         self.__linkto[(rtype, role)] = linkedto
   176         self.__linkto[(rtype, role)] = linkedto
   178         return linkedto
   177         return linkedto
   179 
   178 
   180     # edit controller callbacks ###############################################
   179     # edit controller callbacks ###############################################
   203 
   202 
   204         Should return a list of eids which can be used to generate message ids
   203         Should return a list of eids which can be used to generate message ids
   205         of previously sent email
   204         of previously sent email
   206         """
   205         """
   207         return ()
   206         return ()
   208 
       
   209     # XXX deprecates, may be killed once old widgets system is gone ###########
       
   210 
       
   211     @classmethod
       
   212     def get_widget(cls, rschema, x='subject'):
       
   213         """return a widget to view or edit a relation
       
   214 
       
   215         notice that when the relation support multiple target types, the widget
       
   216         is necessarily the same for all those types
       
   217         """
       
   218         # let ImportError propage if web par isn't available
       
   219         from cubicweb.web.widgets import widget
       
   220         if isinstance(rschema, basestring):
       
   221             rschema = cls.schema.rschema(rschema)
       
   222         if x == 'subject':
       
   223             tschema = rschema.objects(cls.e_schema)[0]
       
   224             wdg = widget(cls.vreg, cls, rschema, tschema, 'subject')
       
   225         else:
       
   226             tschema = rschema.subjects(cls.e_schema)[0]
       
   227             wdg = widget(cls.vreg, tschema, rschema, cls, 'object')
       
   228         return wdg
       
   229 
       
   230     @deprecated('use EntityFieldsForm.subject_relation_vocabulary')
       
   231     def subject_relation_vocabulary(self, rtype, limit):
       
   232         form = self.vreg.select('forms', 'edition', self.req, entity=self)
       
   233         return form.subject_relation_vocabulary(rtype, limit)
       
   234 
       
   235     @deprecated('use EntityFieldsForm.object_relation_vocabulary')
       
   236     def object_relation_vocabulary(self, rtype, limit):
       
   237         form = self.vreg.select('forms', 'edition', self.req, entity=self)
       
   238         return form.object_relation_vocabulary(rtype, limit)
       
   239 
       
   240     @deprecated('use AutomaticEntityForm.[e]relations_by_category')
       
   241     def relations_by_category(self, categories=None, permission=None):
       
   242         from cubicweb.web.views.autoform import AutomaticEntityForm
       
   243         return AutomaticEntityForm.erelations_by_category(self, categories, permission)
       
   244 
       
   245     @deprecated('use AutomaticEntityForm.[e]srelations_by_category')
       
   246     def srelations_by_category(self, categories=None, permission=None):
       
   247         from cubicweb.web.views.autoform import AutomaticEntityForm
       
   248         return AutomaticEntityForm.esrelations_by_category(self, categories, permission)
       
   249 
       
   250     def attribute_values(self, attrname):
       
   251         if self.has_eid() or attrname in self:
       
   252             try:
       
   253                 values = self[attrname]
       
   254             except KeyError:
       
   255                 values = getattr(self, attrname)
       
   256             # actual relation return a list of entities
       
   257             if isinstance(values, list):
       
   258                 return [v.eid for v in values]
       
   259             return (values,)
       
   260         # the entity is being created, try to find default value for
       
   261         # this attribute
       
   262         try:
       
   263             values = self.req.form[attrname]
       
   264         except KeyError:
       
   265             try:
       
   266                 values = self[attrname] # copying
       
   267             except KeyError:
       
   268                 values = getattr(self, 'default_%s' % attrname,
       
   269                                  self.e_schema.default(attrname))
       
   270                 if callable(values):
       
   271                     values = values()
       
   272         if values is None:
       
   273             values = ()
       
   274         elif not isinstance(values, (list, tuple)):
       
   275             values = (values,)
       
   276         return values
       
   277 
       
   278     def use_fckeditor(self, attr):
       
   279         """return True if fckeditor should be used to edit entity's attribute named
       
   280         `attr`, according to user preferences
       
   281         """
       
   282         if self.req.use_fckeditor() and self.e_schema.has_metadata(attr, 'format'):
       
   283             if self.has_eid() or '%s_format' % attr in self:
       
   284                 return self.attr_metadata(attr, 'format') == 'text/html'
       
   285             return self.req.property_value('ui.default-text-format') == 'text/html'
       
   286         return False
       
   287 
   207 
   288 # XXX:  store a reference to the AnyEntity class since it is hijacked in goa
   208 # XXX:  store a reference to the AnyEntity class since it is hijacked in goa
   289 #       configuration and we need the actual reference to avoid infinite loops
   209 #       configuration and we need the actual reference to avoid infinite loops
   290 #       in mro
   210 #       in mro
   291 ANYENTITY = AnyEntity
   211 ANYENTITY = AnyEntity