web/form.py
branchstable
changeset 1947 8696403e5324
parent 1861 21fae1546ac4
child 1962 45af1e015683
equal deleted inserted replaced
1946:d60ddfc4bb09 1947:8696403e5324
   260         if 'domid' in kwargs:# session key changed
   260         if 'domid' in kwargs:# session key changed
   261             self.restore_previous_post(self.session_key())
   261             self.restore_previous_post(self.session_key())
   262 
   262 
   263     @iclassmethod
   263     @iclassmethod
   264     def field_by_name(cls_or_self, name, role='subject'):
   264     def field_by_name(cls_or_self, name, role='subject'):
   265         """return field with the given name and role"""
   265         """return field with the given name and role.
       
   266         Raise FieldNotFound if the field can't be found.
       
   267         """
   266         if isinstance(cls_or_self, type):
   268         if isinstance(cls_or_self, type):
   267             fields = cls_or_self._fields_
   269             fields = cls_or_self._fields_
   268         else:
   270         else:
   269             fields = cls_or_self.fields
   271             fields = cls_or_self.fields
   270         for field in fields:
   272         for field in fields:
   271             if field.name == name and field.role == role:
   273             if field.name == name and field.role == role:
   272                 return field
   274                 return field
   273         raise FieldNotFound(name)
   275         raise FieldNotFound(name)
       
   276 
       
   277     @iclassmethod
       
   278     def fields_by_name(cls_or_self, name, role='subject'):
       
   279         """return a list of fields with the given name and role"""
       
   280         if isinstance(cls_or_self, type):
       
   281             fields = cls_or_self._fields_
       
   282         else:
       
   283             fields = cls_or_self.fields
       
   284         return [field for field in fields
       
   285                 if field.name == name and field.role == role]
   274 
   286 
   275     @iclassmethod
   287     @iclassmethod
   276     def remove_field(cls_or_self, field):
   288     def remove_field(cls_or_self, field):
   277         """remove a field from form class or instance"""
   289         """remove a field from form class or instance"""
   278         if isinstance(cls_or_self, type):
   290         if isinstance(cls_or_self, type):
   331         """
   343         """
   332         self.context = context = {}
   344         self.context = context = {}
   333         # ensure rendervalues is a dict
   345         # ensure rendervalues is a dict
   334         if rendervalues is None:
   346         if rendervalues is None:
   335             rendervalues = {}
   347             rendervalues = {}
   336         for field in self.fields:
   348         # use a copy in case fields are modified while context is build (eg
       
   349         # __linkto handling for instance)
       
   350         for field in self.fields[:]:
   337             for field in field.actual_fields(self):
   351             for field in field.actual_fields(self):
   338                 field.form_init(self)
   352                 field.form_init(self)
   339                 value = self.form_field_display_value(field, rendervalues)
   353                 value = self.form_field_display_value(field, rendervalues)
   340                 context[field] = {'value': value,
   354                 context[field] = {'value': value,
   341                                   'name': self.form_field_name(field),
   355                                   'name': self.form_field_name(field),
   433         super(EntityFieldsForm, self).__init__(*args, **kwargs)
   447         super(EntityFieldsForm, self).__init__(*args, **kwargs)
   434         if self.edited_entity is None:
   448         if self.edited_entity is None:
   435             self.edited_entity = self.complete_entity(self.row or 0, self.col or 0)
   449             self.edited_entity = self.complete_entity(self.row or 0, self.col or 0)
   436         self.form_add_hidden('__type', eidparam=True)
   450         self.form_add_hidden('__type', eidparam=True)
   437         self.form_add_hidden('eid')
   451         self.form_add_hidden('eid')
   438         if msg is not None:
   452         if msg:
   439             # If we need to directly attach the new object to another one
   453             # If we need to directly attach the new object to another one
       
   454             self.form_add_hidden('__message', msg)
       
   455         if not self.is_subform:
   440             for linkto in self.req.list_form_param('__linkto'):
   456             for linkto in self.req.list_form_param('__linkto'):
   441                 self.form_add_hidden('__linkto', linkto)
   457                 self.form_add_hidden('__linkto', linkto)
   442                 msg = '%s %s' % (msg, self.req._('and linked'))
   458                 msg = '%s %s' % (msg, self.req._('and linked'))
   443             self.form_add_hidden('__message', msg)
       
   444         # in case of direct instanciation
   459         # in case of direct instanciation
   445         self.schema = self.edited_entity.schema
   460         self.schema = self.edited_entity.schema
   446         self.vreg = self.edited_entity.vreg
   461         self.vreg = self.edited_entity.vreg
   447 
   462 
   448     def _field_has_error(self, field):
   463     def _field_has_error(self, field):
   468         return res
   483         return res
   469 
   484 
   470     def _req_display_value(self, field):
   485     def _req_display_value(self, field):
   471         value = super(EntityFieldsForm, self)._req_display_value(field)
   486         value = super(EntityFieldsForm, self)._req_display_value(field)
   472         if value is None:
   487         if value is None:
   473             value = self.edited_entity.linked_to(field.name, field.role) or None
   488             value = self.edited_entity.linked_to(field.name, field.role)
       
   489             if value:
       
   490                 searchedvalues = ['%s:%s:%s' % (field.name, eid, field.role)
       
   491                                   for eid in value]
       
   492                 # remove associated __linkto hidden fields
       
   493                 for field in self.fields_by_name('__linkto'):
       
   494                     if field.initial in searchedvalues:
       
   495                         self.remove_field(field)
       
   496             else:
       
   497                 value = None
   474         return value
   498         return value
   475 
   499 
   476     def _form_field_default_value(self, field, load_bytes):
   500     def _form_field_default_value(self, field, load_bytes):
   477         defaultattr = 'default_%s' % field.name
   501         defaultattr = 'default_%s' % field.name
   478         if hasattr(self.edited_entity, defaultattr):
   502         if hasattr(self.edited_entity, defaultattr):