# HG changeset patch # User Sylvain Thénault # Date 1253603835 -7200 # Node ID b02df886eb3e6cdd20e22ca0bc9f457a9b862e8d # Parent 5562f8b901f07e59936de5b632451605e8f38d5c cleanup, reorganize diff -r 5562f8b901f0 -r b02df886eb3e web/views/autoform.py --- a/web/views/autoform.py Tue Sep 22 09:17:03 2009 +0200 +++ b/web/views/autoform.py Tue Sep 22 09:17:15 2009 +0200 @@ -46,6 +46,8 @@ rinlined = uicfg.autoform_is_inlined rpermissions_overrides = uicfg.autoform_permissions_overrides + # class methods mapping schema relations to fields in the form ############ + @classmethod def erelations_by_category(cls, entity, categories=None, permission=None, rtags=None, strict=False): @@ -158,6 +160,8 @@ raise return field + # base automatic entity form methods ####################################### + def __init__(self, *args, **kwargs): super(AutomaticEntityForm, self).__init__(*args, **kwargs) entity = self.edited_entity @@ -185,6 +189,51 @@ return None return self.maxrelitems + 1 + @property + def form_needs_multipart(self): + """true if the form needs enctype=multipart/form-data""" + if super(AutomaticEntityForm, self).form_needs_multipart: + return True + # take a look at inlined forms to check (recursively) if they + # need multipart handling. + # XXX: this is very suboptimal because inlined forms will be + # selected / instantiated twice : here and during form rendering. + # Potential solutions: + # -> use subforms for inlined forms to get easiser access + # -> use a simple onload js function to check if there is + # a input type=file in the form + # -> generate the
node when the content is rendered + # and we know the correct enctype (formrenderer's w attribute + # is not a StringIO) + for rschema, targettypes, role in self.inlined_relations(): + # inlined forms don't handle multiple target types + if len(targettypes) != 1: + continue + targettype = targettypes[0] + if self.should_inline_relation_form(rschema, targettype, role): + entity = self.vreg['etypes'].etype_class(targettype)(self.req) + subform = self.vreg['forms'].select('edition', self.req, entity=entity) + if subform.form_needs_multipart: + return True + return False + + def action(self): + """return the form's action attribute. Default to validateform if not + explicitly overriden. + """ + try: + return self._action + except AttributeError: + return self.build_url('validateform') + + def set_action(self, value): + """override default action""" + self._action = value + + action = property(action, set_action) + + # methods mapping edited entity relations to fields in the form ############ + def relations_by_category(self, categories=None, permission=None): """return a list of (relation schema, target schemas, role) matching given category(ies) and permission @@ -211,26 +260,13 @@ return self.esrelations_by_category(self.edited_entity, categories, permission, strict=strict) - def action(self): - """return the form's action attribute. Default to validateform if not - explicitly overriden. - """ - try: - return self._action - except AttributeError: - return self.build_url('validateform') - - def set_action(self, value): - """override default action""" - self._action = value - - action = property(action, set_action) - def editable_attributes(self): """return a list of (relation schema, role) to edit for the entity""" return [(rschema, role) for rschema, _, role in self.relations_by_category( self.attrcategories, 'add') if rschema != 'eid'] + # generic relations modifier ############################################### + def relations_table(self): """yiels 3-tuples (rtype, target, related_list) where itself a list of : @@ -287,7 +323,7 @@ eview = '%s (%s)' % (eview, display_name(self.req, 'Basket')) yield rtype, pendingid, jscall, label, reid, eview - # should_* method extracted to allow overriding + # inlined forms support #################################################### def should_inline_relation_form(self, rschema, targettype, role): """return true if the given relation with entity has role and a @@ -324,6 +360,10 @@ def display_inline_creation_form(self, w, rschema, targettype, role, i18nctx): + """display inline forms to a newly related (hence created) entity. + + Return True if some inlined form are actually displayed + """ entity = self.edited_entity w(self.view('inline-creation', None, etype=targettype, peid=entity.eid, ptype=entity.e_schema, @@ -346,33 +386,6 @@ """ return card in '1?' - @property - def form_needs_multipart(self): - """true if the form needs enctype=multipart/form-data""" - if super(AutomaticEntityForm, self).form_needs_multipart: - return True - # take a look at inlined forms to check (recursively) if they - # need multipart handling. - # XXX: this is very suboptimal because inlined forms will be - # selected / instantiated twice : here and during form rendering. - # Potential solutions: - # -> use subforms for inlined forms to get easiser access - # -> use a simple onload js function to check if there is - # a input type=file in the form - # -> generate the node when the content is rendered - # and we know the correct enctype (formrenderer's w attribute - # is not a StringIO) - for rschema, targettypes, role in self.inlined_relations(): - # inlined forms don't handle multiple target types - if len(targettypes) != 1: - continue - targettype = targettypes[0] - if self.should_inline_relation_form(rschema, targettype, role): - entity = self.vreg['etypes'].etype_class(targettype)(self.req) - subform = self.vreg['forms'].select('edition', self.req, entity=entity) - if subform.form_needs_multipart: - return True - return False def etype_relation_field(etype, rtype, role='subject'): eschema = AutomaticEntityForm.schema.eschema(etype) diff -r 5562f8b901f0 -r b02df886eb3e web/views/formrenderers.py --- a/web/views/formrenderers.py Tue Sep 22 09:17:03 2009 +0200 +++ b/web/views/formrenderers.py Tue Sep 22 09:17:15 2009 +0200 @@ -470,6 +470,9 @@ w(u'') w(u'') + # NOTE: should_* and display_* method extracted and moved to the form to + # ease overriding + def inline_entities_form(self, w, form): """create a form to edit entity's inlined relations""" if not hasattr(form, 'inlined_relations'):