web/formfields.py
changeset 4369 6d3dae46ee95
parent 4368 d752ad901f14
child 4370 75c610a85949
equal deleted inserted replaced
4368:d752ad901f14 4369:6d3dae46ee95
   209         if self.eidparam:
   209         if self.eidparam:
   210             return eid_param(id, form.edited_entity.eid)
   210             return eid_param(id, form.edited_entity.eid)
   211         return id
   211         return id
   212 
   212 
   213     def typed_value(self, form, load_bytes=False):
   213     def typed_value(self, form, load_bytes=False):
       
   214         if self.eidparam and self.role is not None:
       
   215             entity = form.edited_entity
       
   216             if form._cw.vreg.schema.rschema(self.name).final:
       
   217                 if entity.has_eid() or self.name in entity:
       
   218                     return getattr(entity, self.name)
       
   219             elif entity.has_eid() or entity.relation_cached(self.name, self.role):
       
   220                 return [r[0] for r in entity.related(self.name, self.role)]
       
   221         return self.initial_typed_value(form, load_bytes)
       
   222 
       
   223     def initial_typed_value(self, form, load_bytes):
   214         if self.value is not _MARKER:
   224         if self.value is not _MARKER:
   215             if callable(self.value):
   225             if callable(self.value):
   216                 return self.value(form)
   226                 return self.value(form)
   217             return self.value
   227             return self.value
   218         return self._typed_value(form, load_bytes)
       
   219 
       
   220     def _typed_value(self, form, load_bytes=False):
       
   221         if self.eidparam:
       
   222             assert form._cw.vreg.schema.rschema(self.name).final
       
   223             entity = form.edited_entity
       
   224             if entity.has_eid() or self.name in entity:
       
   225                 return getattr(entity, self.name)
       
   226         formattr = '%s_%s_default' % (self.role, self.name)
   228         formattr = '%s_%s_default' % (self.role, self.name)
   227         if hasattr(form, formattr):
   229         if hasattr(form, formattr):
   228             warn('[3.6] %s.%s deprecated, use field.value' % (
   230             warn('[3.6] %s.%s deprecated, use field.value' % (
   229                 form.__class__.__name__, formattr), DeprecationWarning)
   231                 form.__class__.__name__, formattr), DeprecationWarning)
   230             return getattr(form, formattr)()
   232             return getattr(form, formattr)()
   231         if self.eidparam:
   233         if self.eidparam and self.role is not None:
   232             return entity.e_schema.default(self.name)
   234             if form._cw.vreg.schema.rschema(self.name).final:
       
   235                 return form.edited_entity.e_schema.default(self.name)
       
   236             return ()
   233         return None
   237         return None
   234 
   238 
   235     def example_format(self, req):
   239     def example_format(self, req):
   236         """return a sample string describing what can be given as input for this
   240         """return a sample string describing what can be given as input for this
   237         field
   241         field
   391 
   395 
   392 
   396 
   393 class PasswordField(StringField):
   397 class PasswordField(StringField):
   394     widget = fw.PasswordInput
   398     widget = fw.PasswordInput
   395 
   399 
   396     def _typed_value(self, form, load_bytes=False):
   400     def typed_value(self, form, load_bytes=False):
   397         if self.eidparam:
   401         if self.eidparam:
   398             # no way to fetch actual password value with cw
   402             # no way to fetch actual password value with cw
   399             if form.edited_entity.has_eid():
   403             if form.edited_entity.has_eid():
   400                 return INTERNAL_FIELD_VALUE
   404                 return INTERNAL_FIELD_VALUE
   401             return form.edited_entity.e_schema.default(self.name)
   405             return self.initial_typed_value(form, load_bytes)
   402         return super(PasswordField, self)._typed_value(form, load_bytes)
   406         return super(PasswordField, self).typed_value(form, load_bytes)
   403 
   407 
   404 
   408 
   405 class RichTextField(StringField):
   409 class RichTextField(StringField):
   406     widget = None
   410     widget = None
   407     def __init__(self, format_field=None, **kwargs):
   411     def __init__(self, format_field=None, **kwargs):
   491         if self.encoding_field:
   495         if self.encoding_field:
   492             yield self.encoding_field
   496             yield self.encoding_field
   493         if self.name_field:
   497         if self.name_field:
   494             yield self.name_field
   498             yield self.name_field
   495 
   499 
   496     def _typed_value(self, form, load_bytes=False):
   500     def typed_value(self, form, load_bytes=False):
   497         if self.eidparam:
   501         if self.eidparam and self.role is not None:
   498             if form.edited_entity.has_eid():
   502             if form.edited_entity.has_eid():
   499                 if load_bytes:
   503                 if load_bytes:
   500                     return getattr(form.edited_entity, self.name)
   504                     return getattr(form.edited_entity, self.name)
   501                 # don't actually load data
   505                 # don't actually load data
   502                 # XXX value should reflect if some file is already attached
   506                 # XXX value should reflect if some file is already attached
   503                 # * try to display name metadata
   507                 # * try to display name metadata
   504                 # * check length(data) / data != null
   508                 # * check length(data) / data != null
   505                 return True
   509                 return True
   506             return False
   510             return False
   507         return super(FileField, self)._typed_value(form, load_bytes)
   511         return super(FileField, self).typed_value(form, load_bytes)
   508 
   512 
   509     def render(self, form, renderer):
   513     def render(self, form, renderer):
   510         wdgs = [self.get_widget(form).render(form, self, renderer)]
   514         wdgs = [self.get_widget(form).render(form, self, renderer)]
   511         if self.format_field or self.encoding_field:
   515         if self.format_field or self.encoding_field:
   512             divid = '%s-advanced' % self.input_name(form)
   516             divid = '%s-advanced' % self.input_name(form)
   521             if self.format_field:
   525             if self.format_field:
   522                 wdgs.append(self.render_subfield(form, self.format_field, renderer))
   526                 wdgs.append(self.render_subfield(form, self.format_field, renderer))
   523             if self.encoding_field:
   527             if self.encoding_field:
   524                 wdgs.append(self.render_subfield(form, self.encoding_field, renderer))
   528                 wdgs.append(self.render_subfield(form, self.encoding_field, renderer))
   525             wdgs.append(u'</div>')
   529             wdgs.append(u'</div>')
   526         if not self.required and self._typed_value(form):
   530         if not self.required and self.typed_value(form):
   527             # trick to be able to delete an uploaded file
   531             # trick to be able to delete an uploaded file
   528             wdgs.append(u'<br/>')
   532             wdgs.append(u'<br/>')
   529             wdgs.append(tags.input(name=self.input_name(form, u'__detach'),
   533             wdgs.append(tags.input(name=self.input_name(form, u'__detach'),
   530                                    type=u'checkbox'))
   534                                    type=u'checkbox'))
   531             wdgs.append(form._cw._('detach attached file'))
   535             wdgs.append(form._cw._('detach attached file'))
   792             # remove associated __linkto hidden fields
   796             # remove associated __linkto hidden fields
   793             for field in form.root_form.fields_by_name('__linkto'):
   797             for field in form.root_form.fields_by_name('__linkto'):
   794                 if field.value in searchedvalues:
   798                 if field.value in searchedvalues:
   795                     form.root_form.remove_field(field)
   799                     form.root_form.remove_field(field)
   796             form.formvalues[self] = value
   800             form.formvalues[self] = value
   797 
       
   798     def _typed_value(self, form, load_bytes=False):
       
   799         entity = form.edited_entity
       
   800         # non final relation field
       
   801         if entity.has_eid() or entity.relation_cached(self.name, self.role):
       
   802             return [r[0] for r in entity.related(self.name, self.role)]
       
   803         return ()
       
   804 
   801 
   805     def format_single_value(self, req, value):
   802     def format_single_value(self, req, value):
   806         return value
   803         return value
   807 
   804 
   808     def _process_form_value(self, form):
   805     def _process_form_value(self, form):