web/views/forms.py
branchstable
changeset 3512 2ceaa4e40348
parent 3510 bf746bf4a394
child 3513 c002f6488631
equal deleted inserted replaced
3511:581de819106f 3512:2ceaa4e40348
     8 __docformat__ = "restructuredtext en"
     8 __docformat__ = "restructuredtext en"
     9 
     9 
    10 from warnings import warn
    10 from warnings import warn
    11 
    11 
    12 from logilab.common.compat import any
    12 from logilab.common.compat import any
    13 from logilab.common.decorators import iclassmethod
       
    14 
    13 
    15 from cubicweb.selectors import non_final_entity, match_kwargs, one_line_rset
    14 from cubicweb.selectors import non_final_entity, match_kwargs, one_line_rset
    16 from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param
    15 from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param
    17 from cubicweb.web import form, formwidgets as fwdgs
    16 from cubicweb.web import form, formwidgets as fwdgs
    18 from cubicweb.web.controller import NAV_FORM_PARAMETERS
    17 from cubicweb.web.controller import NAV_FORM_PARAMETERS
    98         if submitmsg is not None:
    97         if submitmsg is not None:
    99             self.form_add_hidden('__message', submitmsg)
    98             self.form_add_hidden('__message', submitmsg)
   100         self.context = None
    99         self.context = None
   101         if 'domid' in kwargs:# session key changed
   100         if 'domid' in kwargs:# session key changed
   102             self.restore_previous_post(self.session_key())
   101             self.restore_previous_post(self.session_key())
   103 
       
   104     @iclassmethod
       
   105     def _fieldsattr(cls_or_self):
       
   106         if isinstance(cls_or_self, type):
       
   107             fields = cls_or_self._fields_
       
   108         else:
       
   109             fields = cls_or_self.fields
       
   110         return fields
       
   111 
       
   112     @iclassmethod
       
   113     def field_by_name(cls_or_self, name, role='subject'):
       
   114         """return field with the given name and role.
       
   115         Raise FieldNotFound if the field can't be found.
       
   116         """
       
   117         for field in cls_or_self._fieldsattr():
       
   118             if field.name == name and field.role == role:
       
   119                 return field
       
   120         raise form.FieldNotFound(name)
       
   121 
       
   122     @iclassmethod
       
   123     def fields_by_name(cls_or_self, name, role='subject'):
       
   124         """return a list of fields with the given name and role"""
       
   125         return [field for field in cls_or_self._fieldsattr()
       
   126                 if field.name == name and field.role == role]
       
   127 
       
   128     @iclassmethod
       
   129     def remove_field(cls_or_self, field):
       
   130         """remove a field from form class or instance"""
       
   131         cls_or_self._fieldsattr().remove(field)
       
   132 
       
   133     @iclassmethod
       
   134     def append_field(cls_or_self, field):
       
   135         """append a field to form class or instance"""
       
   136         cls_or_self._fieldsattr().append(field)
       
   137 
       
   138     @iclassmethod
       
   139     def insert_field_before(cls_or_self, new_field, name, role='subject'):
       
   140         field = cls_or_self.field_by_name(name, role)
       
   141         fields = cls_or_self._fieldsattr()
       
   142         fields.insert(fields.index(field), new_field)
       
   143 
       
   144     @iclassmethod
       
   145     def insert_field_after(cls_or_self, new_field, name, role='subject'):
       
   146         field = cls_or_self.field_by_name(name, role)
       
   147         fields = cls_or_self._fieldsattr()
       
   148         fields.insert(fields.index(field)+1, new_field)
       
   149 
   102 
   150     @property
   103     @property
   151     def form_needs_multipart(self):
   104     def form_needs_multipart(self):
   152         """true if the form needs enctype=multipart/form-data"""
   105         """true if the form needs enctype=multipart/form-data"""
   153         return any(field.needs_multipart for field in self.fields)
   106         return any(field.needs_multipart for field in self.fields)