Skip to content

Commit

Permalink
Use extras to enable preview (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti authored Jun 11, 2020
1 parent 74b3f31 commit 813d583
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Current (in progress)

- Nothing yet
- Use extras attributes to enable preview [#8](https://github.com/opendatateam/udata-tabular-preview/pull/8)

## 2.0.0 (2020-03-11)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_tabular_preview_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ def test_display_preview_with_max_size(size):
assert resource.preview_url == expected_url(resource.url)


@pytest.mark.options(TABULAR_CSVAPI_URL='http://preview.me/',
TABULAR_MAX_SIZE=MAX_SIZE)
def test_display_preview_using_extras():
extras = {
'check:headers:content-type': MIME_TYPE,
'check:headers:content-length': MAX_SIZE - 1,
}
resource = ResourceFactory(
mime=None,
filesize=None,
extras=extras
)

assert resource.preview_url == expected_url(resource.url)


@pytest.mark.options(TABULAR_CSVAPI_URL='http://preview.me/',
TABULAR_MAX_SIZE=MAX_SIZE)
def test_no_preview_for_resource_over_max_size():
Expand Down
15 changes: 13 additions & 2 deletions udata_tabular_preview/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@ def server_url(self):

def can_preview(self, resource):
has_config = bool(self.server_url)
is_supported = resource.mime in 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
)

is_remote = resource.filetype == 'remote'
allow_remote = current_app.config.get('TABULAR_ALLOW_REMOTE')
is_allowed = allow_remote or not is_remote

max_size = current_app.config.get('TABULAR_MAX_SIZE')
size_ok = not max_size or (resource.filesize or float('inf')) <= max_size
extras_size = resource.extras.get('check:headers:content-length')
size_ok = (
not max_size
or (extras_size or resource.filesize or float("inf")) <= max_size
)

return all((has_config, is_supported, is_allowed, size_ok))

Expand Down

0 comments on commit 813d583

Please sign in to comment.