[hooks] Add condition on rql syncing mime type and format id from file category
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 03 Nov 2017 11:44:31 +0100
changeset 2862 f434324b01f0
parent 2861 583f2dbd34dd
child 2863 33b32481052f
[hooks] Add condition on rql syncing mime type and format id from file category Add restriction in each EXISTS clause to ensure the specified eids are actually a generic category (top level concept) or a file extension (1st level concept). This will avoid errors catched by test added in next commit with a different default vocabulary for mime types / format ids. A monkey-patch has to be backported to allow using computed relation in write query, which is not implemented in cw < 3.25.4 Related to #39322647
cubicweb_seda/hooks.py
cubicweb_seda/site_cubicweb.py
--- a/cubicweb_seda/hooks.py	Fri Nov 03 16:03:57 2017 +0100
+++ b/cubicweb_seda/hooks.py	Fri Nov 03 11:44:31 2017 +0100
@@ -409,21 +409,32 @@
         'SET X seda_mime_type_to MT WHERE X eid %(x)s, '
         'MT in_scheme CS, CACLV seda_mime_type_code_list_version_from AT, '
         'CACLV seda_mime_type_code_list_version_to CS, AT eid %(c)s, '
-        'MTL label_of MT, MTL label MTLL, '
-        'FCMTL label_of FCMT , FCMTL label MTLL, '
-        'EXISTS(FCMT broader_concept EXT1, EXT1 eid IN ({eids})) '
-        'OR EXISTS(FCMT broader_concept EXT2, EXT2 broader_concept CAT, '
-        '          CAT eid IN ({eids}))'
+        'MT preferred_label MTL, MTL label MTLL, '
+        'FCMT preferred_label FCMTL , FCMTL label MTLL, '
+        # case where a file extension is given...
+        'EXISTS(FCMT broader_concept EXT1, EXT1 eid IN ({eids}), '
+        #   ... ensured by checking its parent is a root category
+        '       EXISTS(EXT1 broader_concept CAT1, NOT EXISTS(CAT1 broader_concept Z1))) '
+        # case where a generic category is given
+        'OR EXISTS(FCMT broader_concept EXT2, EXT2 broader_concept CAT2, '
+        '          CAT2 eid IN ({eids}), '
+        #   ... ensured by checking it's a root category
+        '          NOT EXISTS(CAT2 broader_concept Z2))'
     )
     set_format_id_rql = (
         'SET X seda_format_id_to FI WHERE X eid %(x)s, '
         'FI in_scheme CS, CACLV seda_file_format_code_list_version_from AT, '
         'CACLV seda_file_format_code_list_version_to CS, AT eid %(c)s, '
-        'FIL label_of FI, FIL label FILL, '
-        'FCFIL label_of FCFI , FCFIL label FILL, '
-        'EXISTS(FCFI broader_concept MT, MT broader_concept EXT1, EXT1 eid IN ({eids})) '
-        'OR EXISTS(FCFI broader_concept MT2, MT2 broader_concept EXT2, EXT2 broader_concept CAT, '
-        '          CAT eid IN ({eids}))'
+        'FI preferred_label FIL, FIL label FILL, '
+        'FCFI preferred_label FCFIL , FCFIL label FILL, '
+        # case where a file extension is given...
+        'EXISTS(FCFI broader_concept MT, MT broader_concept EXT1, EXT1 eid IN ({eids}), '
+        #   ... ensured by checking its parent is a root category
+        '       EXISTS(EXT1 broader_concept CAT1, NOT EXISTS(CAT1 broader_concept Z1))) '
+        'OR EXISTS(FCFI broader_concept MT2, MT2 broader_concept EXT2, EXT2 broader_concept CAT2, '
+        '          CAT2 eid IN ({eids}),'
+        #   ... ensured by checking it's a root category
+        '          NOT EXISTS(CAT2 broader_concept Z2))'
     )
 
     def precommit_event(self):
--- a/cubicweb_seda/site_cubicweb.py	Fri Nov 03 16:03:57 2017 +0100
+++ b/cubicweb_seda/site_cubicweb.py	Fri Nov 03 11:44:31 2017 +0100
@@ -14,10 +14,28 @@
 # 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 logilab.common.decorators import monkeypatch
+
 from cubicweb.entity import Entity
-
+from cubicweb.rqlrewrite import RQLRelationRewriter
+from cubicweb.server import ssplanner
 
 Entity.cw_skip_copy_for.append(('container', 'subject'))
 Entity.cw_skip_copy_for.append(('container', 'object'))
 Entity.cw_skip_copy_for.append(('clone_of', 'subject'))
 Entity.cw_skip_copy_for.append(('clone_of', 'object'))
+
+
+# monkey-patch to allow using computed relation in WHERE clause of write queries
+# (https://www.cubicweb.org/ticket/17113286)
+
+@monkeypatch(ssplanner.SSPlanner)
+def _select_plan(self, plan, select, solutions):
+    union = ssplanner.Union()
+    union.append(select)
+    select.clean_solutions(solutions)
+    ssplanner.add_types_restriction(self.schema, select)
+    rewriter = RQLRelationRewriter(plan.cnx)
+    rewriter.rewrite(union, plan.args)
+    self.rqlhelper.annotate(union)
+    return self.build_select_plan(plan, union)