Skip to content
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

Expose Resource.extras as writable in the API #1660

Merged
merged 2 commits into from May 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,7 @@

## Current (in progress)

- Nothing yet
- Expose Resource.extras as writable in the API [#1660](https://github.com/opendatateam/udata/pull/1660)

## 1.3.9 (2018-05-07)

Expand Down
3 changes: 2 additions & 1 deletion udata/core/dataset/forms.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from udata.forms import ModelForm, fields, validators, widgets
from udata.forms import ModelForm, fields, validators
from udata.i18n import lazy_gettext as _

from udata.core.storages import resources
Expand Down Expand Up @@ -45,6 +45,7 @@ class BaseResourceForm(ModelForm):
published = fields.DateTimeField(
_('Publication date'),
description=_('The publication date of the resource'))
extras = fields.ExtrasField(extras=Resource.extras)


class ResourceForm(BaseResourceForm):
Expand Down
34 changes: 34 additions & 0 deletions udata/tests/api/test_datasets_api.py
Expand Up @@ -309,6 +309,34 @@ def test_dataset_api_update_with_resources(self):
dataset = Dataset.objects.first()
self.assertEqual(len(dataset.resources), initial_length + 1)

def test_dataset_api_update_new_resource_with_extras(self):
'''It should update a dataset with a new resource with extras'''
user = self.login()
dataset = VisibleDatasetFactory(owner=user)
data = dataset.to_dict()
resource_data = ResourceFactory.as_dict()
resource_data['extras'] = {'extra:id': 'id'}
data['resources'].append(resource_data)
response = self.put(url_for('api.dataset', dataset=dataset), data)
self.assert200(response)
dataset.reload()
resource = (
r for r in dataset.resources if r.title == resource_data['title']
).next()
self.assertEqual(resource.extras, {'extra:id': 'id'})

def test_dataset_api_update_existing_resource_with_extras(self):
'''It should update a dataset's existing resource with extras'''
user = self.login()
dataset = VisibleDatasetFactory(owner=user)
data = dataset.to_dict()
data['resources'][0]['extras'] = {'extra:id': 'id'}
response = self.put(url_for('api.dataset', dataset=dataset), data)
self.assert200(response)
dataset.reload()
resource = dataset.resources[0]
self.assertEqual(resource.extras, {'extra:id': 'id'})

def test_dataset_api_update_without_resources(self):
'''It should update a dataset from the API without resources'''
user = self.login()
Expand Down Expand Up @@ -562,12 +590,14 @@ def setUp(self):

def test_create(self):
data = ResourceFactory.as_dict()
data['extras'] = {'extra:id': 'id'}
with self.api_user():
response = self.post(url_for('api.resources',
dataset=self.dataset), data)
self.assert201(response)
self.dataset.reload()
self.assertEqual(len(self.dataset.resources), 1)
self.assertEqual(self.dataset.resources[0].extras, {'extra:id': 'id'})

def test_create_2nd(self):
self.dataset.resources.append(ResourceFactory())
Expand Down Expand Up @@ -675,6 +705,9 @@ def test_update(self):
'description': faker.text(),
'url': faker.url(),
'published': now.isoformat(),
'extras': {
'extra:id': 'id',
}
}
with self.api_user():
response = self.put(url_for('api.resource',
Expand All @@ -687,6 +720,7 @@ def test_update(self):
self.assertEqual(updated.title, data['title'])
self.assertEqual(updated.description, data['description'])
self.assertEqual(updated.url, data['url'])
self.assertEqual(updated.extras, {'extra:id': 'id'})
self.assertEqualDates(updated.published, now)

def test_bulk_update(self):
Expand Down