Skip to content

Commit

Permalink
Normalize resource.format on form
Browse files Browse the repository at this point in the history
  • Loading branch information
abulte committed Apr 3, 2018
1 parent 3564233 commit a03bf9b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
11 changes: 10 additions & 1 deletion udata/core/dataset/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class ChecksumForm(ModelForm):
value = fields.StringField()


def normalize_format(data):
'''Normalize format field: strip and lowercase'''
if data:
return data.strip().lower()


class BaseResourceForm(ModelForm):
title = fields.StringField(_('Title'), [validators.required()])
description = fields.MarkdownField(_('Description'))
Expand All @@ -33,7 +39,10 @@ class BaseResourceForm(ModelForm):
'a remote file or an API'))
url = fields.UploadableURLField(
_('URL'), [validators.required()], storage=resources)
format = fields.StringField(_('Format'))
format = fields.StringField(
_('Format'),
filters=[normalize_format],
)
checksum = fields.FormField(ChecksumForm)
mime = fields.StringField(
_('Mime type'),
Expand Down
24 changes: 24 additions & 0 deletions udata/migrations/2018-04-03-normalize-resource-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* resource.format: trim and convert to lower case
*/

var count = 0;

function normalizeFormat(resource) {
if (resource.format) {
resource.format = resource.format.trim().toLowerCase();
}
return resource;
}

db.dataset.find().forEach(function(dataset) {
if (dataset.resources && dataset.resources.length) {
const result = db.dataset.update(
{_id: dataset._id},
{$set: {resources: dataset.resources.map(normalizeFormat)}}
);
count += result.nModified;
}
});

print(`${count} datasets updated.`);
12 changes: 12 additions & 0 deletions udata/tests/api/test_datasets_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,18 @@ def test_create(self):
self.dataset.reload()
self.assertEqual(len(self.dataset.resources), 1)

def test_create_normalize_format(self):
_format = ' FORMAT '
data = ResourceFactory.as_dict()
data['format'] = _format
with self.api_user():
response = self.post(url_for('api.resources',
dataset=self.dataset), data)
self.assert201(response)
self.dataset.reload()
self.assertEqual(self.dataset.resources[0].format,
_format.strip().lower())

def test_create_2nd(self):
self.dataset.resources.append(ResourceFactory())
self.dataset.save()
Expand Down

0 comments on commit a03bf9b

Please sign in to comment.