Skip to content

Commit

Permalink
[SDCP-201] - Removing image if the update doesn't have it (#1025)
Browse files Browse the repository at this point in the history
* [SDCP-201] - Removing image if the update doesn't have it
  • Loading branch information
akintolga authored Mar 26, 2020
1 parent d72d223 commit 92b64c0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
4 changes: 2 additions & 2 deletions assets/wire/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function getIntVersion(item) {
* @return {Array}
*/
export function getVideos(item) {
return item.type === 'video' ? [item] : Object.values(get(item, 'associations', {})).filter((assoc) => get(assoc, 'type') === 'video');
return item.type === 'video' ? [item] : Object.values(get(item, 'associations', {}) || {}).filter((assoc) => get(assoc, 'type') === 'video');
}


Expand All @@ -45,7 +45,7 @@ export function getPicture(item) {
}

function getBodyPicture(item) {
const pictures = Object.values(get(item, 'associations', {})).filter((assoc) => get(assoc, 'type') === 'picture');
const pictures = Object.values(get(item, 'associations', {}) || {}).filter((assoc) => get(assoc, 'type') === 'picture');

return pictures.length ? pictures[0] : null;
}
Expand Down
10 changes: 6 additions & 4 deletions newsroom/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def push():
superdesk.get_resource_service('agenda').enhance_items([agenda])
notify_new_item(agenda, check_topics=True)
elif item.get('type') == 'text':
orig = app.data.find_one('wire_search', req=None, _id=item['guid'])
item['_id'] = publish_item(item, is_new=orig is None)
orig = superdesk.get_resource_service('items').find_one(req=None, _id=item['guid'])
item['_id'] = publish_item(item, orig)
notify_new_item(item, check_topics=orig is None)
elif item['type'] == 'planning_featured':
publish_planning_featured(item)
Expand All @@ -101,7 +101,7 @@ def set_dates(doc):
doc.setdefault(app.config['VERSION'], 1)


def publish_item(doc, is_new):
def publish_item(doc, original):
"""Duplicating the logic from content_api.publish service."""
set_dates(doc)
doc['firstpublished'] = parse_date_str(doc.get('firstpublished'))
Expand Down Expand Up @@ -130,8 +130,10 @@ def publish_item(doc, is_new):
agenda_items = superdesk.get_resource_service('agenda').set_delivery(doc)
if agenda_items:
[notify_new_item(item, check_topics=False) for item in agenda_items]
publish_item_signal.send(app._get_current_object(), item=doc, is_new=is_new)
publish_item_signal.send(app._get_current_object(), item=doc, is_new=original is None)
_id = service.create([doc])[0]
if 'associations' not in doc and original is not None and bool(original.get('associations', {})):
service.patch(_id, updates={'associations': None})
if 'evolvedfrom' in doc and parent_item:
service.system_update(parent_item['_id'], {'nextversion': _id}, parent_item)
return _id
Expand Down
49 changes: 49 additions & 0 deletions tests/test_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,55 @@ def test_push_featuremedia_generates_renditions(client):
assert 200 == resp.status_code


def test_push_update_removes_featuremedia(client):
media_id = str(bson.ObjectId())
upload_binary('picture.jpg', client, media_id=media_id)
item = {
'guid': 'test',
'type': 'text',
'version': 1,
'associations': {
'featuremedia': {
'type': 'picture',
'mimetype': 'image/jpeg',
'renditions': {
'4-3': {
'media': media_id,
},
'baseImage': {
'media': media_id,
},
'viewImage': {
'media': media_id,
}
}
}
}
}

resp = client.post('/push', data=json.dumps(item), content_type='application/json')
assert 200 == resp.status_code

resp = client.get('/wire/test?format=json')
data = json.loads(resp.get_data())
assert 200 == resp.status_code
assert data['associations'] is not None

item = {
'guid': 'test',
'type': 'text',
'version': 2,
}

resp = client.post('/push', data=json.dumps(item), content_type='application/json')
assert 200 == resp.status_code

resp = client.get('/wire/test?format=json')
data = json.loads(resp.get_data())
assert 200 == resp.status_code
assert data['associations'] is None


def test_push_featuremedia_has_renditions_for_existing_media(client):
media_id = str(bson.ObjectId())
upload_binary('picture.jpg', client, media_id=media_id)
Expand Down

0 comments on commit 92b64c0

Please sign in to comment.