[PATCH 6 of 9 seda] [hooks] Add hooks to create automatically SEDAMimeType and SEDAFormatId entities

Sylvain Thenault sylvain.thenault at logilab.fr
Thu Oct 12 16:59:51 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1507793723 -7200
#      Thu Oct 12 09:35:23 2017 +0200
# Node ID f5069136d86d6775215362fbcf2ab7665dd233c3
# Parent  e562b3d62a0d0a6e19764327d82b48e508ae494c
# Available At https://hg.logilab.org/review/cubes/seda
#              hg pull https://hg.logilab.org/review/cubes/seda -r f5069136d86d
[hooks] Add hooks to create automatically SEDAMimeType and SEDAFormatId entities

on a binary data object. This is done to prepare automatic synchronization of
these entities'values (concept) according to file_category. At some point we may
want to remove them in favor of direct relations to the binary data object,
since these are there to handle a cardinality which will be automatically
handled, provided the implemented behaviour fine with business people.

Related to #36331831

diff --git a/cubicweb_seda/hooks.py b/cubicweb_seda/hooks.py
--- a/cubicweb_seda/hooks.py
+++ b/cubicweb_seda/hooks.py
@@ -467,10 +467,27 @@ class CheckChildrenUnhandledCardinalityO
                         raise ValidationError(added_entity_eid,
                                               {role_name('user_cardinality', 'subject'): msg})
                     has_non_single_cardinality = True
 
 
+# XXX rather kill SEDAMimeType and SEDAFormatId if client is fine with "auto"
+# cardinality handling
+class InitBDO(hook.Hook):
+    """On binary data object creation, link it to a SEDAMimeType and SEDAFormatId
+    sub-entities.
+    """
+    __regid__ = 'seda.ux.autoformat'
+    __select__ = hook.Hook.__select__ & is_instance('SEDABinaryDataObject')
+    events = ('after_add_entity',)
+
+    def __call__(self):
+        self._cw.create_entity('SEDAMimeType', user_cardinality=u'0..1',
+                               seda_mime_type_from=self.entity)
+        self._cw.create_entity('SEDAFormatId', user_cardinality=u'0..1',
+                               seda_format_id_from=self.entity)
+
+
 def registration_callback(vreg):
     from cubicweb.server import ON_COMMIT_ADD_RELATIONS
     from cubicweb_seda import seda_profile_container_def, iter_all_rdefs
 
     vreg.register_all(globals().values(), __name__)
diff --git a/test/data/seda_02_export.rng b/test/data/seda_02_export.rng
--- a/test/data/seda_02_export.rng
+++ b/test/data/seda_02_export.rng
@@ -387,10 +387,15 @@
                   <rng:value type="string">fmt/123</rng:value>
                 </rng:attribute>
                 <rng:attribute name="encodingCode">
                   <rng:value type="string">6</rng:value>
                 </rng:attribute>
+                <rng:optional>
+                  <rng:attribute name="mimeCode">
+                    <rng:data type="string"/>
+                  </rng:attribute>
+                </rng:optional>
                 <rng:attribute name="filename">
                   <rng:value type="string">this_is_the_filename.pdf</rng:value>
                 </rng:attribute>
                 <rng:data type="string"/>
               </rng:element>
diff --git a/test/data/seda_02_export.xsd b/test/data/seda_02_export.xsd
--- a/test/data/seda_02_export.xsd
+++ b/test/data/seda_02_export.xsd
@@ -284,11 +284,11 @@
                       <xsd:complexType>
                         <xsd:simpleContent>
                           <xsd:extension base="qdt:ArchivesBinaryObjectType">
                             <xsd:attribute fixed="fmt/123" name="format" type="clmDAFFileTypeCode:FileTypeCodeType" use="required"/>
                             <xsd:attribute fixed="6" name="encodingCode" type="clm60133:CharacterSetEncodingCodeContentType" use="required"/>
-                            <xsd:attribute name="mimeCode" type="clmIANAMIMEMediaType:MIMEMediaTypeContentType" use="prohibited"/>
+                            <xsd:attribute name="mimeCode" type="clmIANAMIMEMediaType:MIMEMediaTypeContentType" use="optional"/>
                             <xsd:attribute fixed="this_is_the_filename.pdf" name="filename" type="xsd:string" use="required"/>
                             <xsd:attribute name="characterSetCode" type="clmIANACharacterSetCode:CharacterSetCodeContentType" use="prohibited"/>
                             <xsd:attribute name="uri" type="xsd:anyURI" use="prohibited"/>
                           </xsd:extension>
                         </xsd:simpleContent>
diff --git a/test/data/seda_1_export.rng b/test/data/seda_1_export.rng
--- a/test/data/seda_1_export.rng
+++ b/test/data/seda_1_export.rng
@@ -534,10 +534,15 @@
                   <rng:value type="string">fmt/123</rng:value>
                 </rng:attribute>
                 <rng:attribute name="encodingCode">
                   <rng:value type="string">6</rng:value>
                 </rng:attribute>
