Simplify rule inheritance control of simplified profiles
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 13 Oct 2016 18:01:50 +0200
changeset 1835 fa0a0bb2da4b
parent 1834 d3e384864e8f
child 1836 86da8574b021
Simplify rule inheritance control of simplified profiles We may hide the ignore all rules / ignore one rule complexity since there may be only one inherited rule. Do several tweaks to make it nice. Note it doesn't work yet until the parent rule entity isn't created, because we are not able to detect the parent transfer...
i18n/fr.po
test/test_views.py
views/simplified.py
--- a/i18n/fr.po	Thu Oct 13 13:03:10 2016 +0200
+++ b/i18n/fr.po	Thu Oct 13 18:01:50 2016 +0200
@@ -1329,7 +1329,7 @@
 msgstr "objets-données physiques"
 
 msgid "SEDAPreventInheritance"
-msgstr "ignorer toutes les règles héritées"
+msgstr "ignorer les règles héritées"
 
 msgctxt ""
 "inlined:SEDAAltAccessRulePreventInheritance.seda_prevent_inheritance.object"
@@ -3217,7 +3217,7 @@
 msgstr ""
 
 msgid "add a SEDAPreventInheritance"
-msgstr "ignorer toutes les règles héritées"
+msgstr "ignorer les règles héritées"
 
 msgctxt ""
 "inlined:SEDAAltAccessRulePreventInheritance.seda_prevent_inheritance.object"
--- a/test/test_views.py	Thu Oct 13 13:03:10 2016 +0200
+++ b/test/test_views.py	Thu Oct 13 18:01:50 2016 +0200
@@ -291,6 +291,17 @@
                 self.assertNoRemove(form, 'seda_start_date', 'object')
                 self.assertNoTitle(form, 'seda_start_date', 'object')
 
+    def test_prevent_inheritance_form(self):
+        with self.admin_access.web_request() as req:
+            for rule_type in ('access', 'appraisal'):
+                form = self.rule_form(req, 'SEDAAlt{0}RulePreventInheritance'.format(
+                    rule_type.capitalize()))
+                self.assertInlinedFields(form, [
+                    ('seda_prevent_inheritance', 'PreventInheritanceInlineEntityCreationFormView'),
+                ])
+                self.assertNoRemove(form, 'seda_prevent_inheritance', 'object')
+                self.assertEqual(form.form_renderer_id, 'not-an-alt')
+
 
 if __name__ == '__main__':
     unittest.main()
--- a/views/simplified.py	Thu Oct 13 13:03:10 2016 +0200
+++ b/views/simplified.py	Thu Oct 13 18:01:50 2016 +0200
@@ -18,7 +18,7 @@
 from logilab.common.registry import objectify_predicate
 
 from cubicweb.predicates import is_instance
-from cubicweb.web.views import uicfg, autoform
+from cubicweb.web.views import uicfg, autoform, formrenderers
 
 from cubes.seda.entities import simplified_profile
 from cubes.seda.views import copy_rtag
@@ -123,3 +123,63 @@
                   & simplified_start_date())
     removejs = None
     form_renderer_id = 'notitle'
+
+
+# simplified profil will have a single appraisal/access rule, hence 'ignore inherited rules' is
+# enough, no need for explicit rule deactivation. This implies:
+# 1. hide explicit rule deactivation
+# 2. force appearance (hence choice) of the ignore all rules entity
+# 3. adapt alternative's title, so it doesn't appear like an alternative
+
+# 1. hide explicit rule deactivation
+simplified_afs.tag_object_of(('*', 'seda_ref_non_rule_id_from', '*'),
+                             'main', 'hidden')
+
+
+class AltInheritanceAutomaticEntityForm(autoform.AutomaticEntityForm):
+    __select__ = (is_instance('SEDAAltAccessRulePreventInheritance',
+                              'SEDAAltAppraisalRulePreventInheritance')
+                  & simplified_profile())
+
+    # 3. adapt alternative's title, so it doesn't appear like an alternative
+    def __init__(self, *args, **kwargs):
+        super(AltInheritanceAutomaticEntityForm, self).__init__(*args, **kwargs)
+        self.form_renderer_id = 'not-an-alt'
+
+    # 2. force appearance (hence choice) of the ignore all rules entity
+    def should_display_inline_creation_form(self, rschema, existing, card):
+        if not existing and rschema == 'seda_prevent_inheritance':
+            return True
+        return super(AltInheritanceAutomaticEntityForm, self).should_display_inline_creation_form(
+            rschema, existing, card)
+
+
+# 2. force appearance (hence choice) of the ignore all rules entity
+
+@objectify_predicate
+def simplified_prevent_inheritance(cls, req, rtype=None, pform=None, **kwargs):
+    if isinstance(pform, AltInheritanceAutomaticEntityForm) and rtype == 'seda_prevent_inheritance':
+        return 1
+    return 0
+
+
+class PreventInheritanceInlineEntityEditionFormView(autoform.InlineEntityEditionFormView):
+    __select__ = (autoform.InlineEntityEditionFormView.__select__
+                  & simplified_prevent_inheritance())
+    removejs = None
+
+
+class PreventInheritanceInlineEntityCreationFormView(autoform.InlineEntityCreationFormView):
+    __select__ = (autoform.InlineEntityCreationFormView.__select__
+                  & simplified_prevent_inheritance())
+    removejs = None
+
+
+class NotAnAltEntityInlinedFormRenderer(formrenderers.EntityInlinedFormRenderer):
+    """Custom form renderer that remove 'Alternative :' from an alternative's inlined form's title.
+    """
+    __regid__ = 'not-an-alt'
+
+    def render_title(self, w, form, values):
+        values['title'] = values['title'].split(':', 1)[1]
+        super(NotAnAltEntityInlinedFormRenderer, self).render_title(w, form, values)