entity.py
branchtls-sprint
changeset 1101 0c067de38e46
parent 1098 739d4dce9b19
child 1132 96752791c2b6
equal deleted inserted replaced
1100:7ca89f4468e4 1101:0c067de38e46
   459                     break
   459                     break
   460         if mainattr == 'eid':
   460         if mainattr == 'eid':
   461             needcheck = False
   461             needcheck = False
   462         return mainattr, needcheck
   462         return mainattr, needcheck
   463 
   463 
   464     @classmethod
   464     def attribute_metadata(self, attr, metadata):
   465     @obsolete('use method of the same name on the schema')
   465         """return a metadata for an attribute (None if unspecified)"""
   466     def has_format(cls, attr):
   466         value = getattr(self, '%s_%s' % (attr, metadata), None)
   467         """return true if this entity's schema has a format field for the given
   467         if value is None and metadata == 'encoding':
   468         attribute
   468             value = self.vreg.property_value('ui.encoding')
   469         """
   469         return value
   470         return cls.e_schema.has_format(attr)
       
   471         
       
   472     def format(self, attr):
       
   473         """return the mime type format for an attribute (if specified)"""
       
   474         return getattr(self, '%s_format' % attr, None)
       
   475     
       
   476     def text_encoding(self, attr):
       
   477         """return the text encoding for an attribute, default to site encoding
       
   478         """
       
   479         encoding = getattr(self, '%s_encoding' % attr, None)
       
   480         return encoding or self.vreg.property_value('ui.encoding')
       
   481     
       
   482     def has_text_encoding(self, attr):
       
   483         """return true if this entity's schema has ab encoding field for the
       
   484         given attribute
       
   485         """
       
   486         return self.e_schema.has_subject_relation('%s_encoding' % attr)
       
   487 
   470 
   488     def printable_value(self, attr, value=_marker, attrtype=None,
   471     def printable_value(self, attr, value=_marker, attrtype=None,
   489                         format='text/html', displaytime=True):
   472                         format='text/html', displaytime=True):
   490         """return a displayable value (i.e. unicode string) which may contains
   473         """return a displayable value (i.e. unicode string) which may contains
   491         html tags
   474         html tags
   503         if attrtype == 'String':
   486         if attrtype == 'String':
   504             # internalinalized *and* formatted string such as schema
   487             # internalinalized *and* formatted string such as schema
   505             # description...
   488             # description...
   506             if props.get('internationalizable'):
   489             if props.get('internationalizable'):
   507                 value = self.req._(value)
   490                 value = self.req._(value)
   508             attrformat = self.format(attr)
   491             attrformat = self.attribute_metadata(attr, 'format')
   509             if attrformat:
   492             if attrformat:
   510                 return self.mtc_transform(value, attrformat, format,
   493                 return self.mtc_transform(value, attrformat, format,
   511                                           self.req.encoding)
   494                                           self.req.encoding)
   512         elif attrtype == 'Bytes':
   495         elif attrtype == 'Bytes':
   513             attrformat = self.format(attr)
   496             attrformat = self.attribute_metadata(attr, 'format')
   514             if attrformat:
   497             if attrformat:
   515                 try:
   498                 encoding = self.attribute_metadata(attr, 'encoding')
   516                     encoding = getattr(self, '%s_encoding' % attr)
       
   517                 except AttributeError:
       
   518                     encoding = self.req.encoding
       
   519                 return self.mtc_transform(value.getvalue(), attrformat, format,
   499                 return self.mtc_transform(value.getvalue(), attrformat, format,
   520                                           encoding)
   500                                           encoding)
   521             return u''
   501             return u''
   522         value = printable_value(self.req, attrtype, value, props, displaytime)
   502         value = printable_value(self.req, attrtype, value, props, displaytime)
   523         if format == 'text/html':
   503         if format == 'text/html':