added art api endpoints
This commit is contained in:
parent
afdb04d90c
commit
8dcaea83a8
1
sarsoo/api/__init__.py
Normal file
1
sarsoo/api/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
from .art_api import art_api_print
|
57
sarsoo/api/art_api.py
Normal file
57
sarsoo/api/art_api.py
Normal 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
|
@ -25,3 +25,7 @@ def tag_view(tag):
|
|||||||
def all():
|
def all():
|
||||||
sections = getPopulatedTagDicts()
|
sections = getPopulatedTagDicts()
|
||||||
return render_template('art/all.html', staticroot = staticbucketurl, tags=sections)
|
return render_template('art/all.html', staticroot = staticbucketurl, tags=sections)
|
||||||
|
|
||||||
|
|
||||||
|
def getAssetUrl(name):
|
||||||
|
return staticbucketurl + 'art/' + name + '.jpg'
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from google.cloud import firestore
|
from google.cloud import firestore
|
||||||
|
from google.cloud import exceptions
|
||||||
|
|
||||||
staticbucketurl = 'https://storage.googleapis.com/sarsooxyzstatic/'
|
staticbucketurl = 'https://storage.googleapis.com/sarsooxyzstatic/'
|
||||||
|
|
||||||
@ -11,7 +12,7 @@ def getTagDicts():
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
tags = art_tags_collection.get()
|
tags = art_tags_collection.get()
|
||||||
except google.cloud.exceptions.NotFound:
|
except exceptions.NotFound:
|
||||||
return 'no such document'
|
return 'no such document'
|
||||||
|
|
||||||
dicts = list(map(lambda x: x.to_dict(), tags))
|
dicts = list(map(lambda x: x.to_dict(), tags))
|
||||||
@ -32,7 +33,7 @@ def getPopulatedTagDict(name):
|
|||||||
for image in tag_dicts['art']:
|
for image in tag_dicts['art']:
|
||||||
image_list.append(image.get().to_dict())
|
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
|
return tag_dicts
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import os
|
|||||||
|
|
||||||
from .art import art_print
|
from .art import art_print
|
||||||
from .music import music_print
|
from .music import music_print
|
||||||
|
from .api import art_api_print
|
||||||
|
|
||||||
# Project ID is determined by the GCLOUD_PROJECT environment variable
|
# Project ID is determined by the GCLOUD_PROJECT environment variable
|
||||||
db = firestore.Client()
|
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(art_print, url_prefix='/art')
|
||||||
app.register_blueprint(music_print, url_prefix='/music')
|
app.register_blueprint(music_print, url_prefix='/music')
|
||||||
|
app.register_blueprint(art_api_print, url_prefix='/api/art')
|
||||||
|
|
||||||
staticbucketurl = 'https://storage.googleapis.com/sarsooxyzstatic/'
|
staticbucketurl = 'https://storage.googleapis.com/sarsooxyzstatic/'
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user