Commit f0f2f266 authored by Maarten van der Veen's avatar Maarten van der Veen
parents 50097e17 d0cef508
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -4,16 +4,18 @@ Web Services
Usage of the development API v0.1
---------------------------------
request parameters:
'<attribute>' - pass one or more attribute names, such as '<attribute=Name,Vegetation>'
**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,
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,
**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
......
......@@ -816,4 +816,15 @@ section.map {
.stats .table {
font-size: smaller;
}
\ No newline at end of file
}
.stats h5 {
margin-top:20px;
}
.leaflet-container a {
margin-left:0px !important;
}
.dc-chart text {
fill: black !important;
}
\ No newline at end of file
......@@ -811,4 +811,27 @@ section.map {
display: initial; }
#tabs.tab2 .tabcontent2 {
display: initial; }
display: initial ; }
.stats {
display: inline-block;
}
.stats .table {
font-size: smaller !important;
}
.stats .table tr td {
font-weight: normal !important; ;
}
.stats h5 {
margin-top:20px;
}
.leaflet-container a {
margin-left:0px !important;
}
.dc-chart text {
fill: black !important;
}
ID,Name of shelter,Zone,Country,Associated disaster / Immediate cause,GPS Latitude,GPS Longitude,Climate zone,Year of construction (first completed shelters)
BI024,Adobe block house,Africa,Burundi,conflict related,,30.0083055556,moist tropical,2011
BI025,Plastic sheeting refugee shelter,Africa,Burundi,conflict related,3.3563611111,30.2743333333,moist tropical,2009
BD021,Self Built Hut,Asia,Bangladesh,storm/cyclone,22.6861111111,89.8679166667,moist tropical,2009
HT020,"CHF, Steel Frame Shelter",Americas & Caribbean,Haiti,earthquake,18.427825-72.8627139,-72.5347444,moist tropical,2010
NP022,Luxembourg RC Shelter 2,Asia,Nepal,flood,28.8878888889,80.3958888889,moist tropical,
id,nameofshelter,zone,country,associateddisasterimmediatecause,gpslatitude,gpslongitude,climatezone,yearofconstructionfirstcompletedshelters,constructioncostperunitusd,topography
BI024,Adobe block house,Africa,Burundi,conflict related,,30.0083055556,moist tropical,2011,1000,slope
BI025,Plastic sheeting refugee shelter,Africa,Burundi,conflict related,3.3563611111,30.2743333333,moist tropical,2009,200,flat
BD021,Self Built Hut,Asia Pacific,Bangladesh,storm/cyclone,22.6861111111,89.8679166667,moist tropical,2009,4500,
HT020,"CHF, Steel Frame Shelter",Americas & Caribbean,Haiti,earthquake,18.427825-72.8627139,-72.5347444,moist tropical,2010,3000,
NP022,Luxembourg RC Shelter 2,Asia Pacific,Nepal,flood,28.8878888889,80.3958888889,moist tropical,2009,3000,,
\ No newline at end of file
This diff is collapsed.
......@@ -42,43 +42,38 @@
<h5>Filter by</h5>
<form class="myform">
<label for="f1" class="button button-drop">
<select id="f1">
<label for="climateFilter" class="button button-drop">
<select id="climateFilter">
<option value="" selected="selected">Climate</option>
<option value="" disabled>---</option>
<option value="dry moderate">dry moderate</option>
<option value="dry polar">dry polar</option>
<option value="dry tropical">dry tropical</option>
<option value="moist moderate">moist moderate</option>
<option value="moist polar">moist polar</option>
<option value="moist tropical">moist tropical</option>
</select>
</label>
<label for="f2" class="button button-drop">
<select id="f2">
<label for="zoneFilter" class="button button-drop">
<select id="zoneFilter">
<option value="" selected="selected">Continent</option>
<option value="" disabled>---</option>
</select>
</label>
<label for="f3" class="button button-drop">
<select id="f3">
<label for="disasterFilter" class="button button-drop">
<select id="disasterFilter">
<option value="" selected="selected">Disaster</option>
<option value="" disabled>---</option>
</select>
</label>
<label for="f4" class="button button-drop">
<select id="f4">
<label for="materialFilter" class="button button-drop">
<select id="materialFilter">
<option value="" selected="selected">Material</option>
<option value="" disabled>---</option>
</select>
</label>
<label for="f5" class="button button-drop">
<select id="f5">
<label for="commercialFilter" class="button button-drop">
<select id="commercialFilter">
<option value="" selected="selected">Commercial</option>
<option value="" disabled>---</option>
</select>
</label>
<!-- <a href="" class="button button-light button-drop">Climate</a> -->
<!-- <a href="" class="button button-drop">Continent</a> -->
<!-- <a href="" class="button button-drop">Disaster</a> -->
......@@ -89,14 +84,36 @@
<div class="advanced-btn" onclick="toggleAdvanced()">Advanced Search</div>
<div class="advanced-content">
<label for="f10" class="button button-small button-drop">
<select id="f10">
<label for="soilFilter" class="button button-small button-drop">
<select id="soilFilter">
<option value="" selected="selected">Soil Type</option>
<option value="" disabled>---</option>
</select>
</label>
<label for="shelterTypeFilter" class="button button-small button-drop">
<select id="shelterTypeFilter">
<option value="" selected="selected">Shelter Type</option>
<option value="" disabled>---</option>
</select>
</label>
<label for="countryFilter" class="button button-small button-drop">
<select id="countryFilter">
<option value="" selected="selected">Country</option>
<option value="" disabled>---</option>
</select>
</label>
<!--label for="topographyFilter" class="button button-small button-drop">
<select id="topographyFilter">
<option value="" selected="selected">Topography</option>
<option value="" disabled>---</option>
</select>
</label-->
<label for="isSRURatedFilter" class="button button-small ">
<input id="isSRURatedFilter" type="checkbox" value=""> Rated by SRU
</label>
</div>
</div>
</form>
......@@ -113,40 +130,7 @@
<li class="tab2" onclick="setTab(2)">Statistics</li>
</ul>
<div class="tabcontent tabcontent1">
<div class="shelters">
<div class="shelter">
<div class="image" style="background-image: url('img/shelter.jpg')"></div>
<h4 class="title"><a href="shelter.html">Cordaid, TS Urban</a></h4>
<div class="country">Haiti</div>
<div class="description"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.</p></div>
</div>
<div class="shelter">
<div class="image" style="background-image: url('img/shelter.jpg')"></div>
<h4 class="title"><a href="shelter.html">Cordaid, TS Urban</a></h4>
<div class="country">Haiti</div>
<div class="description"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.</p></div>
</div>
<div class="shelter">
<div class="image" style="background-image: url('img/shelter.jpg')"></div>
<h4 class="title"><a href="shelter.html">Cordaid, TS Urban</a></h4>
<div class="country">Haiti</div>
<div class="description"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.</p></div>
</div>
<div class="shelters" id="shelterList">
</div>
</div>
<div class="tabcontent tabcontent2">
......@@ -164,7 +148,7 @@
class="total-count"></span>
records
|
<a id="all" href="#">Reset All</a>
<a id="all" href="#tabs">Reset All</a>
</small>
</h4>
......@@ -201,13 +185,20 @@
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="col-md-6">
<h5>By Country
<small><a id="country">reset</a></small>
</h5>
<div class="dc-chart" id="chart-bar-country"></div>
</div>
<div class="col-md-6">
<h5>By Topography
<small><a id="topography">reset</a></small>
</h5>
<div class="dc-chart" id="chart-topography"></div>
</div>
</div>
</div>
<div class="col-md-6">
......
......@@ -14,32 +14,45 @@ __revision__ = ""
__copyright__ = ""
__license__ = ""
#from bootstrap import db
from bootstrap import db
from sqlalchemy.sql import func
from flask import Blueprint, jsonify, request
from collections import defaultdict
from web.models import Shelter, Attribute, Property
from web.models import Shelter, Attribute, Property, Value, Association
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):
def queryfactory(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])
#helper functions to construct queries
def filter_or(obj,attrib,val):
"""Construct filtering method (OR)"""
list(val)
if len(val) == 1:
return obj.filter(attrib == val[0])
else:
return filters(obj.filter(attrib[len(attrib)-1]), attrib[0:len(attrib)-1])
return obj.filter(attrib.in_(val))
def filter_and(obj,attrib,val):
"""Construct filtering methods recursively (AND)"""
list(val)
if len(val) == 1:
return obj.filter(attrib == val[0])
else:
return filter_and(obj.filter(attrib == val[len(val)-1]),attrib, val[0:len(val)-1])
#subquery = queryfactory(Property,Attribute,Attribute.name,attr)
#construct query
if join and not filt:
return model.query.join(join).subquery()
return model.query.join(join)
elif filt and join:
return model.query.join(join).filter(filt.in_(value)).subquery()
return filter_or(model.query.join(join),filt,value)
elif filt and not join:
return model.query.filter(filt.in_(value)).subquery()
return filter_or(model.in_(value))
else:
return "error"
......@@ -55,7 +68,7 @@ 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).\
attributes = Attribute.query.filter(Attribute.uniqueid==attribute_name).\
first().associated_values
result[attribute_name] = ";".join([attribute.name for attribute in attributes])
......@@ -66,28 +79,34 @@ def allshelters():
"""Returns all shelters and their properties"""
result = tree()
if request.args:
form = request.args.get('format')
#prop = request.args.getlist('property')
#cat = request.args.getlist('category')
## attribute parameter listening
if request.args.getlist('attribute'):
attr = request.args.getlist('attribute')
#val = request.args.getlist('value')
#user = request.args.getlist('user')
subquery = subqueryfactory(Property,Attribute,Attribute.name,attr)
subquery = queryfactory(Property,Attribute,Attribute.uniqueid,attr).subquery()
shelter_properties = Property.query.filter(Property.shelter_id==subquery.c.shelter_id).all()
else:
shelter_properties = Property.query.all()
shelter_properties = db.session.query(Property.shelter_id,Attribute.name,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)
print(shelter_properties)
## value parameter listening
if request.args.getlist('attribute') and request.args.getlist('value'):
valu = request.args.getlist('value')
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.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()
result[shelter_property.shelter_id][shelter_property.name] = shelter_property.value
return jsonify(result)
......@@ -97,8 +116,8 @@ def shelters(shelter_id):
result = tree()
shelter_properties = Property.query.filter(Property.shelter_id==shelter_id)
for shelter_property in shelter_properties:
print(shelter_property)
result[shelter_property.shelter_id][shelter_property.attribute.name] = shelter_property.get_values_as_string()
result[shelter_property.shelter_id][shelter_property.attribute.uniqueid] = shelter_property.get_values_as_string()
return jsonify(result)
@api_bp.route('/shelters/<attribute_name>', methods=['GET'])
......@@ -107,12 +126,12 @@ def attributes(attribute_name, attribute_value=''):
"""Returns all shelters which match a specific attribute name or attribute name + value"""
result = tree()
if not attribute_value:
shelter_properties = Property.query.filter(Property.attribute.has(name=attribute_name))
shelter_properties = Property.query.filter(Property.attribute.has(uniqueid=attribute_name))
else:
shelter_properties = Property.query.filter(Property.attribute.has(name=attribute_name), Property.values.any(name=attribute_value))
shelter_properties = Property.query.filter(Property.attribute.has(uniqueid=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()
result[shelter_property.shelter_id][shelter_property.attribute.uniqueid] = 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