+                <rng:optional>
+                  <rng:attribute name="mimeCode">
+                    <rng:data type="string"/>
+                  </rng:attribute>
+                </rng:optional>
                 <rng:attribute name="filename">
                   <rng:value type="string">this_is_the_filename.pdf</rng:value>
                 </rng:attribute>
                 <rng:data type="string"/>
               </rng:element>
diff --git a/test/data/seda_1_export.xsd b/test/data/seda_1_export.xsd
--- a/test/data/seda_1_export.xsd
+++ b/test/data/seda_1_export.xsd
@@ -429,11 +429,11 @@
                       <xsd:complexType>
                         <xsd:simpleContent>
                           <xsd:extension base="qdt:ArchivesBinaryObjectType">
                             <xsd:attribute fixed="fmt/123" name="format" type="clmDAFFileTypeCode:FileTypeCodeType" use="required"/>
                             <xsd:attribute fixed="6" name="encodingCode" type="clm60133:CharacterSetEncodingCodeContentType" use="required"/>
-                            <xsd:attribute name="mimeCode" type="clmIANAMIMEMediaType:MIMEMediaTypeContentType" use="prohibited"/>
+                            <xsd:attribute name="mimeCode" type="clmIANAMIMEMediaType:MIMEMediaTypeContentType" use="optional"/>
                             <xsd:attribute fixed="this_is_the_filename.pdf" name="filename" type="xsd:string" use="required"/>
                             <xsd:attribute name="characterSetCode" type="clmIANACharacterSetCode:CharacterSetCodeContentType" use="prohibited"/>
                             <xsd:attribute name="uri" type="xsd:anyURI" use="prohibited"/>
                           </xsd:extension>
                         </xsd:simpleContent>
diff --git a/test/test_profile_generation.py b/test/test_profile_generation.py
--- a/test/test_profile_generation.py
+++ b/test/test_profile_generation.py
@@ -1,6 +1,6 @@
-# copyright 2016-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr -- mailto:contact at logilab.fr
 #
 # This program is free software: you can redistribute it and/or modify it under
 # the terms of the GNU Lesser General Public License as published by the Free
 # Software Foundation, either version 2.1 of the License, or (at your option)
@@ -816,14 +816,13 @@ class OldSEDAExportMixin(object):
             bdo = testutils.create_data_object(transfer, user_cardinality=u'0..n',
                                                filename=u'this_is_the_filename.pdf',
                                                seda_algorithm=concepts['md5'],
                                                reverse_seda_data_object_reference_id=ref)
 
-            create('SEDAFormatId',
-                   user_cardinality=u'1',
-                   seda_format_id_from=bdo,
-                   seda_format_id_to=concepts['fmt/123'])
+            bdo.format_id.cw_set(
+                user_cardinality=u'1',
+                seda_format_id_to=concepts['fmt/123'])
             create('SEDAEncoding',
                    user_cardinality=u'1',
                    seda_encoding_from=bdo,
                    seda_encoding_to=concepts['6'])
 
@@ -940,11 +939,11 @@ class OldSEDARNGExportTC(RelaxNGTestMixi
             bdo = testutils.create_data_object(transfer)
             create('SEDADataObjectReference',
                    seda_data_object_reference=subunit_alt_seq,
                    seda_data_object_reference_id=bdo)
             create('SEDAEncoding', seda_encoding_from=bdo)
-            create('SEDAMimeType', seda_mime_type_from=bdo)
+            bdo.mime_type.cw_set(user_cardinality=u'1')
             create('SEDADateCreatedByApplication', seda_date_created_by_application=bdo)
 
             cnx.commit()
 
             root = self.profile_etree(transfer, 'SEDA-0.2.rng')
diff --git a/test/test_schema.py b/test/test_schema.py
--- a/test/test_schema.py
+++ b/test/test_schema.py
@@ -1,6 +1,6 @@
-# copyright 2016-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr -- mailto:contact at logilab.fr
 #
 # This program is free software: you can redistribute it and/or modify it under
 # the terms of the GNU Lesser General Public License as published by the Free
 # Software Foundation, either version 2.1 of the License, or (at your option)
@@ -57,18 +57,14 @@ class SchemaConceptConstraintsTC(CubicWe
 
     def assertMimeTypeConcept(self, bdo):
         cnx = bdo._cw
 
         with self.assertValidationError(cnx) as cm:
-            cnx.create_entity('SEDAMimeType',
-                              seda_mime_type_from=bdo,
-                              seda_mime_type_to=self.enc_concept)
+            bdo.mime_type.cw_set(seda_mime_type_to=self.enc_concept)
         self.assertIn('seda_mime_type_to-subject', cm.exception.errors)
 
-        cnx.create_entity('SEDAMimeType',
-                          seda_mime_type_from=bdo,
-                          seda_mime_type_to=self.mt_concept)
+        bdo.mime_type.cw_set(seda_mime_type_to=self.mt_concept)
         cnx.commit()
 
     def test_archive_transfer_mime_type_constraint(self):
         with self.admin_access.cnx() as cnx:
             bdo = testutils.create_transfer_to_bdo(cnx)


More information about the saem-devel mailing list