Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
where
Commits
ae1e43f5
Commit
ae1e43f5
authored
Feb 18, 2020
by
Zach Perkins
Browse files
waddup REST
parent
149ce9f2
Changes
2
Hide whitespace changes
Inline
Side-by-side
where/app.py
View file @
ae1e43f5
from
flask
import
Flask
,
redirect
,
jsonify
,
abort
,
request
from
flask
import
Flask
,
redirect
,
jsonify
,
abort
,
request
,
url_for
,
Response
,
make_response
from
werkzeug.datastructures
import
Headers
from
where.model.field_types
import
FieldType
from
where.model.sa
import
Category
,
Point
,
Field
,
with_session
...
...
@@ -121,20 +122,40 @@ def get_point(session, id):
abort
(
404
)
@
app
.
route
(
'/add-point'
)
@
app
.
route
(
'/add-point'
,
methods
=
[
'POST'
])
@
with_session
def
add_point
(
session
):
allowed_params
=
{
'name'
,
'lat'
,
'lon'
,
'attributes'
,
'category_id'
,
'parent_id'
}
data
=
request
.
get_json
()
data
=
{
key
:
val
for
key
,
val
in
data
.
items
()
if
key
in
allowed_params
}
# TODO: For some reason point.category is NULL when we do validation, even though the category ID is present
# this is causing an exception whenever a non-null attributes object is passed
point
=
Point
(
category_id
=
data
.
pop
(
'category_id'
))
for
key
,
val
in
data
.
items
():
setattr
(
point
,
key
,
val
)
session
.
add
(
point
)
session
.
commit
()
response
=
make_response
(
jsonify
(
point
.
as_json
()),
201
)
response
.
headers
[
'Location'
]
=
url_for
(
'get_point'
,
id
=
point
.
id
)
return
response
@
app
.
route
(
'/point'
,
methods
=
[
'GET'
])
@
with_session
def
search_points
(
session
):
q
=
session
.
query
(
Point
)
if
'category'
in
request
.
args
:
q
=
q
.
filter
(
Point
.
category_id
==
request
.
args
.
get
(
'category'
))
if
'category
_id
'
in
request
.
args
:
q
=
q
.
filter
(
Point
.
category_id
==
request
.
args
.
get
(
'category
_id
'
))
if
'parent'
in
request
.
args
:
q
=
q
.
filter
(
Point
.
parent_id
==
request
.
args
.
get
(
'parent'
))
if
'parent
_id
'
in
request
.
args
:
q
=
q
.
filter
(
Point
.
parent_id
==
request
.
args
.
get
(
'parent
_id
'
))
return
jsonify
(
list
(
map
(
lambda
p
:
p
.
as_json
(),
q
.
limit
(
100
).
all
())))
if
__name__
==
'__main__'
:
app
.
run
()
\ No newline at end of file
where/model/sa.py
View file @
ae1e43f5
...
...
@@ -59,7 +59,9 @@ class Point(Base):
def
validate_data
(
self
,
_
,
data
):
if
data
is
None
:
return
fields
=
self
.
category
.
fields
for
key
in
data
:
# Find Field object that corresponds to this key
for
field
in
fields
:
...
...
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