--- a/src/pyams_form/form.py Wed Jul 01 10:10:36 2015 +0200
+++ b/src/pyams_form/form.py Wed Jul 01 10:11:16 2015 +0200
@@ -21,6 +21,7 @@
ICustomUpdateSubForm, IFormCreatedEvent, FormCreatedEvent, IInnerForm, IFormContextPermissionChecker
from pyams_form.interfaces.form import IAddFormButtons, IModalAddFormButtons, IEditFormButtons, \
IModalEditFormButtons, IModalDisplayFormButtons
+from pyams_form.interfaces import get_form_weight
from pyams_form.interfaces.form import FormObjectCreatedEvent, FormObjectModifiedEvent
from pyams_i18n.interfaces import II18n
from pyams_skin.interfaces import IDialog, ISkinnable, IContentTitle
@@ -52,14 +53,6 @@
from pyams_form import _
-def get_form_weight(form):
- """Try to get form weight attribute"""
- try:
- return form.weight
- except AttributeError:
- return 0
-
-
REDIRECT_STATUS_CODES = (300, 301, 302, 303, 304, 305, 307)
@@ -177,9 +170,12 @@
def update_content(self, content, data):
changes = applyChanges(self, content, data)
- for subform in self.subforms + self.tabforms:
- if ICustomUpdateSubForm.providedBy(subform):
- updates = ICustomUpdateSubForm(subform).update_content(content, data)
+ subforms = self.subforms + self.tabforms
+ [subforms.extend(group.subforms) for group in self.groups]
+ for subform in subforms:
+ subform_update = ICustomUpdateSubForm(subform, None)
+ if subform_update is not None:
+ updates = subform_update.update_content(content, data)
if isinstance(updates, dict):
changes.update(updates)
else:
@@ -350,6 +346,8 @@
class InnerAddForm(AddForm):
"""Inner add form"""
+ css_class = 'inner'
+
@property
def id(self):
return self.__class__.__name__
@@ -435,6 +433,8 @@
class InnerEditForm(EditForm):
"""Inner edit form"""
+ css_class = 'inner'
+
@property
def id(self):
return self.__class__.__name__
@@ -469,6 +469,8 @@
class InnerDisplayForm(DisplayForm):
"""Inner display form"""
+ css_class = 'inner'
+
@property
def id(self):
return self.__class__.__name__
--- a/src/pyams_form/group.py Wed Jul 01 10:10:36 2015 +0200
+++ b/src/pyams_form/group.py Wed Jul 01 10:11:16 2015 +0200
@@ -16,7 +16,8 @@
# import standard library
# import interfaces
-from pyams_form.interfaces.form import IFormWidgetsGroup, IGroupsBasedForm
+from pyams_form.interfaces import get_form_weight
+from pyams_form.interfaces.form import IFormWidgetsGroup, IGroupsBasedForm, IInnerSubForm
# import packages
from pyramid.decorator import reify
@@ -28,6 +29,7 @@
class FormWidgetsGroup(object):
"""Form widgets group"""
+ form = None
id = FieldProperty(IFormWidgetsGroup['id'])
widgets = FieldProperty(IFormWidgetsGroup['widgets'])
bordered = FieldProperty(IFormWidgetsGroup['bordered'])
@@ -39,14 +41,16 @@
checkbox_field = FieldProperty(IFormWidgetsGroup['checkbox_field'])
checkbox_widget = FieldProperty(IFormWidgetsGroup['checkbox_widget'])
hide_if_empty = FieldProperty(IFormWidgetsGroup['hide_if_empty'])
+ subforms_legend = FieldProperty(IFormWidgetsGroup['subforms_legend'])
- def __init__(self, id, widgets=None, bordered=True, legend=None, help=None, css_class='', switch=False,
+ def __init__(self, form, 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.form = form
self.id = id
self.widgets = widgets or []
self.bordered = bordered
- self.legend = id if legend is None else legend
+ self.legend = legend
self.help = help
self._css_class = css_class
self.switch = switch
@@ -78,7 +82,7 @@
widget = self.checkbox_widget
context = widget.context
name = widget.field.getName()
- value = getattr(context, name, None)
+ value = getattr(widget.field.interface(context), name, None)
return bool(value)
else:
if not (self.switch and self.hide_if_empty):
@@ -88,7 +92,7 @@
field = widget.field
context = widget.context
name = field.getName()
- value = getattr(context, name, None)
+ value = getattr(field.interface(context), name, None)
if value and (value != field.default):
return True
return False
@@ -111,11 +115,19 @@
def checker_state(self):
return 'on' if self.visible else 'off'
+ @reify
+ def subforms(self):
+ registry = self.form.request.registry
+ return sorted((adapter[1]
+ for adapter in registry.getAdapters((self.form.context, self.form.request, self),
+ IInnerSubForm)),
+ key=get_form_weight)
-def NamedWidgetsGroup(id, widgets, names=(), bordered=True, legend=None, help=None, css_class='', switch=False,
+
+def NamedWidgetsGroup(form, 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], bordered, legend, help, css_class, switch,
+ return FormWidgetsGroup(form, id, [widgets.get(name) for name in names], bordered, legend, help, css_class, switch,
checkbox_switch, checkbox_field, hide_if_empty)
@@ -126,6 +138,9 @@
Should be used as a base class for forms also implementing IForm
"""
+ main_group_legend = FieldProperty(IGroupsBasedForm['main_group_legend'])
+ main_group_class = FieldProperty(IGroupsBasedForm['main_group_class'])
+
def __init__(self):
self._groups = []
@@ -145,5 +160,7 @@
if not found:
others.append(widget)
if others:
- result.insert(0, FormWidgetsGroup(None, others))
+ result.insert(0, FormWidgetsGroup(self, None, others,
+ legend=self.main_group_legend,
+ css_class=self.main_group_class))
return result