# HG changeset patch # User Thierry Florac # Date 1425132646 -3600 # Node ID 0f135e3c84b1c7a6f87b4324c7a1b7446bf7e320 # Parent dfa0508e40550af21d41131fd00b3f0cf5f4a4e8 Updated inner sub-forms management diff -r dfa0508e4055 -r 0f135e3c84b1 src/pyams_form/form.py --- a/src/pyams_form/form.py Sat Feb 28 13:33:20 2015 +0100 +++ b/src/pyams_form/form.py Sat Feb 28 15:10:46 2015 +0100 @@ -18,7 +18,7 @@ # import interfaces from pyams_form.interfaces.form import IFormLayer, IForm, IAJAXForm, IInnerSubForm, IInnerTabForm, \ - ICustomUpdateSubForm, IFormCreatedEvent, FormCreatedEvent + ICustomUpdateSubForm, IFormCreatedEvent, FormCreatedEvent, IInnerForm from pyams_form.interfaces.form import IAddFormButtons, IModalAddFormButtons, IEditFormButtons, \ IModalEditFormButtons, IModalDisplayFormButtons from pyams_form.interfaces.form import FormObjectCreatedEvent, FormObjectModifiedEvent @@ -30,6 +30,7 @@ # import packages from pyams_form.group import GroupsBasedForm +from pyams_pagelet.interfaces import PageletCreatedEvent from pyams_skin.skin import apply_skin from pyramid.decorator import reify from pyramid.events import subscriber @@ -40,7 +41,7 @@ from z3c.form.form import applyChanges, \ Form, AddForm as BaseAddForm, EditForm as BaseEditForm, DisplayForm as BaseDisplayForm from zope.component import queryUtility -from zope.interface import implementer, alsoProvides +from zope.interface import implementer, alsoProvides, Interface from zope.lifecycleevent import Attributes, ObjectCreatedEvent from zope.schema.fieldproperty import FieldProperty @@ -104,13 +105,15 @@ @reify def subforms(self): registry = self.request.registry - return sorted((adapter[1] for adapter in registry.getAdapters((self,), IInnerSubForm)), + return sorted((adapter[1] + for adapter in registry.getAdapters((self.context, self.request, self), IInnerSubForm)), key=get_form_weight) @reify def tabforms(self): registry = self.request.registry - return sorted((adapter[1] for adapter in registry.getAdapters((self,), IInnerTabForm)), + return sorted((adapter[1] + for adapter in registry.getAdapters((self.context, self.request, self), IInnerTabForm)), key=get_form_weight) @property @@ -273,8 +276,13 @@ """AJAX add form""" def __call__(self): - self.updateWidgets() - data, errors = self.extractData() + self.request.registry.notify(PageletCreatedEvent(self)) + data, errors = {}, () + for form in self.forms: + form.updateWidgets() + form_data, form_errors = form.extractData() + data.update(form_data) + errors = errors + form_errors if errors or self.errors: return self.get_ajax_errors() result = self.createAndAdd(data) @@ -293,6 +301,24 @@ dialog_class = 'modal-medium' +@implementer(IInnerForm) +class InnerAddForm(AddForm): + """Inner add form""" + + @property + def id(self): + return self.__class__.__name__ + + buttons = Buttons(Interface) + + def __init__(self, context, request, view): + super(InnerAddForm, self).__init__(context, request) + self.parent_form = view + + def get_form_action(self): + return None + + # # Edit forms # @@ -327,8 +353,13 @@ """AJAX edit form""" def __call__(self): - self.updateWidgets() - data, errors = self.extractData() + self.request.registry.notify(PageletCreatedEvent(self)) + data, errors = {}, () + for form in self.forms: + form.updateWidgets() + form_data, form_errors = form.extractData() + data.update(form_data) + errors = errors + form_errors if errors or self.errors: return self.get_ajax_errors() changes = self.applyChanges(data) @@ -354,6 +385,24 @@ dialog_class = 'modal-medium' +@implementer(IInnerForm) +class InnerEditForm(EditForm): + """Inner edit form""" + + @property + def id(self): + return self.__class__.__name__ + + buttons = Buttons(Interface) + + def __init__(self, context, request, view): + super(InnerEditForm, self).__init__(context, request) + self.parent_form = view + + def get_form_action(self): + return None + + # # Display forms # diff -r dfa0508e4055 -r 0f135e3c84b1 src/pyams_form/group.py --- a/src/pyams_form/group.py Sat Feb 28 13:33:20 2015 +0100 +++ b/src/pyams_form/group.py Sat Feb 28 15:10:46 2015 +0100 @@ -30,6 +30,7 @@ id = FieldProperty(IFormWidgetsGroup['id']) widgets = FieldProperty(IFormWidgetsGroup['widgets']) + bordered = FieldProperty(IFormWidgetsGroup['bordered']) legend = FieldProperty(IFormWidgetsGroup['legend']) help = FieldProperty(IFormWidgetsGroup['help']) _css_class = FieldProperty(IFormWidgetsGroup['css_class']) @@ -39,11 +40,12 @@ checkbox_widget = FieldProperty(IFormWidgetsGroup['checkbox_widget']) hide_if_empty = FieldProperty(IFormWidgetsGroup['hide_if_empty']) - def __init__(self, id, widgets=None, legend=None, help=None, css_class='', switch=False, + def __init__(self, id, widgets=None, bordered=True, legend=None, help=None, css_class='', switch=False, checkbox_switch=False, checkbox_field=None, hide_if_empty=False): assert (not checkbox_switch) or checkbox_field, "You must define checkbox field when using checkbox switch" self.id = id self.widgets = widgets or [] + self.bordered = bordered self.legend = id if legend is None else legend self.help = help self._css_class = css_class @@ -67,7 +69,7 @@ if self.checkbox_field is None: return None for widget in self.widgets: - if widget.field is self.checkbox_field.field: + if widget.field is self.checkbox_field: return widget @reify @@ -94,7 +96,7 @@ @property def visible_widgets(self): for widget in self.widgets: - if (self.checkbox_field is None) or (widget.field is not self.checkbox_field.field): + if (self.checkbox_field is None) or (widget.field is not self.checkbox_field): yield widget @property @@ -110,10 +112,10 @@ return 'on' if self.visible else 'off' -def NamedWidgetsGroup(id, widgets, names=(), legend=None, help=None, css_class='', switch=False, +def NamedWidgetsGroup(id, widgets, names=(), bordered=True, legend=None, help=None, css_class='', switch=False, checkbox_switch=False, checkbox_field=None, hide_if_empty=False): """Create a widgets group based on widgets names""" - return FormWidgetsGroup(id, [widgets.get(name) for name in names], legend, help, css_class, switch, + return FormWidgetsGroup(id, [widgets.get(name) for name in names], bordered, legend, help, css_class, switch, checkbox_switch, checkbox_field, hide_if_empty) diff -r dfa0508e4055 -r 0f135e3c84b1 src/pyams_form/templates/form.pt --- a/src/pyams_form/templates/form.pt Sat Feb 28 13:33:20 2015 +0100 +++ b/src/pyams_form/templates/form.pt Sat Feb 28 15:10:46 2015 +0100 @@ -46,13 +46,18 @@ tal:content="structure prefix">Widgets prefix
+ tal:omit-tag="not:legend" + tal:attributes="class 'bordered' if group.bordered else None"> - Legend + data-ams-checker-fieldname '{0}:list'.format(group.checkbox_widget.name); + data-ams-checker-readonly 'readonly' if group.checkbox_widget.mode == 'display' else None; + data-ams-checker-marker '{0}-empty-marker'.format(group.checkbox_widget.name); + data-ams-checker-state group.checker_state;"> + +
-
diff -r dfa0508e4055 -r 0f135e3c84b1 src/pyams_form/templates/inner-form.pt --- a/src/pyams_form/templates/inner-form.pt Sat Feb 28 13:33:20 2015 +0100 +++ b/src/pyams_form/templates/inner-form.pt Sat Feb 28 15:10:46 2015 +0100 @@ -3,9 +3,11 @@ tal:replace="structure prefix">Form prefix
- -