[entities] add SEDA version to exported filename
authorPhilippe Pepiot <philippe.pepiot@logilab.fr>
Mon, 20 Feb 2017 11:36:04 +0100
changeset 2348 c77658529888
parent 2343 7b6041561f7b
child 2349 9e88b1141bb9
[entities] add SEDA version to exported filename Closes extranet #16687524
entities/html_generation.py
entities/profile_generation.py
test/test_views.py
views/export.py
--- a/entities/html_generation.py	Wed Feb 08 15:36:18 2017 +0100
+++ b/entities/html_generation.py	Mon Feb 20 11:36:04 2017 +0100
@@ -59,7 +59,6 @@
 class SEDA2HTMLExport(SEDA2ExportAdapter):
     """Adapter to build a HTML representation of a SEDA profile"""
     __regid__ = 'SEDA-2.0.html'
-    extension = 'html'
     namespaces = {}
     css = '''
     h1{
--- a/entities/profile_generation.py	Wed Feb 08 15:36:18 2017 +0100
+++ b/entities/profile_generation.py	Mon Feb 20 11:36:04 2017 +0100
@@ -236,15 +236,9 @@
     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)
-
     def dump(self):
         """Return an schema string for the adapted SEDA profile."""
         root = self.dump_etree()
@@ -375,7 +369,6 @@
 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 +571,6 @@
     """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 +813,6 @@
     """
     __regid__ = 'SEDA-1.0.xsd'
     __select__ = SEDA2XSDExport.__select__ & simplified_profile()
-    extension = 'xsd'
 
     namespaces = {
         None: 'fr:gouv:culture:archivesdefrance:seda:v1.0',
@@ -1421,7 +1412,6 @@
     specification.
     """
     __regid__ = 'SEDA-1.0.rng'
-    extension = 'rng'
 
     namespaces = SEDA1XSDExport.namespaces.copy()
     namespaces['rng'] = 'http://relaxng.org/ns/structure/1.0'
@@ -1436,7 +1426,6 @@
     specification.
     """
     __regid__ = 'SEDA-0.2.rng'
-    extension = 'rng'
 
     namespaces = SEDA1XSDExport.namespaces.copy()
     namespaces['rng'] = 'http://relaxng.org/ns/structure/1.0'
--- a/test/test_views.py	Wed Feb 08 15:36:18 2017 +0100
+++ b/test/test_views.py	Mon Feb 20 11:36:04 2017 +0100
@@ -265,6 +265,27 @@
             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."""
--- a/views/export.py	Wed Feb 08 15:36:18 2017 +0100
+++ b/views/export.py	Mon Feb 20 11:36:04 2017 +0100
@@ -131,7 +131,10 @@
     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' % (entity.dc_title(),
+                                 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):