[ui] Most of the relation widget configuration code moved as a generic implementation in the skos cube
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 07 Jul 2016 19:15:06 +0200
changeset 1368 ef09c56b211b
parent 1341 a68075226224
child 1369 fe4825937b4b
[ui] Most of the relation widget configuration code moved as a generic implementation in the skos cube Closes #14157219
dev-requirements.txt
views/content.py
views/dataobject.py
--- a/dev-requirements.txt	Wed Jul 06 20:12:37 2016 +0200
+++ b/dev-requirements.txt	Thu Jul 07 19:15:06 2016 +0200
@@ -1,3 +1,4 @@
 hg+https://hg.logilab.org/review/cubes/compound@4c1b3c61e1a7#egg=cubicweb-compound
 hg+https://hg.logilab.org/review/cubes/relationwidget@e57360e87158#egg=cubicweb-relationwidget
+hg+https://hg.logilab.org/review/cubes/skos@4c0a678154fc#egg=cubicweb-skos
 pytest
--- a/views/content.py	Wed Jul 06 20:12:37 2016 +0200
+++ b/views/content.py	Thu Jul 07 19:15:06 2016 +0200
@@ -73,7 +73,8 @@
                                                'seda_keyword_reference_to_scheme',
                                                'seda_keyword_reference_to'])
 
-affk.tag_subject_of(('SEDALanguage', 'seda_language_to', '*'), {'widget': RelationFacetWidget})
+affk.tag_subject_of(('SEDALanguage', 'seda_language_to', '*'),
+                    {'widget': RelationFacetWidget})
 affk.tag_subject_of(('SEDADescriptionLanguage', 'seda_description_language_to', '*'),
                     {'widget': RelationFacetWidget})
 
--- a/views/dataobject.py	Wed Jul 06 20:12:37 2016 +0200
+++ b/views/dataobject.py	Thu Jul 07 19:15:06 2016 +0200
@@ -24,6 +24,7 @@
 from cubicweb.web.views import uicfg, tabs
 
 from cubes.relationwidget import views as rwdg
+from cubes.skos.views import widgets as skos
 
 from cubes.seda.xsd2yams import RDEF_CONSTRAINTS
 from cubes.seda.views import rtags_from_xsd_element, add_subobject_link, parent_and_container
@@ -40,6 +41,7 @@
 
 
 class ContainedRelationFacetWidget(rwdg.RelationFacetWidget):
+
     def _render_triggers(self, w, domid, form, field, rtype):
         parent, container = parent_and_container(form.edited_entity)
         req = form._cw
@@ -78,62 +80,16 @@
         return super(ContainedRelationFacetWidget, self).trigger_search_url(entity, url_params)
 
 
-class ContainedSearchForRelatedEntitiesView(rwdg.SearchForRelatedEntitiesView):
-    __select__ = rwdg.SearchForRelatedEntitiesView.__select__ & match_form_params('container')
-
-    has_creation_form = False
+class ContainedSearchForRelatedEntitiesView(skos.SearchForRelatedConceptsView):
+    __select__ = skos.SearchForRelatedConceptsView.__select__ & match_form_params('container')
 
-    def linkable_rset(self):
-        """Return rset of entities to be displayed as possible values for the
-        edited relation. You may want to override this.
-        """
+    def constrained_rql(self):
         container = int(self._cw.form['container'])
-        entity = self.compute_entity()
-        rtype, tetype, role = self.rdef
-        rdef = entity.e_schema.rdef(rtype, role, tetype)
-        assert len(rdef.constraints) == 1 and 'S container AT' in rdef.constraints[0].expression
-        baserql = rdef.constraints[0].expression
+        constraints = self.schema_rdef.constraints
+        assert len(constraints) == 1 and 'S container AT' in constraints[0].expression, constraints
+        baserql = constraints[0].expression
         baserql = baserql.replace('S container AT', 'AT eid %(at)s')
-        # detect language first
-        languages = set(lang for lang, in self._cw.execute(
-            'DISTINCT Any OLC WHERE ' + baserql + ', O preferred_label OL, '
-            'OL language_code OLC', {'at': container}))
-        for lang in (self._cw.lang, 'en', 'sed-2'):
-            if lang in languages:
-                break
-        else:
-            if not languages:
-                return self._cw.empty_rset()
-            lang = languages.pop()
-        # now build the query to prefetch/sort on the preferred label in the proper language
-        return self._cw.execute(
-            'DISTINCT Any O,OL,OLL ORDERBY OLL WHERE ' + baserql + ', O preferred_label OL, '
-            'OL label OLL, OL language_code %(lang)s',
-            {'at': container, 'lang': lang})
-
-    @staticmethod
-    def pref_label_label(concept):
-        return concept.cw_rset[concept.cw_row][2]
-
-    @staticmethod
-    def pref_label_column(w, concept):
-        w(tags.a(ContainedSearchForRelatedEntitiesView.pref_label_label(concept),
-                 href=concept.absolute_url()))
-
-
-class SelectConceptEntitiesTableView(rwdg.SelectEntitiesTableView):
-    """Table view of the selectable entities in the relation widget
-
-    Selection of columns (and respective renderer) can be overridden by
-    updating `columns` and `column_renderers` class attributes.
-    """
-    __select__ = rwdg.SelectEntitiesTableView.__select__ & is_instance('Concept')
-
-    column_renderers = rwdg.SelectEntitiesTableView.column_renderers.copy()
-    # speed-up things by considering rset shape from ContainedSearchForRelatedEntitiesView
-    column_renderers['entity'] = rwdg.SelectMainEntityColRenderer(
-        sortfunc=ContainedSearchForRelatedEntitiesView.pref_label_label,
-        renderfunc=ContainedSearchForRelatedEntitiesView.pref_label_column)
+        return baserql, {'at': container}
 
 
 # hide some relation from autoform because of limitation of _container_eid