web/form.py
branchtls-sprint
changeset 1061 7d2e6b5fae3c
parent 1060 d795477ad16b
child 1062 185d630d1aef
equal deleted inserted replaced
1060:d795477ad16b 1061:7d2e6b5fae3c
   810                                   'rawvalue': value,
   810                                   'rawvalue': value,
   811                                   'name': self.form_field_name(field),
   811                                   'name': self.form_field_name(field),
   812                                   'id': self.form_field_id(field),
   812                                   'id': self.form_field_id(field),
   813                                   }
   813                                   }
   814 
   814 
   815     def form_field_value(self, field, values):
   815     def form_field_value(self, field, values, load_bytes=False):
   816         """looks for field's value in
   816         """looks for field's value in
   817         1. kw args given to render_form (including previously submitted form
   817         1. kw args given to render_form (including previously submitted form
   818            values if any)
   818            values if any)
   819         2. req.form
   819         2. req.form
   820         3. field's initial value
   820         3. field's initial value
   828         return value
   828         return value
   829 
   829 
   830     def form_field_format(self, field):
   830     def form_field_format(self, field):
   831         return self.req.property_value('ui.default-text-format')
   831         return self.req.property_value('ui.default-text-format')
   832     
   832     
       
   833     def form_field_encoding(self, field):
       
   834         return self.req.encoding
       
   835     
   833     def form_field_name(self, field):
   836     def form_field_name(self, field):
   834         return field.name
   837         return field.name
   835 
   838 
   836     def form_field_id(self, field):
   839     def form_field_id(self, field):
   837         return field.id
   840         return field.id
   877             name = 'edits-%s' % field.name
   880             name = 'edits-%s' % field.name
   878         else:
   881         else:
   879             name = 'edito-%s' % field.name
   882             name = 'edito-%s' % field.name
   880         return HiddenInitialValueField(field, name=name)
   883         return HiddenInitialValueField(field, name=name)
   881         
   884         
   882     def form_field_value(self, field, values):
   885     def form_field_value(self, field, values, load_bytes=False):
   883         """look for field's value with the following rules:
   886         """look for field's value with the following rules:
   884         1. handle special __type and eid fields
   887         1. handle special __type and eid fields
   885         2. looks in kw args given to render_form (including previously submitted
   888         2. looks in kw args given to render_form (including previously submitted
   886            form values if any)
   889            form values if any)
   887         3. looks in req.form
   890         3. looks in req.form
   896         """
   899         """
   897         fieldname = field.name
   900         fieldname = field.name
   898         if fieldname.startswith('edits-') or fieldname.startswith('edito-'):
   901         if fieldname.startswith('edits-') or fieldname.startswith('edito-'):
   899             # edit[s|o]- fieds must have the actual value stored on the entity
   902             # edit[s|o]- fieds must have the actual value stored on the entity
   900             if self.entity.has_eid():
   903             if self.entity.has_eid():
   901                 value = self.form_field_entity_value(field.visible_field,
   904                 value = self._form_field_entity_value(field.visible_field,
   902                                                      default_initial=False)
   905                                                       default_initial=False)
   903             else:
   906             else:
   904                 value = INTERNAL_FIELD_VALUE
   907                 value = INTERNAL_FIELD_VALUE
   905         elif fieldname == '__type':
   908         elif fieldname == '__type':
   906             value = self.entity.id
   909             value = self.entity.id
   907         elif fieldname == 'eid':
   910         elif fieldname == 'eid':
   913         else:
   916         else:
   914             if self.entity.has_eid() and field.eidparam:
   917             if self.entity.has_eid() and field.eidparam:
   915                 # use value found on the entity or field's initial value if it's
   918                 # use value found on the entity or field's initial value if it's
   916                 # not an attribute of the entity (XXX may conflicts and get
   919                 # not an attribute of the entity (XXX may conflicts and get
   917                 # undesired value)
   920                 # undesired value)
   918                 value = self.form_field_entity_value(field, default_initial=True)
   921                 value = self._form_field_entity_value(field, default_initial=True,
       
   922                                                       load_bytes=load_bytes)
   919             else:
   923             else:
   920                 defaultattr = 'default_%s' % fieldname
   924                 defaultattr = 'default_%s' % fieldname
   921                 if hasattr(self.entity, defaultattr):
   925                 if hasattr(self.entity, defaultattr):
   922                     # XXX bw compat, default_<field name> on the entity
   926                     # XXX bw compat, default_<field name> on the entity
   923                     warn('found %s on %s, should be set on a specific form'
   927                     warn('found %s on %s, should be set on a specific form'
   938         if field.eidparam and entity.has_format(field.name) and (
   942         if field.eidparam and entity.has_format(field.name) and (
   939             entity.has_eid() or '%s_format' % field.name in entity):
   943             entity.has_eid() or '%s_format' % field.name in entity):
   940             return self.entity.format(field.name)
   944             return self.entity.format(field.name)
   941         return self.req.property_value('ui.default-text-format')
   945         return self.req.property_value('ui.default-text-format')
   942 
   946 
   943     def form_field_entity_value(self, field, default_initial=True):
   947     def form_field_encoding(self, field):
       
   948         entity = self.entity
       
   949         if field.eidparam and entity.has_encoding(field.name) and (
       
   950             entity.has_eid() or '%s_encoding' % field.name in entity):
       
   951             return self.entity.text_encoding()
       
   952         return super(EntityFieldsForm, self).form_field_encoding(field)
       
   953 
       
   954     def _form_field_entity_value(self, field, default_initial=True, load_bytes=False):
   944         attr = field.name 
   955         attr = field.name 
   945         if field.role == 'object':
   956         if field.role == 'object':
   946             attr = 'reverse_' + attr
   957             attr = 'reverse_' + attr
   947         else:
   958         else:
   948             attrtype = self.entity.e_schema.destination(attr)
   959             attrtype = self.entity.e_schema.destination(attr)
   949             if attrtype == 'Password':
   960             if attrtype == 'Password':
   950                 return self.entity.has_eid() and INTERNAL_FIELD_VALUE or ''
   961                 return self.entity.has_eid() and INTERNAL_FIELD_VALUE or ''
   951             if attrtype == 'Bytes':
   962             if attrtype == 'Bytes':
   952                 # XXX value should reflect if some file is already attached
   963                 if self.entity.has_eid():
   953                 return self.entity.has_eid()
   964                     if load_bytes:
       
   965                         return getattr(self.entity, attr)
       
   966                     # XXX value should reflect if some file is already attached
       
   967                     return True
       
   968                 return False
   954         if default_initial:
   969         if default_initial:
   955             value = getattr(self.entity, attr, field.initial)
   970             value = getattr(self.entity, attr, field.initial)
   956         else:
   971         else:
   957             value = getattr(self.entity, attr)
   972             value = getattr(self.entity, attr)
   958         if isinstance(field, RelationField):
   973         if isinstance(field, RelationField):