From 896de5072265e5b012844f3a20de5a1b40da564c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Bult=C3=A9?= Date: Thu, 15 Oct 2020 15:14:24 +0200 Subject: [PATCH] Add a setting for SUPPORTED_MIME_TYPES (#9) --- CHANGELOG.md | 2 +- tests/test_tabular_preview_preview.py | 11 ++++------- udata_tabular_preview/preview.py | 12 +++--------- udata_tabular_preview/settings.py | 7 +++++++ 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef10e80..75d4f2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Current (in progress) -- Nothing yet +- Add a setting for SUPPORTED_MIME_TYPES [#9](https://github.com/opendatateam/udata-tabular-preview/pull/9) ## 2.0.1 (2020-06-17) diff --git a/tests/test_tabular_preview_preview.py b/tests/test_tabular_preview_preview.py index b79a977..8550e49 100644 --- a/tests/test_tabular_preview_preview.py +++ b/tests/test_tabular_preview_preview.py @@ -1,15 +1,12 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - import pytest from urllib.parse import quote_plus from udata.core.dataset.factories import ResourceFactory -from udata_tabular_preview.preview import SUPPORTED_MIME_TYPES -MIME_TYPE = SUPPORTED_MIME_TYPES[0] +MIME_TYPE = 'text/csv' +DUMMY_MIMES = ['text/csv', 'text/toto'] MAX_SIZE = 50000 pytestmark = [ @@ -24,11 +21,11 @@ def expected_url(url): return '/tabular/preview/?url={0}'.format(encoded_url) -@pytest.mark.parametrize('mime', SUPPORTED_MIME_TYPES) +@pytest.mark.parametrize('mime', DUMMY_MIMES) @pytest.mark.options(TABULAR_CSVAPI_URL='http://preview.me/') +@pytest.mark.options(TABULAR_SUPPORTED_MIME_TYPES=DUMMY_MIMES) def test_display_preview_for_tabular_resources(mime): resource = ResourceFactory(mime=mime) - assert resource.preview_url == expected_url(resource.url) diff --git a/udata_tabular_preview/preview.py b/udata_tabular_preview/preview.py index 6b6107e..be3dc00 100644 --- a/udata_tabular_preview/preview.py +++ b/udata_tabular_preview/preview.py @@ -2,13 +2,6 @@ from udata.core.dataset.preview import PreviewPlugin -SUPPORTED_MIME_TYPES = ( - 'text/csv', - 'application/vnd.ms-excel', - 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', -) - - class TabularPreview(PreviewPlugin): fallback = True @@ -19,10 +12,11 @@ def server_url(self): def can_preview(self, resource): has_config = bool(self.server_url) + supported_mimes = current_app.config.get('TABULAR_SUPPORTED_MIME_TYPES') extras_mime = resource.extras.get('check:headers:content-type') is_supported = ( - extras_mime in SUPPORTED_MIME_TYPES - or resource.mime in SUPPORTED_MIME_TYPES + extras_mime in supported_mimes + or resource.mime in supported_mimes ) is_remote = resource.filetype == 'remote' diff --git a/udata_tabular_preview/settings.py b/udata_tabular_preview/settings.py index 9f2e303..2336b0e 100644 --- a/udata_tabular_preview/settings.py +++ b/udata_tabular_preview/settings.py @@ -15,3 +15,10 @@ # Default page size TABULAR_PAGE_SIZE = 50 + +# Supported mimes +TABULAR_SUPPORTED_MIME_TYPES = ( + 'text/csv', + 'application/vnd.ms-excel', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', +)