[web/uihelper,uicfg] transform uihelper functions into uicfg objects methods (closes #2543949)
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Thu, 10 Jan 2013 18:11:33 +0100
changeset 8667 5a394fc419b4
parent 8666 1dd655788ece
child 8668 4fea61c636b2
[web/uihelper,uicfg] transform uihelper functions into uicfg objects methods (closes #2543949)
rtags.py
web/uihelper.py
web/views/uicfg.py
--- a/rtags.py	Mon Jan 21 16:34:09 2013 +0100
+++ b/rtags.py	Thu Jan 10 18:11:33 2013 +0100
@@ -144,6 +144,17 @@
         self._tagdefs[_ensure_str_key(key)] = tag
         return tag
 
+    def _tag_etype_attr(self, etype, attr, desttype='*', *args, **kwargs):
+        if isinstance(attr, basestring):
+            attr, role = attr, 'subject'
+        else:
+            attr, role = attr
+        if role == 'subject':
+            self.tag_subject_of((etype, attr, desttype), *args, **kwargs)
+        else:
+            self.tag_object_of((desttype, attr, etype), *args, **kwargs)
+
+
     # rtag runtime api ########################################################
 
     def del_rtag(self, *key):
--- a/web/uihelper.py	Mon Jan 21 16:34:09 2013 +0100
+++ b/web/uihelper.py	Thu Jan 10 18:11:33 2013 +0100
@@ -1,4 +1,4 @@
-# copyright 2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2011-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -45,207 +45,30 @@
 """
 __docformat__ = "restructuredtext en"
 
+
+from logilab.common.deprecation import deprecated
 from cubicweb.web.views import uicfg
-from functools import partial
-
-def _tag_rel(rtag, etype, attr, desttype='*', *args, **kwargs):
-    if isinstance(attr, basestring):
-        attr, role = attr, 'subject'
-    else:
-        attr, role = attr
-    if role == 'subject':
-        rtag.tag_subject_of((etype, attr, desttype), *args, **kwargs)
-    else:
-        rtag.tag_object_of((desttype, attr, etype), *args, **kwargs)
 
 
 ## generic uicfg helpers ######################################################
-def append_to_addmenu(etype, attr, createdtype='*'):
-    """adds `attr` in the actions box *addrelated* submenu of `etype`.
-
-    :param etype: the entity type as a string
-    :param attr: the name of the attribute or relation to hide
-
-    `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
-
-    """
-    _tag_rel(uicfg.actionbox_appearsin_addmenu, etype, attr, createdtype, True)
-
-def remove_from_addmenu(etype, attr, createdtype='*'):
-    """removes `attr` from the actions box *addrelated* submenu of `etype`.
-
-    :param etype: the entity type as a string
-    :param attr: the name of the attribute or relation to hide
-
-    `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
-    """
-    _tag_rel(uicfg.actionbox_appearsin_addmenu, etype, attr, createdtype, False)
-
-
-## form uicfg helpers ##########################################################
-def set_fields_order(etype, attrs):
-    """specify the field order in `etype` main edition form.
-
-    :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_relation)
-
-    Unspecified fields will be displayed after specified ones, their
-    order being consistent with the schema definition.
-
-    Examples:
-
-.. sourcecode:: python
-
-  from cubicweb.web import uihelper
-  uihelper.set_fields_order('CWUser', ('firstname', 'surname', 'login'))
-  uihelper.set_fields_order('CWUser', ('firstname', ('in_group', 'subject'), 'surname', 'login'))
-
-    """
-    afk = uicfg.autoform_field_kwargs
-    for index, attr in enumerate(attrs):
-        _tag_rel(afk, etype, attr, '*', {'order': index})
-
-
-def hide_field(etype, attr, desttype='*', formtype='main'):
-    """hide `attr` in `etype` forms.
-
-    :param etype: the entity type as a string
-    :param attr: the name of the attribute or relation to hide
-    :param formtype: which form will be affected ('main', 'inlined', etc.), *main* by default.
-
-    `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
-
-    Examples:
-
-.. sourcecode:: python
-
-  from cubicweb.web import uihelper
-  uihelper.hide_field('CWUser', 'login')
-  uihelper.hide_field('*', 'name')
-  uihelper.hide_field('CWUser', 'use_email', formtype='inlined')
-
-    """
-    _tag_rel(uicfg.autoform_section, etype, attr, desttype,
-             formtype=formtype, section='hidden')
-
-
-def hide_fields(etype, attrs, formtype='main'):
-    """simple for-loop wrapper around :func:`hide_field`.
-
-    :param etype: the entity type as a string
-    :param attrs: the ordered list of attribute names (or relations)
-    :param formtype: which form will be affected ('main', 'inlined', etc.), *main* by default.
-
-    `attrs` can be strings or 2-tuples (relname, role_of_etype_in_the_relation)
-
-    Examples:
-
-.. sourcecode:: python
-
-  from cubicweb.web import uihelper
-  uihelper.hide_fields('CWUser', ('login', ('use_email', 'subject')), formtype='inlined')
-    """
-    for attr in attrs:
-        hide_field(etype, attr, formtype=formtype)
-
 
-def set_field_kwargs(etype, attr, **kwargs):
-    """tag `attr` field of `etype` with additional named paremeters.
-
-    :param etype: the entity type as a string
-    :param attr: the name of the attribute or relation
-
-    `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
-
-    Examples:
-
-.. sourcecode:: python
-
-  from cubicweb.web import uihelper, formwidgets as fwdgs
-
-  uihelper.set_field_kwargs('Person', 'works_for', widget=fwdgs.AutoCompletionWidget())
-  uihelper.set_field_kwargs('CWUser', 'login', label=_('login or email address'),
-                            widget=fwdgs.TextInput(attrs={'size': 30}))
-    """
-    _tag_rel(uicfg.autoform_field_kwargs, etype, attr, '*', kwargs)
-
-
-def set_field(etype, attr, field):
-    """sets the `attr` field of `etype`.
-
-    :param etype: the entity type as a string
-    :param attr: the name of the attribute or relation
-
-    `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
-
-    """
-    _tag_rel(uicfg.autoform_field, etype, attr, '*', field)
-
-
-def edit_inline(etype, attr, desttype='*', formtype=('main', 'inlined')):
-    """edit `attr` with and inlined form.
-
-    :param etype: the entity type as a string
-    :param attr: the name of the attribute or relation
-    :param desttype: the destination type(s) concerned, default is everything
-    :param formtype: which form will be affected ('main', 'inlined', etc.), *main* and *inlined* by default.
-
-    `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
-
-    Examples:
-
-.. sourcecode:: python
+backward_compat_funcs = (('append_to_addmenu', uicfg.actionbox_appearsin_addmenu),
+                         ('remove_from_addmenu', uicfg.actionbox_appearsin_addmenu),
+                         ('set_fields_order', uicfg.autoform_field_kwargs),
+                         ('hide_field', uicfg.autoform_section),
+                         ('hide_fields', uicfg.autoform_section),
+                         ('set_field_kwargs', uicfg.autoform_field_kwargs),
+                         ('set_field', uicfg.autoform_field),
+                         ('edit_inline', uicfg.autoform_section),
+                         ('edit_as_attr', uicfg.autoform_section),
+                         ('set_muledit_editable', uicfg.autoform_section),
+                         )
 
-  from cubicweb.web import uihelper
-
-  uihelper.edit_inline('*', 'use_email')
-  """
-    _tag_rel(uicfg.autoform_section, etype, attr, desttype,
-             formtype=formtype, section='inlined')
-
-
-def edit_as_attr(etype, attr, desttype='*', formtype=('main', 'muledit')):
-    """make `attr` appear in the *attributes* section of `etype` form.
-
-    :param etype: the entity type as a string
-    :param attr: the name of the attribute or relation
-    :param desttype: the destination type(s) concerned, default is everything
-    :param formtype: which form will be affected ('main', 'inlined', etc.), *main* and *muledit* by default.
-
-    `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
-
-    Examples:
-
-.. sourcecode:: python
-
-  from cubicweb.web import uihelper
-
-  uihelper.edit_as_attr('CWUser', 'in_group')
-    """
-    _tag_rel(uicfg.autoform_section, etype, attr, desttype,
-             formtype=formtype, section='attributes')
-
-
-def set_muledit_editable(etype, attrs):
-    """make `attrs` appear in muledit form of `etype`.
-
-    :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_relation)
-
-    Examples:
-
-.. sourcecode:: python
-
-  from cubicweb.web import uihelper
-
-  uihelper.set_muledit_editable('CWUser', ('firstname', 'surname', 'in_group'))
-    """
-    for attr in attrs:
-        edit_as_attr(etype, attr, formtype='muledit')
+for funcname, tag in backward_compat_funcs:
+    msg = ('[3.16] uihelper.%(name)s is deprecated, please use '
+           'web.uicfg.%(classname)s.%(name)s' % dict(
+               name=funcname, classname=tag.__class__.__name__))
+    globals()[funcname] = deprecated(msg)(getattr(tag, funcname))
 
 
 class meta_formconfig(type):
@@ -253,17 +76,23 @@
     def __init__(cls, name, bases, classdict):
         if cls.etype is None:
             return
+        if cls.uicfg_afs is None:
+            uicfg_afs = uicfg.autoform_section
+        if cls.uicfg_aff is None:
+            uicfg_aff = uicfg.autoform_field
+        if cls.uicfg_affk is None:
+            uicfg_affk = uicfg.autoform_field_kwargs
         for attr_role in cls.hidden:
-            hide_field(cls.etype, attr_role, formtype=cls.formtype)
+            uicfg_afs.hide_field(cls.etype, attr_role, formtype=cls.formtype)
         for attr_role in cls.rels_as_attrs:
-            edit_as_attr(cls.etype, attr_role, formtype=cls.formtype)
+            uicfg_afs.edit_as_attr(cls.etype, attr_role, formtype=cls.formtype)
         for attr_role in cls.inlined:
-            edit_inline(cls.etype, attr_role, formtype=cls.formtype)
+            uicfg_afs.edit_inline(cls.etype, attr_role, formtype=cls.formtype)
         for rtype, widget in cls.widgets.items():
-            set_field_kwargs(cls.etype, rtype, widget=widget)
+            uicfg_affk.set_field_kwargs(cls.etype, rtype, widget=widget)
         for rtype, field in cls.fields.items():
-            set_field(cls.etype, rtype, field)
-        set_fields_order(cls.etype, cls.fields_order)
+            uicfg_aff.set_field(cls.etype, rtype, field)
+        uicfg_affk.set_fields_order(cls.etype, cls.fields_order)
         super(meta_formconfig, cls).__init__(name, bases, classdict)
 
 
@@ -303,6 +132,18 @@
     :attr:`fields`
       a dictionary mapping attribute names to field instances.
 
+    :attr:`uicfg_afs`
+      an instance of ``cubicweb.web.uicfg.AutoformSectionRelationTags``
+      Default is None, meaning ``cubicweb.web.uicfg.autoform_section`` is used.
+
+    :attr:`uicfg_aff`
+      an instance of ``cubicweb.web.uicfg.AutoformFieldTags``
+      Default is None, meaning ``cubicweb.web.uicfg.autoform_field`` is used.
+
+    :attr:`uicfg_affk`
+      an instance of ``cubicweb.web.uicfg.AutoformFieldKwargsTags``
+      Default is None, meaning ``cubicweb.web.uicfg.autoform_field_kwargs`` is used.
+
     Examples:
 
 .. sourcecode:: python
@@ -333,3 +174,6 @@
     fields_order = ()
     widgets = {}
     fields = {}
+    uicfg_afs = None
+    uicfg_aff = None
+    uicfg_affk = None
--- a/web/views/uicfg.py	Mon Jan 21 16:34:09 2013 +0100
+++ b/web/views/uicfg.py	Thu Jan 10 18:11:33 2013 +0100
@@ -354,18 +354,184 @@
                     continue
             yield (rschema, targetschemas, role)
 
+    def hide_field(self, etype, attr, desttype='*', formtype='main'):
+        """hide `attr` in `etype` forms.
+
+        :param etype: the entity type as a string
+        :param attr: the name of the attribute or relation to hide
+        :param formtype: which form will be affected ('main', 'inlined', etc.),
+         *main* by default.
+
+        `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_rel)
+
+        Examples:
+
+        .. sourcecode:: python
+
+          from cubicweb.web.views.uicfg import autoform_section as afs
+          afs.hide_field('CWUser', 'login')
+          afs.hide_field('*', 'name')
+          afs.hide_field('CWUser', 'use_email', formtype='inlined')
+
+        """
+        self._tag_etype_attr(etype, attr, desttype,
+                             formtype=formtype, section='hidden')
+
+    def hide_fields(self, etype, attrs, formtype='main'):
+        """simple for-loop wrapper around :func:`hide_field`.
+
+        :param etype: the entity type as a string
+        :param attrs: the ordered list of attribute names (or relations)
+        :param formtype: which form will be affected ('main', 'inlined', etc.),
+         *main* by default.
+
+        `attrs` can be strings or 2-tuples (relname, role_of_etype_in_the_rel)
+
+        Examples:
+
+        .. sourcecode:: python
+
+          from cubicweb.web.views.uicfg import autoform_section as afs
+          afs.hide_fields('CWUser', ('login', ('use_email', 'subject')),
+                          formtype='inlined')
+        """
+        for attr in attrs:
+            self.hide_field(etype, attr, formtype=formtype)
+
+    def edit_inline(self, etype, attr, desttype='*', formtype=('main', 'inlined')):
+        """edit `attr` with and inlined form.
+
+        :param etype: the entity type as a string
+        :param attr: the name of the attribute or relation
+        :param desttype: the destination type(s) concerned, default is everything
+        :param formtype: which form will be affected ('main', 'inlined', etc.),
+          *main* and *inlined* by default.
+
+        `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
+
+        Examples:
+
+        .. sourcecode:: python
+
+          from cubicweb.web.views.uicfg import autoform_section as afs
+
+          afs.edit_inline('*', 'use_email')
+      """
+        self._tag_etype_attr(etype, attr, desttype, formtype=formtype,
+                             section='inlined')
+
+    def edit_as_attr(self, etype, attr, desttype='*', formtype=('main', 'muledit')):
+        """make `attr` appear in the *attributes* section of `etype` form.
+
+        :param etype: the entity type as a string
+        :param attr: the name of the attribute or relation
+        :param desttype: the destination type(s) concerned, default is everything
+        :param formtype: which form will be affected ('main', 'inlined', etc.),
+          *main* and *muledit* by default.
+
+        `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
+
+        Examples:
+
+        .. sourcecode:: python
+
+          from cubicweb.web.views.uicfg import autoform_section as afs
+
+          afs.edit_as_attr('CWUser', 'in_group')
+        """
+        self._tag_etype_attr(etype, attr, desttype,
+                             formtype=formtype, section='attributes')
+
+    def set_muledit_editable(self, etype, attrs):
+        """make `attrs` appear in muledit form of `etype`.
+
+        :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_relation)
+
+        Examples:
+
+        .. sourcecode:: python
+
+          from cubicweb.web.views.uicfg import autoform_section as afs
+
+          afs.set_muledit_editable('CWUser', ('firstname', 'surname', 'in_group'))
+        """
+        for attr in attrs:
+            self.edit_as_attr(self, etype, attr, formtype='muledit')
+
 autoform_section = AutoformSectionRelationTags()
 
+
 # relations'field class
+
 class AutoformFieldTags(RelationTags):
     __regid__ = 'autoform_field'
 
+    def set_field(self, etype, attr, field):
+        """sets the `attr` field of `etype`.
+
+        :param etype: the entity type as a string
+        :param attr: the name of the attribute or relation
+
+        `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
+
+        """
+        self._tag_etype_attr(etype, attr, '*', field)
+
 autoform_field = AutoformFieldTags()
 
+
 # relations'field explicit kwargs (given to field's __init__)
+
 class AutoformFieldKwargsTags(RelationTagsDict):
     __regid__ = 'autoform_field_kwargs'
 
+    def set_fields_order(self, etype, attrs):
+        """specify the field order in `etype` main edition form.
+
+        :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 autoform_field_kwargs as affk
+          affk.set_fields_order('CWUser', ('firstname', 'surname', 'login'))
+          affk.set_fields_order('CWUser', ('firstname', ('in_group', 'subject'),
+                                'surname', 'login'))
+
+        """
+        for index, attr in enumerate(attrs):
+            self._tag_etype_attr(etype, attr, '*', {'order': index})
+
+    def set_field_kwargs(self, etype, attr, **kwargs):
+        """tag `attr` field of `etype` with additional named paremeters.
+
+        :param etype: the entity type as a string
+        :param attr: the name of the attribute or relation
+
+        `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
+
+        Examples:
+
+        .. sourcecode:: python
+
+          from cubicweb.web.views.uicfg import autoform_field_kwargs as affk
+          affk.set_field_kwargs('Person', 'works_for', widget=fwdgs.AutoCompletionWidget())
+          affk.set_field_kwargs('CWUser', 'login', label=_('login or email address'),
+                                widget=fwdgs.TextInput(attrs={'size': 30}))
+        """
+        self._tag_etype_attr(etype, attr, '*', kwargs)
+
+
 autoform_field_kwargs = AutoformFieldKwargsTags()
 
 
@@ -442,6 +608,7 @@
 # boxes.EditBox configuration #################################################
 
 # 'link' / 'create' relation tags, used to control the "add entity" submenu
+
 class ActionBoxUicfg(RelationTagsBool):
     __regid__ = 'actionbox_appearsin_addmenu'
 
@@ -454,6 +621,39 @@
             if not rdef.role_cardinality(role) in '?1' and rdef.composite == role:
                 self.tag_relation((sschema, rschema, oschema, role), True)
 
+    def _tag_etype_attr(self, etype, attr, desttype='*', *args, **kwargs):
+        if isinstance(attr, basestring):
+            attr, role = attr, 'subject'
+        else:
+            attr, role = attr
+        if role == 'subject':
+            self.tag_subject_of((etype, attr, desttype), *args, **kwargs)
+        else:
+            self.tag_object_of((desttype, attr, etype), *args, **kwargs)
+
+    def append_to_addmenu(self, etype, attr, createdtype='*'):
+        """adds `attr` in the actions box *addrelated* submenu of `etype`.
+
+        :param etype: the entity type as a string
+        :param attr: the name of the attribute or relation to hide
+        :param createdtype: the target type of the relation (optional, defaults to '*' (all possible types))
+
+        `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
+
+        """
+        self._tag_etype_attr(etype, attr, createdtype, True)
+
+    def remove_from_addmenu(self, etype, attr, createdtype='*'):
+        """removes `attr` from the actions box *addrelated* submenu of `etype`.
+
+        :param etype: the entity type as a string
+        :param attr: the name of the attribute or relation to hide
+        :param createdtype: the target type of the relation (optional, defaults to '*' (all possible types))
+
+        `attr` can be a string or 2-tuple (relname, role_of_etype_in_the_relation)
+        """
+        self._tag_etype_attr(etype, attr, createdtype, False)
+
 actionbox_appearsin_addmenu = ActionBoxUicfg()