-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/80 new upload widget #83
Open
aivuk
wants to merge
45
commits into
master
Choose a base branch
from
feature/80-new-upload-widget
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
1c984dd
Add new action to infer a tabular resource schema
aivuk 154d529
Update ckanext/validation/logic.py
aivuk 3b1773c
Remove coverage report
aivuk a167acb
Merge branch 'feature/76-add-resource-table-schema-infer' of github.c…
aivuk 670d896
correct toolkit imported name
aivuk 1dd1a8a
use new action endpoint with upload widget to create a resource
aivuk 0f45263
remove default resource upload file field
aivuk 124a0f7
Update widget and logic to replace already existing resource files
aivuk f2af792
Pass the url_type parameter to the ckan-uploader component
aivuk 52ca754
get variables from resource form from hidden inputs
aivuk e8b5b42
add ckan_uploader snippet
aivuk 6c167a6
Update logic to add another resource after saving one
aivuk a3c6e27
Add some comments to ckan-uploader-module
aivuk a225742
Corrects behaviour for uploaded file is not tabular
aivuk 0644e0a
use custom resource_create and resource_update instead of new actions…
aivuk 4c61a0b
remove check for content_length in uploaded resource schema file
aivuk 6ece563
Remove unused actions
amercader 61ff6d4
Use helper to get package id from url
aivuk d744d2f
Import ckan.model on helpers
aivuk 9c16f8f
Fix blueprints missing imports
aivuk fb8f7f7
add resource edit new endpoint
aivuk 28eec48
Create resource update endpoint and update ckan-uploader widget
aivuk 547c5ac
Correct default return values for helpers
aivuk 5fb0ec0
Get initialization variables for ckan-uploade its template.
aivuk 8ac15f1
Remove custom resource_form.html template.
aivuk bfd400a
Get the schema from the returned schema from the action
aivuk 036262e
Stop infering the schema as default in resource create and update
aivuk 25e39ad
Add helpers that are used by ckan_uploader.html template
aivuk 93e1956
Fix template for ckan_uploader adding correctly the variables intial …
aivuk 431beeb
shorter form to compare schema_url value
aivuk 63d8bcd
add logic to switch between file upload and resource url
aivuk 930bb35
Merge branch 'feature/80-new-upload-widget' of github.com:frictionles…
aivuk a9e5459
remove erroneous test for schema file uploaded size
aivuk 56ab8bb
Remove scrolling to schema json on resource edit
aivuk ebcc71a
Add basic create/update tests
amercader 78a3351
Bump frictionless to fix schema infer errors
amercader 25dbed6
Revert accidental changes in ckan-uploader.js
amercader 95f31e4
Revert changes made to resource_create / resource_update actions
amercader 9cbb754
Add turn_off_validation context manager
amercader d9598c5
Don't run validations when creating the draft resource
amercader 3af20e2
Specify format in tests because of ckan/ckan#7415
amercader 9236615
Remove duplicated test
amercader 0198797
Bump responses
amercader d0f780d
Revert file size check removal in a9e5459
amercader f990969
Remove debugger
amercader File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,347 @@ | ||
import io | ||
import pytest | ||
import responses | ||
|
||
from ckantoolkit import config | ||
from ckantoolkit.tests.factories import Sysadmin, Dataset, Resource | ||
from ckantoolkit.tests.helpers import call_action | ||
from ckanext.validation.tests.helpers import VALID_CSV, INVALID_CSV | ||
|
||
|
||
pytestmark = pytest.mark.usefixtures("clean_db", "validation_setup", "mock_uploads") | ||
|
||
|
||
def _new_resource_upload_url(dataset_id): | ||
|
||
url = "/dataset/{}/resource/file".format(dataset_id) | ||
|
||
return url | ||
|
||
|
||
def _edit_resource_upload_url(dataset_id, resource_id): | ||
|
||
url = "/dataset/{}/resource/{}/file".format(dataset_id, resource_id) | ||
|
||
return url | ||
|
||
|
||
def _get_env(): | ||
user = Sysadmin() | ||
return {"REMOTE_USER": user["name"]} | ||
|
||
|
||
# Create resources | ||
|
||
|
||
def test_create_upload_with_schema(app): | ||
|
||
dataset = Dataset() | ||
|
||
data = { | ||
"upload": (io.BytesIO(bytes(VALID_CSV.encode("utf8"))), "valid.csv"), | ||
} | ||
|
||
app.post( | ||
url=_new_resource_upload_url(dataset["id"]), extra_environ=_get_env(), data=data | ||
) | ||
|
||
dataset = call_action("package_show", id=dataset["id"]) | ||
|
||
assert len(dataset["resources"]) == 1 | ||
assert dataset["resources"][0]["format"] == "CSV" | ||
assert dataset["resources"][0]["url_type"] == "upload" | ||
|
||
assert dataset["resources"][0]["schema"] == { | ||
"fields": [ | ||
{"name": "a", "type": "integer"}, | ||
{"name": "b", "type": "integer"}, | ||
{"name": "c", "type": "integer"}, | ||
{"name": "d", "type": "integer"}, | ||
] | ||
} | ||
|
||
|
||
def test_create_upload_no_tabular_no_schema(app): | ||
|
||
dataset = Dataset() | ||
|
||
data = { | ||
"upload": (io.BytesIO(b"test file"), "some.txt"), | ||
} | ||
|
||
app.post( | ||
url=_new_resource_upload_url(dataset["id"]), extra_environ=_get_env(), data=data | ||
) | ||
|
||
dataset = call_action("package_show", id=dataset["id"]) | ||
|
||
assert len(dataset["resources"]) == 1 | ||
assert dataset["resources"][0]["format"] == "TXT" | ||
assert dataset["resources"][0]["url_type"] == "upload" | ||
|
||
assert "schema" not in dataset["resources"][0] | ||
|
||
|
||
@responses.activate | ||
def test_create_url_with_schema(app): | ||
|
||
url = "https://example.com/valid.csv" | ||
|
||
responses.add(responses.GET, url, body=VALID_CSV) | ||
responses.add_passthru(config["solr_url"]) | ||
|
||
dataset = Dataset() | ||
|
||
data = {"url": url} | ||
|
||
app.post( | ||
url=_new_resource_upload_url(dataset["id"]), extra_environ=_get_env(), data=data | ||
) | ||
|
||
dataset = call_action("package_show", id=dataset["id"]) | ||
|
||
assert len(dataset["resources"]) == 1 | ||
assert dataset["resources"][0]["format"] == "CSV" | ||
assert dataset["resources"][0]["url_type"] is None | ||
|
||
assert dataset["resources"][0]["schema"] == { | ||
"fields": [ | ||
{"name": "a", "type": "integer"}, | ||
{"name": "b", "type": "integer"}, | ||
{"name": "c", "type": "integer"}, | ||
{"name": "d", "type": "integer"}, | ||
] | ||
} | ||
|
||
|
||
@responses.activate | ||
def test_create_url_no_tabular(app): | ||
|
||
url = "https://example.com/some.txt" | ||
|
||
responses.add(responses.GET, url, body="some text") | ||
responses.add_passthru(config["solr_url"]) | ||
|
||
dataset = Dataset() | ||
|
||
data = {"url": url} | ||
|
||
app.post( | ||
url=_new_resource_upload_url(dataset["id"]), extra_environ=_get_env(), data=data | ||
) | ||
|
||
dataset = call_action("package_show", id=dataset["id"]) | ||
|
||
assert len(dataset["resources"]) == 1 | ||
assert dataset["resources"][0]["format"] == "TXT" | ||
assert dataset["resources"][0]["url_type"] is None | ||
|
||
assert "schema" not in dataset["resources"][0] | ||
|
||
|
||
# Update resources | ||
|
||
|
||
def test_update_upload_with_schema(app): | ||
|
||
resource = Resource() | ||
|
||
data = { | ||
"upload": (io.BytesIO(bytes(VALID_CSV.encode("utf8"))), "valid.csv"), | ||
} | ||
|
||
app.post( | ||
url=_edit_resource_upload_url(resource["package_id"], resource["id"]), | ||
extra_environ=_get_env(), | ||
data=data, | ||
) | ||
|
||
resource = call_action("resource_show", id=resource["id"]) | ||
|
||
assert resource["format"] == "CSV" | ||
assert resource["url_type"] == "upload" | ||
|
||
assert resource["schema"] == { | ||
"fields": [ | ||
{"name": "a", "type": "integer"}, | ||
{"name": "b", "type": "integer"}, | ||
{"name": "c", "type": "integer"}, | ||
{"name": "d", "type": "integer"}, | ||
] | ||
} | ||
|
||
|
||
def test_update_upload_no_tabular_no_schema(app): | ||
|
||
resource = Resource() | ||
|
||
data = { | ||
"upload": (io.BytesIO(b"test file"), "some.txt"), | ||
} | ||
|
||
app.post( | ||
url=_edit_resource_upload_url(resource["package_id"], resource["id"]), | ||
extra_environ=_get_env(), | ||
data=data, | ||
) | ||
|
||
resource = call_action("resource_show", id=resource["id"]) | ||
|
||
assert resource["format"] == "TXT" | ||
assert resource["url_type"] == "upload" | ||
|
||
assert "schema" not in resource | ||
|
||
|
||
def test_update_updates_schema(app): | ||
|
||
dataset = Dataset() | ||
|
||
data = { | ||
"upload": (io.BytesIO(bytes(VALID_CSV.encode("utf8"))), "valid.csv"), | ||
} | ||
|
||
app.post( | ||
url=_new_resource_upload_url(dataset["id"]), extra_environ=_get_env(), data=data | ||
) | ||
|
||
dataset = call_action("package_show", id=dataset["id"]) | ||
|
||
assert dataset["resources"][0]["schema"] | ||
|
||
data = { | ||
"upload": (io.BytesIO(b"e,f\n5,6"), "some.other.csv"), | ||
} | ||
|
||
app.post( | ||
url=_edit_resource_upload_url(dataset["id"], dataset["resources"][0]["id"]), | ||
extra_environ=_get_env(), | ||
data=data, | ||
) | ||
|
||
resource = call_action("resource_show", id=dataset["resources"][0]["id"]) | ||
|
||
assert resource["schema"] == { | ||
"fields": [ | ||
{"name": "e", "type": "integer"}, | ||
{"name": "f", "type": "integer"}, | ||
] | ||
} | ||
|
||
|
||
def test_update_removes_schema(app): | ||
|
||
dataset = Dataset() | ||
|
||
data = { | ||
"upload": (io.BytesIO(bytes(VALID_CSV.encode("utf8"))), "valid.csv"), | ||
} | ||
|
||
app.post( | ||
url=_new_resource_upload_url(dataset["id"]), extra_environ=_get_env(), data=data | ||
) | ||
|
||
dataset = call_action("package_show", id=dataset["id"]) | ||
|
||
assert dataset["resources"][0]["schema"] | ||
|
||
data = { | ||
"upload": (io.BytesIO(b"test file"), "some.txt"), | ||
} | ||
|
||
app.post( | ||
url=_edit_resource_upload_url(dataset["id"], dataset["resources"][0]["id"]), | ||
extra_environ=_get_env(), | ||
data=data, | ||
) | ||
|
||
resource = call_action("resource_show", id=dataset["resources"][0]["id"]) | ||
|
||
assert "schema" not in resource | ||
|
||
|
||
def test_update_upload_no_tabular_no_schema(app): | ||
|
||
resource = Resource() | ||
|
||
data = { | ||
"upload": (io.BytesIO(b"test file"), "some.txt"), | ||
} | ||
|
||
app.post( | ||
url=_edit_resource_upload_url(resource["package_id"], resource["id"]), | ||
extra_environ=_get_env(), | ||
data=data, | ||
) | ||
|
||
resource = call_action("resource_show", id=resource["id"]) | ||
|
||
assert resource["format"] == "TXT" | ||
assert resource["url_type"] == "upload" | ||
|
||
assert "schema" not in resource | ||
|
||
|
||
@responses.activate | ||
def test_update_url_with_schema(app): | ||
url = "https://example.com/valid.csv" | ||
|
||
responses.add(responses.GET, url, body=VALID_CSV) | ||
responses.add_passthru(config["solr_url"]) | ||
|
||
resource = Resource() | ||
|
||
data = { | ||
"url": url, | ||
# CKAN does not refresh the format, see https://github.com/ckan/ckan/issues/7415 | ||
"format": "CSV", | ||
} | ||
|
||
app.post( | ||
url=_edit_resource_upload_url(resource["package_id"], resource["id"]), | ||
extra_environ=_get_env(), | ||
data=data, | ||
) | ||
|
||
resource = call_action("resource_show", id=resource["id"]) | ||
|
||
assert resource["format"] == "CSV" | ||
assert resource["url_type"] is None | ||
|
||
assert resource["schema"] == { | ||
"fields": [ | ||
{"name": "a", "type": "integer"}, | ||
{"name": "b", "type": "integer"}, | ||
{"name": "c", "type": "integer"}, | ||
{"name": "d", "type": "integer"}, | ||
] | ||
} | ||
|
||
|
||
@responses.activate | ||
def test_update_url_no_tabular_no_schema(app): | ||
url = "https://example.com/some.txt" | ||
|
||
responses.add(responses.GET, url, body="some text") | ||
responses.add_passthru(config["solr_url"]) | ||
|
||
resource = Resource() | ||
|
||
data = { | ||
"url": url, | ||
# CKAN does not refresh the format, see https://github.com/ckan/ckan/issues/7415 | ||
"format": "TXT", | ||
} | ||
|
||
app.post( | ||
url=_edit_resource_upload_url(resource["package_id"], resource["id"]), | ||
extra_environ=_get_env(), | ||
data=data, | ||
) | ||
|
||
resource = call_action("resource_show", id=resource["id"]) | ||
|
||
assert resource["format"] == "TXT" | ||
assert resource["url_type"] is None | ||
|
||
assert "schema" not in resource |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with the new widget, I'm retrieving the format when the user replace the uploaded resource and changing it on the resource edit form. if the user save the changes the new format is updated but I agree with you, it would be better to do it in the backend