[ui] Factorise a copy_rtag function
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 22 Jul 2016 11:24:07 +0200
changeset 1484 56a15e820988
parent 1474 aebba811c46a
child 1485 9d9cdfe9d85b
[ui] Factorise a copy_rtag function So underlying things such as deepcopy are hidden a bit. Along the way, rename dor_afs to do_ref_afs which is easier to read.
views/__init__.py
views/archiveunit.py
views/content.py
--- a/views/__init__.py	Thu Jul 21 12:04:00 2016 +0200
+++ b/views/__init__.py	Fri Jul 22 11:24:07 2016 +0200
@@ -14,6 +14,8 @@
 # You should have received a copy of the GNU Lesser General Public License
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
 
+from copy import deepcopy
+
 from yams import BASE_TYPES
 
 from cubicweb import tags, neg_role
@@ -50,6 +52,17 @@
     afs.tag_attribute((etype, 'user_annotation'), 'main', 'attributes')
 
 
+def copy_rtag(rtag, module, select):
+    """Return a copy of the given relation tag, associated to given module and selector.
+
+    >>> class_afs = copy_rtag(uicfg.autoform_section, __module__, is_instance('Class'))
+    """
+    copied = deepcopy(rtag)
+    copied.__module__ = module
+    copied.__select__ = select
+    return copied
+
+
 def rtags_from_xsd_element(etype, element_name):
     """Return primary view section and display control rtags, generated from information in the XSD
     for the given element name.
--- a/views/archiveunit.py	Thu Jul 21 12:04:00 2016 +0200
+++ b/views/archiveunit.py	Fri Jul 22 11:24:07 2016 +0200
@@ -15,8 +15,6 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 """cubicweb-seda views for ArchiveUnit"""
 
-from copy import deepcopy
-
 from logilab.common.registry import objectify_predicate, yes
 
 from cubicweb import tags
@@ -27,7 +25,8 @@
 from cubes.compound import views as compound
 
 from cubes.seda.entities.itree import parent_archive_unit
-from cubes.seda.views import add_subobject_link, dropdown_button, rtags_from_rtype_role_targets
+from cubes.seda.views import (add_subobject_link, dropdown_button, rtags_from_rtype_role_targets,
+                              copy_rtag)
 from cubes.seda.views import viewlib
 from cubes.seda.views import uicfg as sedauicfg  # noqa - ensure those rules are defined first
 
@@ -126,9 +125,8 @@
         _('seda_au_data_objects_refs_tab'),
     ]
 
-au_pvs = deepcopy(pvs)
-au_pvs.__module__ = __name__
-au_pvs.__select__ = is_instance('SEDAArchiveUnit') & is_archive_unit_ref()
+au_pvs = copy_rtag(pvs, __name__,
+                   is_instance('SEDAArchiveUnit') & is_archive_unit_ref())
 au_pvs.tag_subject_of(
     ('SEDAArchiveUnit', 'seda_alt_archive_unit_archive_unit_ref_id', '*'),
     'attributes')
@@ -280,10 +278,9 @@
                   'main', 'hidden')
 
 
-au_ref_afs = deepcopy(afs)
-au_ref_afs.__module__ = __name__
-au_ref_afs.__select__ = (is_instance('SEDAAltArchiveUnitArchiveUnitRefId')
-                         & is_archive_unit_ref())
+au_ref_afs = copy_rtag(afs, __name__,
+                       is_instance('SEDAAltArchiveUnitArchiveUnitRefId')
+                       & is_archive_unit_ref())
 au_ref_afs.tag_object_of(
     ('*', 'seda_archive_unit_ref_id_from', 'SEDAAltArchiveUnitArchiveUnitRefId'),
     'inlined', 'inlined')
@@ -292,10 +289,9 @@
      'seda_seq_alt_archive_unit_archive_unit_ref_id_management', '*'),
     'inlined', 'hidden')
 
-au_content_afs = deepcopy(afs)
-au_content_afs.__module__ = __name__
-au_content_afs.__select__ = (is_instance('SEDAAltArchiveUnitArchiveUnitRefId')
-                             & ~is_archive_unit_ref())
+au_content_afs = copy_rtag(afs, __name__,
+                           is_instance('SEDAAltArchiveUnitArchiveUnitRefId')
+                           & ~is_archive_unit_ref())
 au_content_afs.tag_object_of(
     ('*', 'seda_archive_unit_ref_id_from', 'SEDAAltArchiveUnitArchiveUnitRefId'),
     'inlined', 'hidden')
--- a/views/content.py	Thu Jul 21 12:04:00 2016 +0200
+++ b/views/content.py	Fri Jul 22 11:24:07 2016 +0200
@@ -15,8 +15,6 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 """cubicweb-seda views for content of archive units."""
 
-from copy import deepcopy
-
 from logilab.common.registry import objectify_predicate
 from logilab.mtconverter import xml_escape
 
@@ -26,7 +24,7 @@
 from cubes.relationwidget.views import RelationFacetWidget
 
 from cubes.seda.xsd import un_camel_case
-from cubes.seda.views import rtags_from_rtype_role_targets, rtags_from_xsd_element
+from cubes.seda.views import rtags_from_rtype_role_targets, rtags_from_xsd_element, copy_rtag
 from cubes.seda.views import viewlib, widgets
 from cubes.seda.views import uicfg as sedauicfg  # noqa - ensure those rules are defined first
 
@@ -373,10 +371,9 @@
         return (u'<b>{0}</b>'.format(self._cw._(' ALT_I18N '))).join(alternatives)
 
 
-dor_afs = deepcopy(afs)
-dor_afs.__module__ = __name__
-dor_afs.__select__ = is_instance('SEDADataObjectReference') & is_typed_reference()
-dor_afs.tag_attribute(('SEDADataObjectReference', 'user_cardinality'), 'main', 'hidden')
+do_ref_afs = copy_rtag(afs, __name__,
+                       is_instance('SEDADataObjectReference') & is_typed_reference())
+do_ref_afs.tag_attribute(('SEDADataObjectReference', 'user_cardinality'), 'main', 'hidden')
 
 for etype in TYPED_REFERENCE_ENTITY_TYPES:
     affk.set_fields_order(etype, [('seda_data_object_reference', 'object'),