added art api endpoints

This commit is contained in:
aj 2019-06-04 11:40:45 +01:00
parent afdb04d90c
commit 8dcaea83a8
5 changed files with 67 additions and 2 deletions

1
sarsoo/api/__init__.py Normal file
View File

@ -0,0 +1 @@
from .art_api import art_api_print

57
sarsoo/api/art_api.py Normal file
View File

@ -0,0 +1,57 @@
from flask import Blueprint, jsonify, abort
from sarsoo.art import db
from sarsoo.art.art import getAssetUrl
art_api_print = Blueprint('artapi', __name__)
@art_api_print.route('/', methods=['GET'])
def collections():
tagdicts = db.getTagDicts()
collections = []
for dict in tagdicts:
collections.append({
'name': dict['name'],
'id': dict['doc_name'],
'description': dict['description'],
'index': dict['index'],
'splash_url': getAssetUrl(dict['splash']['file_name'])
})
response = {'collections': collections}
return jsonify(response)
@art_api_print.route('/<id>', methods=['GET'])
def getcollection(id):
try:
tagdict = db.getPopulatedTagDict(id)
except TypeError as e:
abort(404)
artlist = []
for image in tagdict['images']:
artlist.append({
'file_url': getAssetUrl(image['file_name']),
'date': image['date'].strftime('%d %B %y')
})
response = {'name': tagdict['name'],
'description': tagdict['description'],
'art': artlist}
return jsonify(response)
@art_api_print.errorhandler(404)
def error400(error):
errorresponse = {'error': 'collection not found'}
return jsonify(errorresponse), 404

View File

@ -25,3 +25,7 @@ def tag_view(tag):
def all():
sections = getPopulatedTagDicts()
return render_template('art/all.html', staticroot = staticbucketurl, tags=sections)
def getAssetUrl(name):
return staticbucketurl + 'art/' + name + '.jpg'

View File

@ -1,4 +1,5 @@
from google.cloud import firestore
from google.cloud import exceptions
staticbucketurl = 'https://storage.googleapis.com/sarsooxyzstatic/'
@ -11,7 +12,7 @@ def getTagDicts():
try:
tags = art_tags_collection.get()
except google.cloud.exceptions.NotFound:
except exceptions.NotFound:
return 'no such document'
dicts = list(map(lambda x: x.to_dict(), tags))
@ -32,7 +33,7 @@ def getPopulatedTagDict(name):
for image in tag_dicts['art']:
image_list.append(image.get().to_dict())
tag_dicts['images'] = sorted(image_list, key = lambda k: k['date'], reverse = True)
tag_dicts['images'] = sorted(image_list, key=lambda k: k['date'], reverse=True)
return tag_dicts

View File

@ -4,6 +4,7 @@ import os
from .art import art_print
from .music import music_print
from .api import art_api_print
# Project ID is determined by the GCLOUD_PROJECT environment variable
db = firestore.Client()
@ -12,6 +13,7 @@ app = Flask(__name__, static_folder=os.path.join(os.path.dirname(__file__), '..'
app.register_blueprint(art_print, url_prefix='/art')
app.register_blueprint(music_print, url_prefix='/music')
app.register_blueprint(art_api_print, url_prefix='/api/art')
staticbucketurl = 'https://storage.googleapis.com/sarsooxyzstatic/'