Commit 9ab8bef8 authored by Maarten van der Veen's avatar Maarten van der Veen
parents 86519a73 f8a6a501
......@@ -3,7 +3,17 @@ Web Services
Usage of the development API v0.1
---------------------------------
request parameters:
'<attribute>' - pass one or more attribute names, such as '<attribute=Name,Vegetation>'
NOTE: Currently the attribute names may contain capital letters and spaces.
Spaces should be substituted as '<%20>' int the URL, for example
'<Climate%20Zone>'. An alternative "coding-friendly" attribute naming will be implemented shortly,
as unique strings for each attribute whithout spaces and upper case letters.
'<format>' - use '<format=prettytext>' to get the nicely formatted attribute names in the JSON,
instead of the "coding-friendly" attribute names.
Examples:
.. code-block:: shell
http://0.0.0.0:5000/api/v0.1
......@@ -15,6 +25,12 @@ Usage of the development API v0.1
http://0.0.0.0:5000/api/v0.1/shelters/3
# Get a specific shelter via shelter ID
http://0.0.0.0:5000/api/v0.1/shelters?attribute=Vegetation
# Get all shelters which have an atttribute value for "Vegetation"
http://0.0.0.0:5000/api/v0.1/shelters?attribute=vegetation&format=prettytext
# Get all shelters which have an atttribute value for "Vegetation", with nicely formatted attribute names
http://0.0.0.0:5000/api/v0.1/attributes/Climate zone
# Get all available values of an attribute
......
......@@ -29,7 +29,8 @@ with app.app_context():
app.register_blueprint(views.shelter_bp)
app.register_blueprint(views.shelters_bp)
app.register_blueprint(views.admin_bp)
app.register_blueprint(views.api_bp)
# API
from web import processors
# 'User' Web service
......@@ -114,4 +115,4 @@ with app.app_context():
if __name__ == "__main__":
app.run(host=conf.WEBSERVER_HOST,
port=conf.WEBSERVER_PORT,
debug=conf.WEBSERVER_DEBUG)
\ No newline at end of file
debug=conf.WEBSERVER_DEBUG)
......@@ -5,6 +5,7 @@ from web.views import views
from web.views.page import recommendations
from web.views.admin import *
from web.views.session_mgmt import *
from web.views.shelterapi import api_bp
import conf
from flask import g
......
......@@ -14,7 +14,7 @@ __revision__ = ""
__copyright__ = ""
__license__ = ""
from flask import Blueprint, jsonify
from flask import Blueprint, jsonify, request
from collections import defaultdict
from web.models import Shelter, Attribute, Property
......@@ -22,6 +22,26 @@ api_bp = Blueprint('api for shelter', __name__, url_prefix='/api/v0.1')
def tree():
return defaultdict(tree)
def subqueryfactory(model,join=False,filt=False,value=False):
#helper functions
def filters(obj,attrib):
"""recursively construct filtering methods (AND)"""
if len(attrib) == 1:
return obj.filter(attrib[0])
else:
return filters(obj.filter(attrib[len(attrib)-1]), attrib[0:len(attrib)-1])
#construct query
if join and not filt:
return model.query.join(join).subquery()
elif filt and join:
return model.query.join(join).filter(filt.in_(value)).subquery()
elif filt and not join:
return model.query.filter(filt.in_(value)).subquery()
else:
return "error"
@api_bp.route('/', methods=['GET'])
def apimessage():
......@@ -31,47 +51,54 @@ def apimessage():
return jsonify(message)
@api_bp.route('/attributes/<attribute_name>', methods=['GET'])
def getattributes(attribute_name):
def getattributes(attribute_name, safetext=False):
"""Returns available values for a given attribute name, separated by semicolons"""
result= tree()
attributes = Attribute.query.filter(Attribute.name==attribute_name).\
first().associated_values
for attribute in attributes:
if result[attribute_name]:
result[attribute_name] += (";" + attribute.name)
else:
result[attribute_name] = attribute.name
result[attribute_name] = ";".join([attribute.name for attribute in attributes])
return jsonify(result)
@api_bp.route('/shelters', methods=['GET'])
def allshelters():
"""Returns all shelters and their properties"""
result = tree()
shelter_properties = Property.query.all()
attributes = Attribute.query.all()
for shelter_property in shelter_properties:
for attribute in attributes:
if attribute.id == shelter_property.attribute_id:
result[shelter_property.shelter_id][attribute.name] = shelter_property.values[0].name
if request.args:
form = request.args.get('format')
#prop = request.args.getlist('property')
#cat = request.args.getlist('category')
attr = request.args.getlist('attribute')
#val = request.args.getlist('value')
#user = request.args.getlist('user')
subquery = subqueryfactory(Property,Attribute,Attribute.name,attr)
shelter_properties = Property.query.filter(Property.shelter_id==subquery.c.shelter_id).all()
else:
shelter_properties = Property.query.all()
if request.args.get('format') == 'prettytext':
for shelter_property in shelter_properties:
result[shelter_property.shelter_id][shelter_property.attribute.name] = shelter_property.get_values_as_string()
else:
for shelter_property in shelter_properties:
result[shelter_property.shelter_id][shelter_property.attribute.uniqueid] = shelter_property.get_values_as_string()
return jsonify(result)
@api_bp.route('/shelters/<int:shelter_id>', methods=['GET'])
def shelters(shelter_id):
"""Returns specific shelter with its properties"""
result = tree()
shelter_properties = Property.query.filter(Property.shelter_id==shelter_id)
attributes = Attribute.query.all()
for shelter_property in shelter_properties:
for attribute in attributes:
if attribute.id == shelter_property.attribute_id:
result[shelter_property.shelter_id][attribute.name] = shelter_property.values[0].name
print(shelter_property)
result[shelter_property.shelter_id][shelter_property.attribute.name] = shelter_property.get_values_as_string()
return jsonify(result)
@api_bp.route('/shelters/<attribute_name>', methods=['GET'])
......@@ -79,23 +106,18 @@ def shelters(shelter_id):
def attributes(attribute_name, attribute_value=''):
"""Returns all shelters which match a specific attribute name or attribute name + value"""
result = tree()
shelter_properties = Property.query.filter(Property.attribute.has(name=attribute_name))
attributes = Attribute.query.all()
if not attribute_value:
for shelter_property in shelter_properties:
for attribute in attributes:
if attribute.id == shelter_property.attribute_id:
result[shelter_property.shelter_id][attribute.name] = shelter_property.values[0].name
shelter_properties = Property.query.filter(Property.attribute.has(name=attribute_name))
else:
for shelter_property in shelter_properties:
for attribute in attributes:
if attribute.id == shelter_property.attribute_id \
and attribute.name == attribute_name \
and shelter_property.values[0].name == attribute_value:
result[shelter_property.shelter_id][attribute.name] = shelter_property.values[0].name
shelter_properties = Property.query.filter(Property.attribute.has(name=attribute_name), Property.values.any(name=attribute_value))
for shelter_property in shelter_properties:
result[shelter_property.shelter_id][shelter_property.attribute.name] = shelter_property.get_values_as_string()
return jsonify(result)
#shelter_properties = Property.query.join(Property.category).filter(Property.category.has(name="Identification"))
#for shelter_property in shelter_properties:
# result[shelter_property.shelter_id][shelter_property.attribute.name] = shelter_property.get_values_as_string()
# return jsonify(result)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment