Updated groups display mode API
authorThierry Florac <thierry.florac@onf.fr>
Wed, 30 May 2018 16:26:00 +0200
changeset 107 fec6b5f9601e
parent 106 2dbbb022d2e8
child 108 87ee710b7639
Updated groups display mode API
src/pyams_form/group.py
src/pyams_form/interfaces/form.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)
--- 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?")