Skip to content

Commit

Permalink
Download option for story image (#1003)
Browse files Browse the repository at this point in the history
Co-authored-by: Tolga Akin <[email protected]>
  • Loading branch information
akintolga and Tolga Akin authored Mar 11, 2020
1 parent 581b821 commit cf29938
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions newsroom/wire/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from . import utils
from superdesk.metadata.item import not_analyzed

from .formatters.picture import PictureFormatter

blueprint = Blueprint('wire', __name__)

from . import views # noqa
Expand Down Expand Up @@ -70,6 +72,7 @@ def init_app(app):
app.download_formatter('nitf', NITFFormatter(), 'NITF', ['wire'])
app.download_formatter('newsmlg2', NewsMLG2Formatter(), 'NewsMLG2', ['wire'])
app.download_formatter('json', JsonFormatter(), 'Json', ['agenda'])
app.download_formatter('picture', PictureFormatter(), gettext('Story Image'), ['wire'])

app.add_template_global(utils.get_picture, 'get_picture')
app.add_template_global(utils.get_caption, 'get_caption')
Expand Down
1 change: 1 addition & 0 deletions newsroom/wire/formatters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ def __init__(cls, name, bases, attrs):
from .newsmlg2 import NewsMLG2Formatter # noqa
from .json import JsonFormatter # noqa
from .ninjs import NINJSFormatter # noqa
from .picture import PictureFormatter # noqa
19 changes: 19 additions & 0 deletions newsroom/wire/formatters/picture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from newsroom.wire.formatters.base import BaseFormatter
from newsroom.wire.utils import get_picture


class PictureFormatter(BaseFormatter):

MIMETYPE = 'image/jpeg'
FILE_EXTENSION = 'jpg'

def format_item(self, item, item_type='items'):
if item_type == 'agenda':
raise TypeError('Undefined format for agenda')

picture = get_picture(item)

if not picture:
raise TypeError('Undefined picture')

return picture.get('renditions', {}).get('baseImage', {}).get('media')
11 changes: 9 additions & 2 deletions newsroom/wire/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from bson import ObjectId
from operator import itemgetter
from flask import current_app as app, request, jsonify
from flask import current_app as app, request, jsonify, url_for
from eve.render import send_response
from eve.methods.get import get_internal
from werkzeug.utils import secure_filename
Expand Down Expand Up @@ -173,7 +173,14 @@ def download(_ids):
formatter = app.download_formatters[_format]['formatter']
mimetype = None
attachment_filename = '%s-newsroom.zip' % utcnow().strftime('%Y%m%d%H%M')
if len(items) == 1 or _format == 'watch_lists':
if _format == 'picture':
try:
media_id = formatter.format_item(items[0], item_type=item_type)
return flask.redirect(
url_for('upload.get_upload', media_id=media_id, filename='baseimage.%s' % formatter.FILE_EXTENSION))
except TypeError:
return flask.abort(404)
elif len(items) == 1 or _format == 'watch_lists':
item = items[0]
args_item = item if _format != 'watch_lists' else items
parse_dates(item) # fix for old items
Expand Down

0 comments on commit cf29938

Please sign in to comment.