[uicfg] give a set_fields_order method to the primary view display control
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Mon, 21 Oct 2013 09:58:26 +0200
changeset 9341 099a3a33eaaa
parent 9340 b1e933b0e850
child 9342 e148583db39e
[uicfg] give a set_fields_order method to the primary view display control Hence it is nicely symmetrical with the fields kwargs. Closes #2741963.
doc/3.18.rst
web/test/unittest_uicfg.py
web/views/uicfg.py
--- a/doc/3.18.rst	Mon Dec 09 16:13:10 2013 +0100
+++ b/doc/3.18.rst	Mon Oct 21 09:58:26 2013 +0200
@@ -7,6 +7,9 @@
 * add a security debugging tool
   (see `#2920304 <http://www.cubicweb.org/2920304>`_)
 
+* the primary view display controller (uicfg) now has a
+  `set_fields_order` method similar to the one available for forms
+
 
 API changes
 -----------
--- a/web/test/unittest_uicfg.py	Mon Dec 09 16:13:10 2013 +0100
+++ b/web/test/unittest_uicfg.py	Mon Oct 21 09:58:26 2013 +0200
@@ -73,6 +73,14 @@
         uihelper.set_fields_order('CWUser', ('login', 'firstname', 'surname'))
         self.assertEqual(afk_get('CWUser', 'firstname', 'String', 'subject'), {'order': 1})
 
+    @tag('uicfg', 'order', 'func')
+    def test_uicfg_primaryview_set_fields_order(self):
+        pvdc = uicfg.primaryview_display_ctrl
+        pvdc.set_fields_order('CWUser', ('login', 'firstname', 'surname'))
+        self.assertEqual(pvdc.get('CWUser', 'login', 'String', 'subject'), {'order': 0})
+        self.assertEqual(pvdc.get('CWUser', 'firstname', 'String', 'subject'), {'order': 1})
+        self.assertEqual(pvdc.get('CWUser', 'surname', 'String', 'subject'), {'order': 2})
+
     @tag('uihelper', 'kwargs', 'func')
     def test_uihelper_set_field_kwargs(self):
         afk_get = uicfg.autoform_field_kwargs.get
--- a/web/views/uicfg.py	Mon Dec 09 16:13:10 2013 +0100
+++ b/web/views/uicfg.py	Mon Oct 21 09:58:26 2013 +0200
@@ -114,6 +114,36 @@
                         'order',
                         self.counter)
 
+    def set_fields_order(self, etype, relations):
+        """specify the field order in `etype` primary view.
+
+        :param etype: the entity type as a string
+        :param attrs: the ordered list of attribute names (or relations)
+
+        `attrs` can be strings or 2-tuples (relname, role_of_etype_in_the_rel)
+
+        Unspecified fields will be displayed after specified ones, their
+        order being consistent with the schema definition.
+
+        Examples:
+
+        .. sourcecode:: python
+
+          from cubicweb.web.views.uicfg import primaryview_display_ctrl as pvdc
+          pvdc.set_fields_order('CWUser', ('firstname', ('in_group', 'subject'),
+                                           'surname', 'login'))
+
+        """
+        for index, relation in enumerate(relations):
+            if not isinstance(relation, tuple):
+                relation = (relation, 'subject')
+            rtype, role = relation
+            if role == 'subject':
+                self.tag_subject_of((etype, rtype, '*'), {'order': index})
+            else:
+                self.tag_object_of((etype, rtype, '*'), {'order': index})
+
+
 primaryview_display_ctrl = DisplayCtrlRelationTags()