Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Shelter Database
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
CHARISM
Shelter Database
Commits
3d63bffe
Commit
3d63bffe
authored
Aug 24, 2016
by
Cédric Bonhomme
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://github.com/rodekruis/shelter-database
parents
44b38b3a
befc7c6c
Changes
7
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
257 additions
and
76 deletions
+257
-76
src/bootstrap.py
src/bootstrap.py
+3
-1
src/scripts/import_shelters_pictures.py
src/scripts/import_shelters_pictures.py
+23
-3
src/web/models/shelter_picture.py
src/web/models/shelter_picture.py
+1
-0
src/web/static/data/countries.geojson
src/web/static/data/countries.geojson
+2
-2
src/web/static/js/create-shelter.js
src/web/static/js/create-shelter.js
+9
-5
src/web/templates/contribute.html
src/web/templates/contribute.html
+209
-16
src/web/views/shelterapiv02.py
src/web/views/shelterapiv02.py
+10
-49
No files found.
src/bootstrap.py
View file @
3d63bffe
...
...
@@ -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
src/scripts/import_shelters_pictures.py
View file @
3d63bffe
...
...
@@ -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
()
...
...
src/web/models/shelter_picture.py
View file @
3d63bffe
...
...
@@ -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'
),
...
...
src/web/static/data/countries.geojson
View file @
3d63bffe
{
"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
src/web/static/js/create-shelter.js
View file @
3d63bffe
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
);
}
}
});
},
...
...
src/web/templates/contribute.html
View file @
3d63bffe
This diff is collapsed.
Click to expand it.
src/web/views/shelterapiv02.py
View file @
3d63bffe
...
...
@@ -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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment