misc: set Content-Disposition header for exports (#47060)

This commit is contained in:
Benjamin Dauvergne 2020-09-28 10:15:50 +02:00
parent 555a70c276
commit 91adb6e0b2
2 changed files with 12 additions and 1 deletions

View File

@ -23,6 +23,7 @@ from django.conf import settings
from django.contrib import messages
from django.utils.encoding import force_bytes, force_text
from django.utils.text import slugify
from django.utils.timezone import now
from django.utils.translation import ungettext, ugettext as _
from django.views.generic.edit import CreateView, DeleteView, UpdateView, FormView
from django.views.generic.list import MultipleObjectMixin
@ -369,6 +370,9 @@ class ExportVisualizationView(views.AuthorizationMixin, DetailView):
def get(self, request, *args, **kwargs):
response = HttpResponse(content_type='application/json')
response['Content-Disposition'] = (
'attachment; filename="%s"' % 'export_stats_%s.json' % now().strftime('%Y%m%d')
)
json.dump({'visualizations': [self.get_object().export_json()]}, response, indent=2)
return response
@ -414,6 +418,9 @@ class VisualizationsExportView(views.AuthorizationMixin, View):
def get(self, request, *args, **kwargs):
response = HttpResponse(content_type='application/json')
response['Content-Disposition'] = (
'attachment; filename="%s"' % 'export_stats_%s.json' % now().strftime('%Y%m%d')
)
json.dump(export_site(), response, indent=2)
return response

View File

@ -167,12 +167,14 @@ def test_visualization_cube_view_errors(schema1, app, admin):
app.get('/visualization/warehouse/schema1/fact1/', status=404)
def test_import_visualization(schema1, app, admin, visualization, settings):
def test_import_visualization(schema1, app, admin, visualization, settings, freezer):
freezer.move_to('2020-01-01T00:00:00Z')
settings.LANGUAGE_CODE = 'en-us'
login(app, admin)
resp = app.get('/visualization/%s/' % visualization.id)
resp = resp.click('Export as JSON')
assert resp.headers['content-type'] == 'application/json'
assert resp.headers['content-disposition'] == 'attachment; filename="export_stats_20200101.json"'
visualization_export = resp.text
# invalid json
@ -226,6 +228,8 @@ def test_import_visualization(schema1, app, admin, visualization, settings):
# global export/import
resp = app.get('/').click('Export')
assert resp.headers['content-type'] == 'application/json'
assert resp.headers['content-disposition'] == 'attachment; filename="export_stats_20200101.json"'
visualizations_export = resp.text
Visualization.objects.all().delete()