web/form.py
changeset 1935 a54a3b665361
parent 1861 21fae1546ac4
child 1962 45af1e015683
equal deleted inserted replaced
1934:be86bb31c4c2 1935:a54a3b665361
   259         self.context = None
   259         self.context = None
   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 _fieldsattr(cls_or_self):
   265         """return field with the given name and role"""
       
   266         if isinstance(cls_or_self, type):
   265         if isinstance(cls_or_self, type):
   267             fields = cls_or_self._fields_
   266             fields = cls_or_self._fields_
   268         else:
   267         else:
   269             fields = cls_or_self.fields
   268             fields = cls_or_self.fields
   270         for field in fields:
   269         return fields
       
   270 
       
   271     @iclassmethod
       
   272     def field_by_name(cls_or_self, name, role='subject'):
       
   273         """return field with the given name and role"""
       
   274         for field in cls_or_self._fieldsattr():
   271             if field.name == name and field.role == role:
   275             if field.name == name and field.role == role:
   272                 return field
   276                 return field
   273         raise FieldNotFound(name)
   277         raise FieldNotFound(name)
   274 
   278 
   275     @iclassmethod
   279     @iclassmethod
   276     def remove_field(cls_or_self, field):
   280     def remove_field(cls_or_self, field):
   277         """remove a field from form class or instance"""
   281         """remove a field from form class or instance"""
   278         if isinstance(cls_or_self, type):
   282         cls_or_self._fieldsattr().remove(field)
   279             fields = cls_or_self._fields_
       
   280         else:
       
   281             fields = cls_or_self.fields
       
   282         fields.remove(field)
       
   283 
   283 
   284     @iclassmethod
   284     @iclassmethod
   285     def append_field(cls_or_self, field):
   285     def append_field(cls_or_self, field):
   286         """append a field to form class or instance"""
   286         """append a field to form class or instance"""
   287         if isinstance(cls_or_self, type):
   287         cls_or_self._fieldsattr().append(field)
   288             fields = cls_or_self._fields_
   288 
   289         else:
   289     @iclassmethod
   290             fields = cls_or_self.fields
   290     def insert_field_before(cls_or_self, new_field, name, role='subject'):
   291         fields.append(field)
   291         field = cls_or_self.field_by_name(name, role)
       
   292         fields = cls_or_self._fieldsattr()
       
   293         fields.insert(fields.index(field), new_field)
       
   294 
       
   295     @iclassmethod
       
   296     def insert_field_after(cls_or_self, new_field, name, role='subject'):
       
   297         field = cls_or_self.field_by_name(name, role)
       
   298         fields = cls_or_self._fieldsattr()
       
   299         fields.insert(fields.index(field)+1, new_field)
   292 
   300 
   293     @property
   301     @property
   294     def form_needs_multipart(self):
   302     def form_needs_multipart(self):
   295         """true if the form needs enctype=multipart/form-data"""
   303         """true if the form needs enctype=multipart/form-data"""
   296         return any(field.needs_multipart for field in self.fields)
   304         return any(field.needs_multipart for field in self.fields)