# HG changeset patch # User Thierry Florac # Date 1527690360 -7200 # Node ID fec6b5f9601ee7df70ff170a76e451d02d594961 # Parent 2dbbb022d2e8eebe47348c774f579839ce7da38a Updated groups display mode API diff -r 2dbbb022d2e8 -r fec6b5f9601e src/pyams_form/group.py --- a/src/pyams_form/group.py Wed May 23 15:06:41 2018 +0200 +++ b/src/pyams_form/group.py Wed May 30 16:26:00 2018 +0200 @@ -41,14 +41,13 @@ switch = FieldProperty(IFormWidgetsGroup['switch']) checkbox_switch = FieldProperty(IFormWidgetsGroup['checkbox_switch']) checkbox_field = FieldProperty(IFormWidgetsGroup['checkbox_field']) - checkbox_widget = FieldProperty(IFormWidgetsGroup['checkbox_widget']) checkbox_mode = FieldProperty(IFormWidgetsGroup['checkbox_mode']) - hide_if_empty = FieldProperty(IFormWidgetsGroup['hide_if_empty']) + display_mode = FieldProperty(IFormWidgetsGroup['display_mode']) subforms_legend = FieldProperty(IFormWidgetsGroup['subforms_legend']) def __init__(self, form, id, widgets=None, bordered=True, fieldset_class=None, legend=None, help=None, css_class='', switch=False, checkbox_switch=False, checkbox_field=None, checkbox_mode='hide', - hide_if_empty=False): + display_mode='never'): assert (not checkbox_switch) or checkbox_field, "You must define checkbox field when using checkbox switch" self.form = form self.id = id @@ -62,7 +61,7 @@ self.checkbox_switch = checkbox_switch self.checkbox_field = checkbox_field self.checkbox_mode = checkbox_mode - self.hide_if_empty = hide_if_empty + self.display_mode = display_mode @property def css_class(self): @@ -91,22 +90,25 @@ value = getattr(widget.field.interface(context), name, None) return bool(value) else: - if not (self.switch and self.hide_if_empty): + if self.display_mode == 'never': + return False + elif (not self.switch) or (self.display_mode == 'always'): return True - for widget in self.widgets: - if not widget.ignoreContext: - field = widget.field - context = widget.context - name = field.getName() - value = getattr(field.interface(context), name, None) - if value and (value != field.default): - if II18nField.providedBy(field): - for i18n_value in value.values(): - if i18n_value: - return True - else: - return True - return False + else: # no switch or auto mode + for widget in self.widgets: + if not widget.ignoreContext: + field = widget.field + context = widget.context + name = field.getName() + value = getattr(field.interface(context), name, None) + if value and (value != field.default): + if II18nField.providedBy(field): + for i18n_value in value.values(): + if i18n_value: + return True + else: + return True + return False @property def visible_widgets(self): @@ -148,13 +150,13 @@ def NamedWidgetsGroup(form, id, widgets, names=(), bordered=True, fieldset_class=None, legend=None, help=None, css_class='', switch=False, checkbox_switch=False, checkbox_field=None, checkbox_mode='hide', - hide_if_empty=False, factory=FormWidgetsGroup): + display_mode='never', factory=FormWidgetsGroup): """Create a widgets group based on widgets names""" if widgets is None: form.updateWidgets() widgets = form.widgets return factory(form, id, [widgets.get(name) for name in names], bordered, fieldset_class, legend, help, - css_class, switch, checkbox_switch, checkbox_field, checkbox_mode, hide_if_empty) + css_class, switch, checkbox_switch, checkbox_field, checkbox_mode, display_mode) @implementer(IGroupsBasedForm) diff -r 2dbbb022d2e8 -r fec6b5f9601e src/pyams_form/interfaces/form.py --- a/src/pyams_form/interfaces/form.py Wed May 23 15:06:41 2018 +0200 +++ b/src/pyams_form/interfaces/form.py Wed May 30 16:26:00 2018 +0200 @@ -233,11 +233,13 @@ values=('disable', 'hide'), default='hide') - hide_if_empty = Bool(title="Hide group if empty?", - description="""If 'Yes', a switchable group containing only """ - """widgets with default values is hidden""", - required=True, - default=False) + display_mode = Choice(title="Group display mode", + description="""If 'auto', a switchable group containing only """ + """widgets with default values is hidden; if 'always', group is always + visible; if 'never', group is always hidden""", + values=('auto', 'never', 'always'), + required=True, + default='never') visible = Attribute("Visible group?")