Projet

Général

Profil

0001-misc-set-Content-Disposition-header-for-exports-4706.patch

Benjamin Dauvergne, 28 septembre 2020 10:16

Télécharger (3,13 ko)

Voir les différences:

Subject: [PATCH] misc: set Content-Disposition header for exports (#47060)

 bijoe/visualization/views.py | 7 +++++++
 tests/test_views.py          | 6 +++++-
 2 files changed, 12 insertions(+), 1 deletion(-)
bijoe/visualization/views.py
23 23
from django.contrib import messages
24 24
from django.utils.encoding import force_bytes, force_text
25 25
from django.utils.text import slugify
26
from django.utils.timezone import now
26 27
from django.utils.translation import ungettext, ugettext as _
27 28
from django.views.generic.edit import CreateView, DeleteView, UpdateView, FormView
28 29
from django.views.generic.list import MultipleObjectMixin
......
369 370

  
370 371
    def get(self, request, *args, **kwargs):
371 372
        response = HttpResponse(content_type='application/json')
373
        response['Content-Disposition'] = (
374
            'attachment; filename="%s"' % 'export_stats_%s.json' % now().strftime('%Y%m%d')
375
        )
372 376
        json.dump({'visualizations': [self.get_object().export_json()]}, response, indent=2)
373 377
        return response
374 378

  
......
414 418

  
415 419
    def get(self, request, *args, **kwargs):
416 420
        response = HttpResponse(content_type='application/json')
421
        response['Content-Disposition'] = (
422
            'attachment; filename="%s"' % 'export_stats_%s.json' % now().strftime('%Y%m%d')
423
        )
417 424
        json.dump(export_site(), response, indent=2)
418 425
        return response
419 426

  
tests/test_views.py
167 167
    app.get('/visualization/warehouse/schema1/fact1/', status=404)
168 168

  
169 169

  
170
def test_import_visualization(schema1, app, admin, visualization, settings):
170
def test_import_visualization(schema1, app, admin, visualization, settings, freezer):
171
    freezer.move_to('2020-01-01T00:00:00Z')
171 172
    settings.LANGUAGE_CODE = 'en-us'
172 173
    login(app, admin)
173 174
    resp = app.get('/visualization/%s/' % visualization.id)
174 175
    resp = resp.click('Export as JSON')
175 176
    assert resp.headers['content-type'] == 'application/json'
177
    assert resp.headers['content-disposition'] == 'attachment; filename="export_stats_20200101.json"'
176 178
    visualization_export = resp.text
177 179

  
178 180
    # invalid json
......
226 228

  
227 229
    # global export/import
228 230
    resp = app.get('/').click('Export')
231
    assert resp.headers['content-type'] == 'application/json'
232
    assert resp.headers['content-disposition'] == 'attachment; filename="export_stats_20200101.json"'
229 233
    visualizations_export = resp.text
230 234
    Visualization.objects.all().delete()
231 235

  
232
-