Commit 3a592af6 authored by Aron Gergely's avatar Aron Gergely

optimized parameter handling and shelter picture serving for API "/shelters" resource

parent ed543e0a
......@@ -44,7 +44,7 @@ def queryfactory(model,join=False,filt=False,value=False):
return obj.filter(attrib == val[0])
else:
return filter_and(obj.filter(attrib == val[len(val)-1]),attrib, val[0:len(val)-1])
# model join filt value
#subquery = queryfactory(Property,Attribute,Attribute.name,attr)
if join and not filt:
......@@ -75,58 +75,67 @@ def getattributes(attribute_name, safetext=False):
return jsonify(result)
@api_bp.route('/shelters', methods=['GET'])
#@api_bp.route('/shelters/<int:shelter_id>', methods=['GET'])
def allshelters():
"""Returns all shelters and their properties"""
result = tree()
## shelter picture query
#shelter_pictures = db.session.query(ShelterPicture.shelter_id, func.string_agg(ShelterPicture.file_name,';').label("filename"), Category.name)\
# .join(Category, Category.id == ShelterPicture.category_id)\
# .group_by(ShelterPicture.shelter_id, Category.name)
shelter_pictures = db.session.query(ShelterPicture.shelter_id, ShelterPicture.file_name.label("filename"), Category.name)\
.join(Category, Category.id == ShelterPicture.category_id)
print(shelter_pictures)
#shelter pictures folder path
picpath = 'data/shelters/pictures'
## attribute parameter listening
if request.args.getlist('attribute'):
attr = request.args.getlist('attribute')
subquery = queryfactory(Property,Attribute,Attribute.uniqueid,attr).subquery()
shelter_properties = Property.query.filter(Property.shelter_id==subquery.c.shelter_id).all()
else:
shelter_properties = db.session.query(Property.shelter_id,Attribute.name,Attribute.uniqueid,func.string_agg(Value.name,';').label("value"))\
querybase = db.session.query(Property.shelter_id,Attribute.name,Attribute.uniqueid,func.string_agg(Value.name,';').label("value"))\
.join(Attribute)\
.join(Association,Property.id==Association.property_id)\
.join(Value, Association.value_id==Value.id)\
.group_by(Property.shelter_id, Attribute.name,Attribute.uniqueid)
.group_by(Property.shelter_id, Attribute.name, Attribute.uniqueid)
## value parameter listening
if request.args.getlist('attribute') and request.args.getlist('value'):
valu = request.args.getlist('value')
picquerybase = db.session.query(ShelterPicture.shelter_id, ShelterPicture.file_name.label("filename"), Category.name)\
.join(Category, Category.id == ShelterPicture.category_id)
##queries if no request arguments
shelter_properties = querybase
shelter_pictures = picquerybase
if request.args.getlist('attribute'):
attribute = request.args.getlist('attribute')
subquery = db.session.query(Property.shelter_id)\
.join(Attribute, Attribute.id==Property.attribute_id)\
.filter(Attribute.uniqueid.in_(attribute))\
.group_by(Property.shelter_id)
shelter_properties = querybase.filter(subquery.subquery().c.shelter_id==Property.shelter_id)
shelter_pictures = picquerybase.filter(subquery.subquery().c.shelter_id==ShelterPicture.shelter_id)
if request.args.getlist('value'):
value = request.args.getlist('value')
if not request.args.getlist('attribute'):
subquery = db.session.query(Property.shelter_id)\
.join(Attribute, Attribute.id==Property.attribute_id)\
.filter(Property.values.any(Value.name.in_(value)))\
.group_by(Property.shelter_id)
else:
subquery = subquery.filter(Property.values.any(Value.name.in_(value)))
shelter_properties = querybase.filter(subquery.subquery().c.shelter_id==Property.shelter_id)
shelter_pictures = picquerybase.filter(subquery.subquery().c.shelter_id==ShelterPicture.shelter_id)
#print(shelter_properties)
#print(shelter_pictures)
subquery = Property.query.filter(Property.attribute.has(Attribute.uniqueid.in_(attr)), Property.values.any(Value.name.in_(valu))).subquery()
shelter_properties = Property.query.filter(Property.shelter_id==subquery.c.shelter_id).all()
## format parameter listening and populate defaultict
if request.args.get('format') == 'prettytext':
for shelter_property in shelter_properties:
result[shelter_property.shelter_id][shelter_property.name] = shelter_property.value
for picture in shelter_pictures:
if not result[picture.shelter_id]["shelterpicture"][picture.name]:
result[picture.shelter_id]["shelterpicture"][picture.name] = ["{}/{}/{}".format(picpath, result[picture.shelter_id]["ID"], picture.filename)]
else:
result[picture.shelter_id]["shelterpicture"][picture.name].append("{}/{}/{}".format(picpath, result[picture.shelter_id]["ID"], picture.filename))
else:
for shelter_property in shelter_properties:
result[shelter_property.shelter_id][shelter_property.uniqueid] = shelter_property.value
for picture in shelter_pictures:
if not result[picture.shelter_id]["shelterpicture"][picture.name]:
result[picture.shelter_id]["shelterpicture"][picture.name] = ["{}/{}/{}".format(picpath, result[picture.shelter_id]["id"], picture.filename)]
else:
result[picture.shelter_id]["shelterpicture"][picture.name].append("{}/{}/{}".format(picpath, result[picture.shelter_id]["id"], picture.filename))
for picture in shelter_pictures:
if not result[picture.shelter_id]["shelterpicture"][picture.name]:
result[picture.shelter_id]["shelterpicture"][picture.name] = ["{}/{}/{}".format(picpath, result[picture.shelter_id]["ID"], picture.filename)]
else:
result[picture.shelter_id]["shelterpicture"][picture.name].append("{}/{}/{}".format(picpath, result[picture.shelter_id]["ID"], picture.filename))
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