[saem-devel] [PATCH] [entities] add SEDA version to exported filename

Philippe Pepiot philippe.pepiot at logilab.fr
Wed Feb 15 11:47:05 CET 2017


# HG changeset patch
# User Philippe Pepiot <philippe.pepiot at logilab.fr>
# Date 1487154564 -3600
#      Wed Feb 15 11:29:24 2017 +0100
# Node ID 5ab62efbc66a1caff66fe2c4eda71f419db59693
# Parent  7b6041561f7bd5e3389dca5eed8b4d9fa0b51241
# Available At https://hg.logilab.org/review/cubes/seda
#              hg pull https://hg.logilab.org/review/cubes/seda -r 5ab62efbc66a
# Tested at https://jenkins.logilab.org/job/cubicweb-seda/40/
[entities] add SEDA version to exported filename

Closes extranet #16687524

diff --git a/entities/html_generation.py b/entities/html_generation.py
--- a/entities/html_generation.py
+++ b/entities/html_generation.py
@@ -59,7 +59,6 @@ def attribute_cardinality_as_string(occ,
 class SEDA2HTMLExport(SEDA2ExportAdapter):
     """Adapter to build a HTML representation of a SEDA profile"""
     __regid__ = 'SEDA-2.0.html'
-    extension = 'html'
     namespaces = {}
     css = '''
     h1{
diff --git a/entities/profile_generation.py b/entities/profile_generation.py
--- a/entities/profile_generation.py
+++ b/entities/profile_generation.py
@@ -236,14 +236,13 @@ class SEDA2ExportAdapter(EntityAdapter):
     encoding = 'utf-8'
     content_type = 'application/xml'
     # to be defined in concret implementations
-    extension = None
     namespaces = {}
     root_attributes = {}
 
     @property
     def file_name(self):
         """Return a file name for the dump"""
-        return '%s.%s' % (self.entity.dc_title(), self.extension)
+        return self.entity.dc_title()
 
     def dump(self):
         """Return an schema string for the adapted SEDA profile."""
@@ -375,7 +374,6 @@ class SEDA2ExportAdapter(EntityAdapter):
 class SEDA2XSDExport(SEDA2ExportAdapter):
     """Adapter to build an XSD representation of a SEDA profile, using SEDA 2.0 specification."""
     __regid__ = 'SEDA-2.0.xsd'
-    extension = 'xsd'
     namespaces = {
         None: 'fr:gouv:culture:archivesdefrance:seda:v2.0',
         'seda': 'fr:gouv:culture:archivesdefrance:seda:v2.0',
@@ -578,7 +576,6 @@ class SEDA2RelaxNGExport(RNGMixin, SEDA2
     """Adapter to build a Relax NG representation of a SEDA profile, using SEDA 2.0 specification.
     """
     __regid__ = 'SEDA-2.0.rng'
-    extension = 'rng'
 
     namespaces = SEDA2XSDExport.namespaces.copy()
     namespaces['rng'] = 'http://relaxng.org/ns/structure/1.0'
@@ -821,7 +818,6 @@ class SEDA1XSDExport(SEDA2XSDExport):
     """
     __regid__ = 'SEDA-1.0.xsd'
     __select__ = SEDA2XSDExport.__select__ & simplified_profile()
-    extension = 'xsd'
 
     namespaces = {
         None: 'fr:gouv:culture:archivesdefrance:seda:v1.0',
@@ -1421,7 +1417,6 @@ class SEDA1RNGExport(OldSEDARNGExportMix
     specification.
     """
     __regid__ = 'SEDA-1.0.rng'
-    extension = 'rng'
 
     namespaces = SEDA1XSDExport.namespaces.copy()
     namespaces['rng'] = 'http://relaxng.org/ns/structure/1.0'
@@ -1436,7 +1431,6 @@ class SEDA02RNGExport(OldSEDARNGExportMi
     specification.
     """
     __regid__ = 'SEDA-0.2.rng'
-    extension = 'rng'
 
     namespaces = SEDA1XSDExport.namespaces.copy()
     namespaces['rng'] = 'http://relaxng.org/ns/structure/1.0'
diff --git a/test/test_views.py b/test/test_views.py
--- a/test/test_views.py
+++ b/test/test_views.py
@@ -265,6 +265,27 @@ class ArchiveTransferExportTC(CubicWebTC
             exporter = self.vreg['views'].select('seda.export', req, entity=transfer)
             self.assertEqual(exporter.__class__, export.SEDAExportView)
 
+    def test_export_filename(self):
+        with self.admin_access.web_request() as req:
+            transfer = req.create_entity('SEDAArchiveTransfer',
+                                         title=u'diagnosis testing',
+                                         simplified_profile=True)
+            req.cnx.commit()
+            for version, fmt, expected_filename in (
+                ('2.0', 'rng', 'diagnosis testing-2.0.rng'),
+                ('2.0', 'xsd', 'diagnosis testing-2.0.xsd'),
+                ('2.0', 'html', 'diagnosis testing-2.0.html'),
+                ('1.0', 'rng', 'diagnosis testing-1.0.rng'),
+                ('1.0', 'xsd', 'diagnosis testing-1.0.xsd'),
+            ):
+                req.form['version'], req.form['format'] = version, fmt
+                view = self.vreg['views'].select('seda.export', req,
+                                                 rset=transfer.as_rset())
+                view.set_request_content_type()
+                self.assertEqual(
+                    view._cw.headers_out.getRawHeaders('content-disposition'),
+                    ['attachment;filename="{0}"'.format(expected_filename)])
+
 
 class SimplifiedFormsTC(CubicWebTC):
     """Functional test case about forms in the web interface."""
diff --git a/views/export.py b/views/export.py
--- a/views/export.py
+++ b/views/export.py
@@ -131,7 +131,10 @@ class SEDAExportView(SEDAExportViewMixin
     def set_request_content_type(self):
         entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
         adapter = entity.cw_adapt_to(self.seda_adapter_id)
-        self._cw.set_content_type(adapter.content_type, filename=adapter.file_name,
+        filename = '%s-%s.%s' % (adapter.file_name,
+                                 self._cw.form.get('version', '2.0'),
+                                 self._cw.form.get('format', 'rng').lower())
+        self._cw.set_content_type(adapter.content_type, filename=filename,
                                   encoding=adapter.encoding, disposition='attachment')
 
     def call(self, **kwargs):



More information about the saem-devel mailing list