Commit 3d63bffe authored by Cédric Bonhomme's avatar Cédric Bonhomme
Browse files
parents 44b38b3a befc7c6c
......@@ -44,6 +44,8 @@ app.config['SQLALCHEMY_DATABASE_URI'] = conf.SQLALCHEMY_DATABASE_URI
app.config['PUBLIC_PATH'] = conf.PUBLIC_PATH
app.config['JSON_AS_ASCII'] = False
db = SQLAlchemy(app)
# Create the Flask-Restless API manager.
......@@ -71,4 +73,4 @@ def populate_g():
from flask import g
g.db = db
g.app = app
g.babel = babel
\ No newline at end of file
g.babel = babel
......@@ -9,6 +9,17 @@ import conf
from web.models import Shelter, Category, ShelterPicture
from bootstrap import db
#! /usr/bin/python
#-*- coding:utf-8 -*
import os
import glob
import shutil
import conf
from web.models import Shelter, Category, ShelterPicture
from bootstrap import db
def import_shelters_pictures(folder):
shelters = Shelter.query.all()
......@@ -17,8 +28,10 @@ def import_shelters_pictures(folder):
for picture in glob.glob(folder + shelter_rid + '/**.jpg'):
picture_name = os.path.basename(picture)
try:
category_name = picture_name.split('_')[1]
picture_subject = os.path.splitext(picture_name.split('_')[2])[0]
except:
continue
......@@ -26,9 +39,16 @@ def import_shelters_pictures(folder):
Category.parent_id!=None).first()
if category:
new_picture = ShelterPicture(file_name=picture_name,
shelter_id=shelter.id,
category_id=category.id)
if picture_subject.lower() == 'facade':
new_picture = ShelterPicture(file_name=picture_name,
shelter_id=shelter.id,
category_id=category.id,
is_main_picture=True)
else:
new_picture = ShelterPicture(file_name=picture_name,
shelter_id=shelter.id,
category_id=category.id)
db.session.add(new_picture)
db.session.commit()
......
......@@ -25,6 +25,7 @@ class ShelterPicture(db.Model):
"""
id = db.Column(db.Integer, primary_key=True)
file_name = db.Column(db.String(), default='')
is_main_picture = db.Column(db.Boolean(), default=False)
# relationship
shelter_id = db.Column(db.Integer, db.ForeignKey('shelter.id'),
......
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
......@@ -250,5 +251,4 @@
{ "type": "Feature", "properties": { "fips": "SV", "iso2": "SJ", "iso3": "SJM", "un": 744, "name": "Svalbard", "area": 0, "pop2005": 0, "region": 150, "subregion": 154, "lon": 18.374000, "lat": 78.830000, "cartodb_id": 240, "created_at": "2014\/08\/28 00:00:00.000", "updated_at": "2014\/08\/28 00:00:00.000", "other_name": "Svalbard and Jan Mayen" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -8.290895, 70.974154 ], [ -9.120058, 70.857759 ], [ -7.927002, 71.15082 ], [ -8.290895, 70.974154 ] ] ], [ [ [ 19.18194, 74.359713 ], [ 18.791945, 74.481661 ], [ 19.299723, 74.474993 ], [ 19.18194, 74.359713 ] ] ], [ [ [ 25.016111, 76.466387 ], [ 24.936384, 76.44887 ], [ 25.58222, 76.706652 ], [ 25.016111, 76.466387 ] ] ], [ [ [ 23.285555, 77.264711 ], [ 23.135275, 77.235262 ], [ 23.036386, 77.259996 ], [ 23.285555, 77.264711 ] ] ], [ [ [ 23.336111, 78.197481 ], [ 23.059721, 78.0161 ], [ 24.910276, 77.749712 ], [ 22.641108, 77.253054 ], [ 22.407499, 77.420824 ], [ 22.778334, 77.547762 ], [ 20.858889, 77.462496 ], [ 21.644999, 77.912493 ], [ 20.903051, 78.114992 ], [ 23.336111, 78.197481 ] ] ], [ [ [ 21.501665, 78.565538 ], [ 22.028887, 78.580828 ], [ 22.271944, 78.263613 ], [ 20.67111, 78.187487 ], [ 20.12472, 78.472216 ], [ 21.501665, 78.565538 ] ] ], [ [ [ 26.847498, 78.707491 ], [ 26.396387, 78.77083 ], [ 27.0175, 78.693041 ], [ 26.847498, 78.707491 ] ] ], [ [ [ 11.271944, 78.606936 ], [ 12.16361, 78.199709 ], [ 10.488611, 78.896379 ], [ 11.271944, 78.606936 ] ] ], [ [ [ 29.030554, 78.916094 ], [ 29.708052, 78.896379 ], [ 28.01972, 78.867479 ], [ 29.030554, 78.916094 ] ] ], [ [ [ 30.20944, 78.976381 ], [ 30.066942, 79.001101 ], [ 30.336111, 78.988878 ], [ 30.20944, 78.976381 ] ] ], [ [ [ 21.226107, 79.037493 ], [ 20.982496, 79.014162 ], [ 20.923609, 79.029985 ], [ 21.226107, 79.037493 ] ] ], [ [ [ 20.833055, 79.054155 ], [ 20.04611, 79.037493 ], [ 20.19972, 79.124697 ], [ 20.833055, 79.054155 ] ] ], [ [ [ 20.007502, 79.242205 ], [ 19.954718, 79.226656 ], [ 20.168886, 79.319155 ], [ 20.007502, 79.242205 ] ] ], [ [ [ 20.109442, 79.365816 ], [ 20.180277, 79.338884 ], [ 19.628332, 79.388323 ], [ 20.109442, 79.365816 ] ] ], [ [ [ 10.922777, 79.651094 ], [ 10.712221, 79.71361 ], [ 11.076109, 79.662767 ], [ 10.922777, 79.651094 ] ] ], [ [ [ 11.000555, 79.740267 ], [ 10.639166, 79.750002 ], [ 10.756109, 79.783327 ], [ 11.000555, 79.740267 ] ] ], [ [ [ 16.819166, 79.87221 ], [ 18.098333, 79.720263 ], [ 17.635553, 79.372484 ], [ 18.356943, 79.628313 ], [ 18.920553, 79.164156 ], [ 21.549166, 78.767763 ], [ 18.965551, 78.456102 ], [ 19.092222, 78.095537 ], [ 18.41083, 78.021654 ], [ 18.292776, 77.50638 ], [ 17.738329, 77.471102 ], [ 16.919165, 76.799715 ], [ 17.198332, 76.694704 ], [ 16.612776, 76.570543 ], [ 15.498053, 76.879976 ], [ 16.51972, 76.998598 ], [ 14.467222, 77.171648 ], [ 13.914167, 77.527773 ], [ 16.223051, 77.434984 ], [ 14.739164, 77.658602 ], [ 17.006109, 77.931368 ], [ 13.941942, 77.716661 ], [ 13.588888, 78.049425 ], [ 17.301664, 78.420259 ], [ 16.328886, 78.452211 ], [ 16.83111, 78.671923 ], [ 15.464167, 78.451662 ], [ 15.193331, 78.588045 ], [ 15.379999, 78.844713 ], [ 14.385557, 78.497759 ], [ 14.726389, 78.383883 ], [ 13.006666, 78.197481 ], [ 12.360556, 78.480272 ], [ 13.19861, 78.540819 ], [ 11.333334, 78.960543 ], [ 12.505556, 78.908037 ], [ 11.755278, 79.075823 ], [ 12.113054, 79.295824 ], [ 11.236387, 79.09305 ], [ 10.676943, 79.544985 ], [ 13.82472, 79.875261 ], [ 12.447779, 79.566378 ], [ 13.484442, 79.580828 ], [ 13.257223, 79.471376 ], [ 14.058889, 79.260271 ], [ 13.886946, 79.541368 ], [ 14.584997, 79.804155 ], [ 16.449999, 78.903872 ], [ 15.638056, 79.827211 ], [ 16.307775, 80.062761 ], [ 16.819166, 79.87221 ] ] ], [ [ [ 27.837778, 80.110262 ], [ 27.674166, 80.122759 ], [ 28.074442, 80.144152 ], [ 27.837778, 80.110262 ] ] ], [ [ [ 36.849161, 80.139986 ], [ 36.590548, 80.154985 ], [ 36.760279, 80.160814 ], [ 36.849161, 80.139986 ] ] ], [ [ [ 32.366388, 80.094439 ], [ 31.449442, 80.085817 ], [ 33.637499, 80.21332 ], [ 32.366388, 80.094439 ] ] ], [ [ [ 18.553331, 80.245531 ], [ 18.119444, 80.284716 ], [ 18.758612, 80.301928 ], [ 18.553331, 80.245531 ] ] ], [ [ [ 24.335833, 80.384157 ], [ 24.128611, 80.393053 ], [ 24.196943, 80.454165 ], [ 24.335833, 80.384157 ] ] ], [ [ [ 19.933332, 80.47748 ], [ 21.860556, 80.267763 ], [ 21.598608, 80.118319 ], [ 22.226385, 79.979158 ], [ 22.578611, 80.298601 ], [ 22.361109, 80.410265 ], [ 22.886942, 80.490267 ], [ 23.36083, 80.42943 ], [ 23.101389, 80.120821 ], [ 24.836386, 80.350817 ], [ 27.244997, 79.904985 ], [ 23.511667, 79.178865 ], [ 19.635553, 79.603868 ], [ 22.312498, 79.796938 ], [ 18.771387, 79.717485 ], [ 18.154165, 79.90999 ], [ 18.809443, 79.994707 ], [ 17.783056, 80.127764 ], [ 19.340555, 80.086382 ], [ 19.584166, 80.151384 ], [ 18.979998, 80.336657 ], [ 19.837778, 80.213884 ], [ 19.463331, 80.454714 ], [ 19.933332, 80.47748 ] ] ], [ [ [ 24.265001, 80.471102 ], [ 23.97444, 80.490541 ], [ 24.174997, 80.509157 ], [ 24.265001, 80.471102 ] ] ], [ [ [ 19.933332, 80.47748 ], [ 19.844721, 80.503054 ], [ 20.130278, 80.512774 ], [ 19.933332, 80.47748 ] ] ], [ [ [ 20.753611, 80.62137 ], [ 20.499163, 80.657763 ], [ 20.793333, 80.647493 ], [ 20.753611, 80.62137 ] ] ], [ [ [ 21.330278, 80.700274 ], [ 21.365, 80.678865 ], [ 21.026388, 80.681658 ], [ 21.330278, 80.700274 ] ] ], [ [ [ 20.819166, 80.719149 ], [ 20.518332, 80.759996 ], [ 21.006666, 80.701387 ], [ 20.819166, 80.719149 ] ] ] ] } },
{ "type": "Feature", "properties": { "fips": "SX", "iso2": "GS", "iso3": "SGS", "un": 239, "name": "South Georgia South Sandwich Islands", "area": 0, "pop2005": 0, "region": 0, "subregion": 0, "lon": -36.891000, "lat": -54.209000, "cartodb_id": 245, "created_at": "2014\/08\/28 00:00:00.000", "updated_at": "2014\/08\/28 00:00:00.000", "other_name": "South Georgia and the South Sandwich Islands" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -27.325836, -59.427223 ], [ -27.298058, -59.473061 ], [ -27.412502, -59.434448 ], [ -27.325836, -59.427223 ] ] ], [ [ [ -26.582779, -59.070007 ], [ -26.665001, -59.025841 ], [ -26.501114, -59.036949 ], [ -26.582779, -59.070007 ] ] ], [ [ [ -26.246391, -58.407501 ], [ -26.24889, -58.498611 ], [ -26.459167, -58.427223 ], [ -26.246391, -58.407501 ] ] ], [ [ [ -26.412781, -57.80806 ], [ -26.512222, -57.771118 ], [ -26.443336, -57.743896 ], [ -26.412781, -57.80806 ] ] ], [ [ [ -26.660835, -57.086395 ], [ -26.728889, -57.062782 ], [ -26.675835, -57.056396 ], [ -26.660835, -57.086395 ] ] ], [ [ [ -27.108612, -56.717506 ], [ -27.209724, -56.693611 ], [ -27.129448, -56.682228 ], [ -27.108612, -56.717506 ] ] ], [ [ [ -27.563892, -56.320557 ], [ -27.600002, -56.281113 ], [ -27.549446, -56.27417 ], [ -27.563892, -56.320557 ] ] ], [ [ [ -37.038612, -54.511673 ], [ -37.083618, -54.512779 ], [ -37.105835, -54.471115 ], [ -37.038612, -54.511673 ] ] ], [ [ [ -37.495003, -54.010559 ], [ -36.288063, -54.266396 ], [ -35.782501, -54.765556 ], [ -36.115837, -54.887222 ], [ -37.411392, -54.268333 ], [ -37.239449, -54.147781 ], [ -38.033058, -54.048889 ], [ -37.495003, -54.010559 ] ] ] ] } }
]
}
}
\ No newline at end of file
function create_shelter (name_of_shelter, country_value_id, country_name) {
function create_shelter (name_of_shelter, country_value_id, country_name, callback) {
new_shelter_dict = {} // the id of the shelter's owner is controlled by a POST preprocessor function
$.ajax({
type: 'POST',
......@@ -99,12 +99,16 @@ function create_shelter (name_of_shelter, country_value_id, country_name) {
dataType: "json",
data: JSON.stringify(new_property),
success: function (result) {
console.log(result);
window.location = '/shelter/' + new_shelter.id + '/general-Information';
if(callback != null) {
callback(true, new_shelter.id);
}
//window.location = '/shelter/' + new_shelter.id + '/general-Information';
},
error: function(XMLHttpRequest, textStatus, errorThrown){
console.log(errorThrown);
// invoke the callback function here
if(callback != null) {
callback(false, errorThrown);
}
}
});
},
......
{% extends "layout.html" %}
{% block head %}
{{ super() }}
<script src="{{ url_for('static', filename = 'lib/leaflet/leaflet.js') }}"></script>
<script src="{{ url_for('static', filename='lib/leaflet-geosearch/src/js/l.control.geosearch.js') }}"></script>
<script src="{{ url_for('static', filename='lib/leaflet-geosearch/src/js/l.geosearch.provider.google.js') }}"></script>
<script src="{{ url_for('static', filename='lib/leaflet-geocoder-mapzen/src/leaflet-geocoder-mapzen.js') }}"></script>
<script src="{{ url_for('static', filename='lib/Leaflet.label/dist/leaflet.label.js') }}"></script>
<link href="{{ url_for('static', filename='lib/leaflet/leaflet.css') }}" rel="stylesheet" media="screen" />
<link rel="stylesheet" href="{{ url_for('static', filename='lib/leaflet-geocoder-mapzen/src/leaflet-geocoder-mapzen.css') }}" />
<link rel="stylesheet" href="{{ url_for('static', filename='lib/Leaflet.label/dist/leaflet.label.css') }}" />
{% endblock %}
{% block content %}
<section class="contribute">
<div class="content">
......@@ -33,26 +47,38 @@
<div class="content page1" id="modalcontent">
<div class="page page1">
<form id="createShelterForm" action="#" class="navbar-form navbar-left" method="GET" name="save" class="myform">
<div class="mylabel">Title of your project:</div>
<div class="myinput"><input type="text" name="name" placeholder="Name" required="required" /></div>
<div class="mylabel">Country:</div>
<div class="mylabel">Title of your project:</div>
<div class="myinput"><input type="text" name="name" placeholder="Name" required="required" /></div>
<div class="mylabel">Country:</div>
<label for="f1" class="button button-drop">
<select id="countrySelect" name="country" data-live-search="true"></select>
</label>
</form>
<!-- <a href="" class="button button-light button-drop button-stretch">Select</a> -->
<div class="buttons">
<div onclick="modalClose()" class="button button-light"><i class="fa fa-times"></i><span class="text">Cancel</span></div>
<!--<button type="submit" id="createShelter" class="btn btn-default form-control" value="Create">Create</button> -->
<div onclick="next()" class="button button-light right"><i class="fa fa-arrow-right"></i><span class="text">Next</span></div>
</div>
<!-- <a href="" class="button button-light button-drop button-stretch">Select</a> -->
<div class="buttons">
<div onclick="modalClose()" class="button button-light"><i class="fa fa-times"></i><span class="text">Cancel</span></div>
<!--<button type="submit" id="createShelter" class="btn btn-default form-control" value="Create">Create</button> -->
<div id="createShelter" class="button button-light right"><i class="fa fa-arrow-right"></i><span class="text">Next</span></div>
</div>
</form>
</div>
<div class="page page2">
<div class="mylabel">Province / District / Region:</div>
<div class="myinput"><input type="" name="" placeholder="Province / District / Region.." /></div>
<div class="myinput"><input class="free-text-attribute"
property-id=""
attribute-id="6"
category-id="2"
value-id=""
value=""
type=""
name=""
placeholder="Province / District / Region.." /></div>
<div class="mylabel">City / Village:</div>
<div class="myinput"><input type="" name="" placeholder="City / Village.." /></div>
<div id="locationpicker" style="width:100%; height:400px;"></div>
<div class="mylabel">Image</div>
<!-- <form class="myimgupload" enctype="multipart/form-data"> -->
<!-- <input type="file" style="visibility:hidden;"> -->
......@@ -91,6 +117,8 @@
</div>
<script>
var shelter_id;
$(document).ready(function() {
$("#newShelter").click(function(evt) {
var filters = [{"name":"name","op":"eq","val":"Country"}];
......@@ -114,11 +142,9 @@
});
//Attach a submit handler to the form
$( "#createShelterForm" ).submit(function( event ) {
// Stop form from submitting normally
event.preventDefault();
$('#createShelter').on( "click", function() {
// Get some values from elements on the page:
var $form = $(this);
var $form = $('#createShelterForm');
var values = {};
form_complete = true;
$.each($form.serializeArray(), function(i, field) {
......@@ -134,13 +160,180 @@
name_of_shelter = values["name"]
country_value_id = values["country"];
country_name = $('#countrySelect option:selected').text()
create_shelter(name_of_shelter, country_value_id, country_name);
create_shelter(name_of_shelter, country_value_id, country_name, function (success, result){
if(!success){
console.log(result);
} else {
shelter_id = result;
// initiate_map
initiateLocationPicker();
}
});
} else {
alert("Please fill all the fields of the form.");
}
});
});
</script>
<script type="text/javascript">
function findCountry(data, country){
var countryData = {};
$.each(data.features, function (key, val) {
if (val.properties.other_name === country) {
countryData.lat = val.properties.lat;
countryData.lon = val.properties.lon;
countryData.polygon = val;
//if found, break out of loop
return false;
}
});
//return object (or empty object)
return countryData;
};
function initiateLocationPicker (){
//Query nomatimim for lat lon for country
var country = $('#countrySelect option:selected').text();
$.getJSON("{{ url_for('static', filename='data/countries.geojson') }}", function(data) {
var countryData = findCountry(data, country);
if(jQuery.isEmptyObject(countryData)){
//country was not found
//TODO: handle error
return;
}
// Initiate leaflet map
var locationpicker = L.map('locationpicker');
// Add OSM base layer
L.tileLayer('http://a.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png').addTo(locationpicker);
// Add country polygon to map and fit map to bounds
var countryLayer = L.geoJson(countryData.polygon).addTo(locationpicker);
var bounds = countryLayer.getBounds();
locationpicker.fitBounds(bounds);
// Add Mapzen geocoder
//L.control.geocoder('search-Us5vhhe').addTo(locationpicker);
// add marker position to centroid of country
/*var marker = L.marker([countryData.lat, countryData.lon], {draggable:'true'}).bindLabel('This is where I have built this shelter');
marker.addTo(locationpicker);
marker.on('dragend', function(event){
var position = event.target.getLatLng();
console.log(position);
marker.setLatLng(position,{draggable:'true'}).bindPopup(position).update();
});
locationpicker.on('click', function(e) {
var location = e.latlng;
marker.setLatLng(location).update();
});*/
// go to next modal
next();
});
};
</script>
<script type="text/javascript" class="source">
$(document).ready(function() {
var dateSupported = (function() {
var el = document.createElement('input'),
invalidVal = 'foo'; // Any value that is not a date
el.setAttribute('type','date');
el.setAttribute('value', invalidVal);
// A supported browser will modify this if it is a true date field
return el.value !== invalidVal;
}());
if (!dateSupported) {
$( ".datepicker" ).datepicker({dateFormat: 'dd/mm/yyyy'});
}
$('.select-attribute').change(function(evt) {
property_id = $(evt.target).attr("property-id");
id_of_values = $(evt.target).val();
if (typeof id_of_values === 'string') {
tmp = []
tmp.push(id_of_values)
id_of_values = tmp
}
if (property_id==""){
category_id = $(evt.target).attr("category-id");
attribute_id = $(evt.target).attr("attribute-id");
new_property(shelter_id,
category_id, attribute_id, id_of_values,
$(evt.target))
} else {
update_property(property_id, id_of_values)
}
});
$('.free-text-attribute').change(function(evt) {
value = $(evt.target).val();
if ($(evt.target).context.type == "checkbox") {
if (value == "on" || value == "1") {
value = "0";
} else {
value = "1";
}
}
value_id = $(evt.target).attr("value-id");
if (value_id != "None") {
update_free_text_value(value_id, value);
} else {
category_id = $(evt.target).attr("category-id");
attribute_id = $(evt.target).attr("attribute-id");
new_free_text_property(shelter_id, category_id,
attribute_id, value, $(evt.target));
}
});
$('.add-value').click(function(evt) {
attribute_id = $(evt.target).attr('attribute-id');
$('#addValueDialog').attr('attribute-id', attribute_id);
$('#addValueDialog').modal({backdrop:'static', keyboard:false});
})
$('#add-new-value').click(function(evt) {
new_value = {
attribute_id: $('#addValueDialog').attr('attribute-id'),
name: $('#new-value-input').val()
}
$.ajax({
type: 'POST',
url: '/api/value',
contentType: "application/json",
dataType: "json",
data: JSON.stringify(new_value),
success: function (result) {
$('#select'+attribute_id)
.append($("<option></option>")
.attr("value", result.id)
.text(result.name));
$('.selectpicker').selectpicker('refresh');
},
error: function(XMLHttpRequest, textStatus, errorThrown){
console.log(errorThrown);
}
});
})
});
</script>
<script type="text/javascript">
var cnt = document.getElementById("modalcontent")
......
......@@ -22,51 +22,24 @@ from collections import defaultdict
from web.models import Shelter, Attribute, Property, Value, Association, ShelterPicture, Category, Tsvector
apiv02_bp = Blueprint('development api v0.2', __name__, url_prefix='/api/v0.2')
def tree():
return defaultdict(tree)
def queryfactory(model,join=False,filt=False,value=False):
#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 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])
if join and not filt:
return model.query.join(join)
elif filt and join:
return filter_or(model.query.join(join),filt,value)
elif filt and not join:
return filter_or(model.in_(value))
else:
return "error"
@apiv02_bp.route('/', methods=['GET'])
def apimessage():
message = tree()
message["API version"] = 0.2
message["Message"] = "This is the development API"
return jsonify(message)
@apiv02_bp.route('/worldmap', methods=['GET'])
def worldmap():
"""Returns a world map in GeoJSON"""
with app.open_resource('static/data/countries.geojson') as f:
data = json.load(f)
return json.dumps(data)
data = json.load(f, encoding='utf-8')
return json.dumps(data, encoding='utf-8')
#return app.send_static_file('data/world_borders.geojson')
@apiv02_bp.route('/attributes/<attribute_name>', methods=['GET'])
......@@ -99,7 +72,7 @@ def allshelters(shelter_id=None):
.join(Value, Association.value_id==Value.id)\
.group_by(Property.shelter_id, Supercategory.name, Category.name, Attribute.name, Attribute.uniqueid)
picquerybase = db.session.query(ShelterPicture.shelter_id, ShelterPicture.file_name.label("filename"), Category.name)\
picquerybase = db.session.query(ShelterPicture.shelter_id, ShelterPicture.file_name.label("filename"), ShelterPicture.is_main_picture, Category.name)\
.join(Category, Category.id == ShelterPicture.category_id)
##queries if no request arguments
......@@ -148,7 +121,9 @@ def allshelters(shelter_id=None):
result[shelter_property.shelter_id][shelter_property.supercategory_name]["Attributes"][shelter_property.name] = shelter_property.value
for picture in shelter_pictures:
if not result[picture.shelter_id][picture.name]["Pictures"]:
if picture.is_main_picture == True:
result[picture.shelter_id]["Identification"]["Cover"] = ["{}/{}/{}".format(picpath, result[picture.shelter_id]["Identification"]["Attributes"]["ID"], picture.filename)]
elif not result[picture.shelter_id][picture.name]["Pictures"]:
result[picture.shelter_id][picture.name]["Pictures"] = ["{}/{}/{}".format(picpath, result[picture.shelter_id]["Identification"]["Attributes"]["ID"], picture.filename)]
else:
result[picture.shelter_id][picture.name]["Pictures"].append("{}/{}/{}".format(picpath, result[picture.shelter_id]["Identification"]["Attributes"]["ID"], picture.filename))
......@@ -158,26 +133,12 @@ def allshelters(shelter_id=None):
result[shelter_property.shelter_id][shelter_property.supercategory_name]["Attributes"][shelter_property.uniqueid] = shelter_property.value
for picture in shelter_pictures:
if not result[picture.shelter_id][picture.name]["Pictures"]:
if picture.is_main_picture == True:
result[picture.shelter_id]["Identification"]["Cover"] = ["{}/{}/{}".format(picpath, result[picture.shelter_id]["Identification"]["Attributes"]["id"], picture.filename)]
elif not result[picture.shelter_id][picture.name]["Pictures"]:
result[picture.shelter_id][picture.name]["Pictures"] = ["{}/{}/{}".format(picpath, result[picture.shelter_id]["Identification"]["Attributes"]["id"], picture.filename)]
else:
result[picture.shelter_id][picture.name]["Pictures"].append("{}/{}/{}".format(picpath, result[picture.shelter_id]["Identification"]["Attributes"]["id"], picture.filename))
return jsonify(result)
@apiv02_bp.route('/shelters/<attribute_name>', methods=['GET'])
@apiv02_bp.route('/shelters/<attribute_name>/<attribute_value>', methods=['GET'])
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(uniqueid=attribute_name))
else:
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.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