entities/__init__.py
branchtls-sprint
changeset 912 9ad0867692e1
parent 713 5adb6d8e5fa7
child 940 15dcdc863965
equal deleted inserted replaced
911:467fb77c6a33 912:9ad0867692e1
    94         # set a default_ATTR method for rich text format fields
    94         # set a default_ATTR method for rich text format fields
    95         for attr, formatattr in eschema.rich_text_fields():
    95         for attr, formatattr in eschema.rich_text_fields():
    96             if not hasattr(cls, 'default_%s' % formatattr):
    96             if not hasattr(cls, 'default_%s' % formatattr):
    97                 setattr(cls, 'default_%s' % formatattr, cls._default_format)
    97                 setattr(cls, 'default_%s' % formatattr, cls._default_format)
    98             eschema.format_fields[formatattr] = attr
    98             eschema.format_fields[formatattr] = attr
    99             
       
   100     def _default_format(self):
       
   101         return self.req.property_value('ui.default-text-format')
       
   102 
       
   103     def use_fckeditor(self, attr):
       
   104         """return True if fckeditor should be used to edit entity's attribute named
       
   105         `attr`, according to user preferences
       
   106         """
       
   107         req = self.req
       
   108         if req.property_value('ui.fckeditor') and self.has_format(attr):
       
   109             if self.has_eid() or '%s_format' % attr in self:
       
   110                 return self.format(attr) == 'text/html'
       
   111             return req.property_value('ui.default-text-format') == 'text/html'
       
   112         return False
       
   113     
    99     
   114     # meta data api ###########################################################
   100     # meta data api ###########################################################
   115 
   101 
   116     def dc_title(self):
   102     def dc_title(self):
   117         """return a suitable *unicode* title for this entity"""
   103         """return a suitable *unicode* title for this entity"""
   233         value = self.get_value(rtype)
   219         value = self.get_value(rtype)
   234         # do not restrict to `unicode` because Bytes will return a `str` value
   220         # do not restrict to `unicode` because Bytes will return a `str` value
   235         if isinstance(value, basestring):
   221         if isinstance(value, basestring):
   236             return self.printable_value(rtype, format='text/plain').lower()
   222             return self.printable_value(rtype, format='text/plain').lower()
   237         return value
   223         return value
   238 
       
   239     def after_deletion_path(self):
       
   240         """return (path, parameters) which should be used as redirect
       
   241         information when this entity is being deleted
       
   242         """
       
   243         return str(self.e_schema).lower(), {}
       
   244 
   224 
   245     def add_related_schemas(self):
   225     def add_related_schemas(self):
   246         """this is actually used ui method to generate 'addrelated' actions from
   226         """this is actually used ui method to generate 'addrelated' actions from
   247         the schema.
   227         the schema.
   248 
   228 
   341                                                                   permission):
   321                                                                   permission):
   342             if rschema.is_final():
   322             if rschema.is_final():
   343                 continue
   323                 continue
   344             result.append( (rschema.display_name(self.req, target), rschema, target) )
   324             result.append( (rschema.display_name(self.req, target), rschema, target) )
   345         return sorted(result)
   325         return sorted(result)
       
   326 
       
   327     def linked_to(self, rtype, target, remove=True):
       
   328         """if entity should be linked to another using __linkto form param for
       
   329         the given relation/target, return eids of related entities
       
   330 
       
   331         This method is consuming matching link-to information from form params
       
   332         if `remove` is True (by default).
       
   333         """
       
   334         try:
       
   335             return self.__linkto[(rtype, target)]
       
   336         except AttributeError:
       
   337             self.__linkto = {}
       
   338         except KeyError:
       
   339             pass
       
   340         linktos = list(self.req.list_form_param('__linkto'))
       
   341         linkedto = []
       
   342         for linkto in linktos[:]:
       
   343             ltrtype, eid, lttarget = linkto.split(':')
       
   344             if rtype == ltrtype and target == lttarget:
       
   345                 # delete __linkto from form param to avoid it being added as
       
   346                 # hidden input
       
   347                 if remove:
       
   348                     linktos.remove(linkto)
       
   349                     self.req.form['__linkto'] = linktos
       
   350                 linkedto.append(typed_eid(eid))
       
   351         self.__linkto[(rtype, target)] = linkedto
       
   352         return linkedto
       
   353     
       
   354     # edit controller callbacks ###############################################
       
   355     
       
   356     def after_deletion_path(self):
       
   357         """return (path, parameters) which should be used as redirect
       
   358         information when this entity is being deleted
       
   359         """
       
   360         return str(self.e_schema).lower(), {}
       
   361 
       
   362     def pre_web_edit(self):
       
   363         """callback called by the web editcontroller when an entity will be
       
   364         created/modified, to let a chance to do some entity specific stuff.
       
   365 
       
   366         Do nothing by default.
       
   367         """
       
   368         pass
       
   369     
       
   370     # server side helpers #####################################################
       
   371     
       
   372     def notification_references(self, view):
       
   373         """used to control References field of email send on notification
       
   374         for this entity. `view` is the notification view.
       
   375         
       
   376         Should return a list of eids which can be used to generate message ids
       
   377         of previously sent email
       
   378         """
       
   379         return ()
       
   380             
       
   381     # XXX deprecates, may be killed once old widgets system is gone ###########
       
   382     
       
   383     def _default_format(self):
       
   384         return self.req.property_value('ui.default-text-format')
   346                 
   385                 
   347     def attribute_values(self, attrname):
   386     def attribute_values(self, attrname):
   348         if self.has_eid() or attrname in self:
   387         if self.has_eid() or attrname in self:
   349             try:
   388             try:
   350                 values = self[attrname]
   389                 values = self[attrname]
   370             values = ()
   409             values = ()
   371         elif not isinstance(values, (list, tuple)):
   410         elif not isinstance(values, (list, tuple)):
   372             values = (values,)
   411             values = (values,)
   373         return values
   412         return values
   374 
   413 
   375     def linked_to(self, rtype, target, remove=True):
   414     def use_fckeditor(self, attr):
   376         """if entity should be linked to another using __linkto form param for
   415         """return True if fckeditor should be used to edit entity's attribute named
   377         the given relation/target, return eids of related entities
   416         `attr`, according to user preferences
   378 
   417         """
   379         This method is consuming matching link-to information from form params
   418         req = self.req
   380         if `remove` is True (by default).
   419         if req.property_value('ui.fckeditor') and self.has_format(attr):
   381         """
   420             if self.has_eid() or '%s_format' % attr in self:
   382         try:
   421                 return self.format(attr) == 'text/html'
   383             return self.__linkto[(rtype, target)]
   422             return req.property_value('ui.default-text-format') == 'text/html'
   384         except AttributeError:
   423         return False
   385             self.__linkto = {}
       
   386         except KeyError:
       
   387             pass
       
   388         linktos = list(self.req.list_form_param('__linkto'))
       
   389         linkedto = []
       
   390         for linkto in linktos[:]:
       
   391             ltrtype, eid, lttarget = linkto.split(':')
       
   392             if rtype == ltrtype and target == lttarget:
       
   393                 # delete __linkto from form param to avoid it being added as
       
   394                 # hidden input
       
   395                 if remove:
       
   396                     linktos.remove(linkto)
       
   397                     self.req.form['__linkto'] = linktos
       
   398                 linkedto.append(typed_eid(eid))
       
   399         self.__linkto[(rtype, target)] = linkedto
       
   400         return linkedto
       
   401 
       
   402     def pre_web_edit(self):
       
   403         """callback called by the web editcontroller when an entity will be
       
   404         created/modified, to let a chance to do some entity specific stuff.
       
   405 
       
   406         Do nothing by default.
       
   407         """
       
   408         pass
       
   409     
       
   410     # server side helpers #####################################################
       
   411     
       
   412     def notification_references(self, view):
       
   413         """used to control References field of email send on notification
       
   414         for this entity. `view` is the notification view.
       
   415         
       
   416         Should return a list of eids which can be used to generate message ids
       
   417         of previously sent email
       
   418         """
       
   419         return ()
       
   420 
   424 
   421 # XXX:  store a reference to the AnyEntity class since it is hijacked in goa
   425 # XXX:  store a reference to the AnyEntity class since it is hijacked in goa
   422 #       configuration and we need the actual reference to avoid infinite loops
   426 #       configuration and we need the actual reference to avoid infinite loops
   423 #       in mro
   427 #       in mro
   424 ANYENTITY = AnyEntity
   428 ANYENTITY = AnyEntity