web/views/editforms.py
branchtls-sprint
changeset 1313 9cff1eee0208
parent 1305 395ef7f2b95b
child 1318 50e1a778c5ee
equal deleted inserted replaced
1312:df7f847e1388 1313:9cff1eee0208
    18 from cubicweb.selectors import (match_kwargs, one_line_rset, non_final_entity,
    18 from cubicweb.selectors import (match_kwargs, one_line_rset, non_final_entity,
    19                                 specified_etype_implements, yes)
    19                                 specified_etype_implements, yes)
    20 from cubicweb.utils import make_uid
    20 from cubicweb.utils import make_uid
    21 from cubicweb.view import EntityView
    21 from cubicweb.view import EntityView
    22 from cubicweb.common import tags
    22 from cubicweb.common import tags
    23 from cubicweb.web import INTERNAL_FIELD_VALUE, stdmsgs, formwidgets, uicfg
    23 from cubicweb.web import INTERNAL_FIELD_VALUE, stdmsgs, uicfg
    24 from cubicweb.web.form import (FieldNotFound, CompositeForm, EntityFieldsForm,
    24 from cubicweb.web.form import (FieldNotFound, CompositeForm, EntityFieldsForm,
    25                                FormMixIn)
    25                                FormMixIn)
    26 from cubicweb.web.formfields import guess_field
    26 from cubicweb.web.formfields import guess_field
    27 from cubicweb.web.formwidgets import Button, SubmitButton, ResetButton
    27 from cubicweb.web.formwidgets import Button, SubmitButton, ResetButton
    28 from cubicweb.web.formrenderers import (FormRenderer, EntityFormRenderer,
    28 from cubicweb.web.formrenderers import (FormRenderer, EntityFormRenderer,
   101         else:
   101         else:
   102             value = entity.printable_value(rtype)
   102             value = entity.printable_value(rtype)
   103         if not entity.has_perm('update'):
   103         if not entity.has_perm('update'):
   104             self.w(value)
   104             self.w(value)
   105             return
   105             return
   106         self.req.add_js( ('cubicweb.ajax.js',) )
       
   107         eid = entity.eid
   106         eid = entity.eid
   108         edit_key = make_uid('%s-%s' % (rtype, eid))
   107         edit_key = make_uid('%s-%s' % (rtype, eid))
   109         divid = 'd%s' % edit_key
   108         divid = 'd%s' % edit_key
   110         reload = dumps(reload)
   109         reload = dumps(reload)
   111         buttons = [SubmitButton(stdmsgs.BUTTON_OK, cwaction='apply'),
   110         buttons = [SubmitButton(stdmsgs.BUTTON_OK, cwaction='apply'),
   125 
   124 
   126 class AutomaticEntityForm(EntityFieldsForm):
   125 class AutomaticEntityForm(EntityFieldsForm):
   127     """base automatic form to edit any entity
   126     """base automatic form to edit any entity
   128 
   127 
   129     Designed to be flly generated from schema but highly configurable through:
   128     Designed to be flly generated from schema but highly configurable through:
   130     * rtags (rcategories, rwidgets, inlined, rpermissions)
   129     * rtags (rcategories, rfields, rwidgets, inlined, rpermissions)
   131     * various standard form parameters
   130     * various standard form parameters
   132 
   131 
   133     You can also easily customise it by adding/removing fields in
   132     You can also easily customise it by adding/removing fields in
   134     AutomaticEntityForm instances.
   133     AutomaticEntityForm instances.
   135     """
   134     """
   136     id = 'edition'
   135     id = 'edition'
   137     
   136 
   138     needs_js = EntityFieldsForm.needs_js + ('cubicweb.ajax.js',)
       
   139     cwtarget = 'eformframe'
   137     cwtarget = 'eformframe'
   140     cssclass = 'entityForm'
   138     cssclass = 'entityForm'
   141     copy_nav_params = True
   139     copy_nav_params = True
   142     form_buttons = [SubmitButton(stdmsgs.BUTTON_OK),
   140     form_buttons = [SubmitButton(stdmsgs.BUTTON_OK),
   143                     Button(stdmsgs.BUTTON_APPLY, cwaction='apply'),
   141                     Button(stdmsgs.BUTTON_APPLY, cwaction='apply'),
   144                     Button(stdmsgs.BUTTON_CANCEL, cwaction='cancel')]    
   142                     Button(stdmsgs.BUTTON_CANCEL, cwaction='cancel')]    
   145     attrcategories = ('primary', 'secondary')
   143     attrcategories = ('primary', 'secondary')
   146     # class attributes below are actually stored in the uicfg module since we
   144     # class attributes below are actually stored in the uicfg module since we
   147     # don't want them to be reloaded
   145     # don't want them to be reloaded
   148     rcategories = uicfg.rcategories
   146     rcategories = uicfg.rcategories
       
   147     rfields = uicfg.rfields
   149     rwidgets = uicfg.rwidgets
   148     rwidgets = uicfg.rwidgets
   150     rinlined = uicfg.rinlined
   149     rinlined = uicfg.rinlined
   151     rpermissions_overrides = uicfg.rpermissions_overrides
   150     rpermissions_overrides = uicfg.rpermissions_overrides
   152         
   151         
   153     @classmethod
   152     @classmethod
   280             try:
   279             try:
   281                 self.field_by_name(rschema.type, role)
   280                 self.field_by_name(rschema.type, role)
   282                 continue # explicitly specified
   281                 continue # explicitly specified
   283             except FieldNotFound:
   282             except FieldNotFound:
   284                 pass # has to be guessed
   283                 pass # has to be guessed
   285             wdgname = self.rwidgets.etype_rtag(self.edited_entity.id, rschema,
   284             fieldcls = self.rfields.etype_rtag(self.edited_entity.id, rschema,
   286                                                role)
   285                                                role)
   287             if wdgname:
   286             if fieldcls:
   288                 widget = getattr(formwidgets, wdgname)
   287                 field = fieldcls(name=rschema.type, role=role, eidparam=True)
       
   288                 self.fields.append(field)
       
   289                 continue
       
   290             widget = self.rwidgets.etype_rtag(self.edited_entity.id, rschema,
       
   291                                               role)
       
   292             if widget:
   289                 field = guess_field(self.edited_entity.__class__, rschema, role,
   293                 field = guess_field(self.edited_entity.__class__, rschema, role,
   290                                     eidparam=True, widget=widget)
   294                                     eidparam=True, widget=widget)
   291             else:
   295             else:
   292                 field = guess_field(self.edited_entity.__class__, rschema, role,
   296                 field = guess_field(self.edited_entity.__class__, rschema, role,
   293                                     eidparam=True)
   297                                     eidparam=True)