Skip to content

Commit

Permalink
Merge pull request #591 from petrjasek/fix-image-crop-custom-size
Browse files Browse the repository at this point in the history
fix(crop): cast image crop vocab width/height values to int
  • Loading branch information
petrjasek committed Sep 30, 2016
2 parents e5a9804 + 7c269dc commit 0123a46
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
25 changes: 25 additions & 0 deletions features/vocabularies.feature
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,28 @@ Feature: Vocabularies
]}
"""
Then we get updated response

@auth
Scenario: Cast crop image width/height to int
Given "vocabularies"
"""
[{"_id": "crop_sizes", "items": [
{
"is_active" : true,
"width" : "300",
"name" : "foo",
"height" : "200"
}
]}]
"""
When we get "/vocabularies"
Then we get list with 1 items
"""
{"_items": [{"items": [{"width": 300, "height": 200}]}]}
"""

When we get "/vocabularies/crop_sizes"
Then we get existing resource
"""
{"items": [{"width": 300, "height": 200}]}
"""
24 changes: 23 additions & 1 deletion superdesk/vocabularies/vocabularies.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from flask import request, current_app as app
from eve.utils import config
from eve.methods.common import serialize_value

from superdesk import privilege
from superdesk.notification import push_notification
Expand All @@ -30,6 +31,15 @@
description="User can manage vocabularies' contents.")


# TODO(petr): add api to specify vocabulary schema
vocab_schema = {
'crop_sizes': {
'width': {'type': 'integer'},
'height': {'type': 'integer'},
}
}


class VocabulariesResource(Resource):
schema = {
'_id': {
Expand Down Expand Up @@ -99,13 +109,14 @@ def on_fetched(self, doc):

for item in doc[config.ITEMS]:
self._filter_inactive_vocabularies(item)
self._cast_items(item)

def on_fetched_item(self, doc):
"""
Overriding to filter out inactive vocabularies and pops out 'is_active' property from the response.
"""

self._filter_inactive_vocabularies(doc)
self._cast_items(doc)

def on_update(self, updates, original):
""" Checks the duplicates if a unique field is defined """
Expand Down Expand Up @@ -155,6 +166,17 @@ def _filter_inactive_vocabularies(self, item):

item['items'] = list(active_vocs)

def _cast_items(self, vocab):
"""Cast values in vocabulary items using predefined schema.
:param vocab
"""
schema = vocab_schema.get(vocab.get('_id'), {})
for item in vocab.get('items', []):
for field, field_schema in schema.items():
if field in item:
item[field] = serialize_value(field_schema['type'], item[field])

def _send_notification(self, updated_vocabulary):
"""
Sends notification about the updated vocabulary to all the connected clients.
Expand Down

0 comments on commit 0123a46

Please sign in to comment.