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
c0a86139
Commit
c0a86139
authored
Feb 20, 2020
by
Zach Perkins
Browse files
Fixed a thing and reworked point creation
parent
61d2aaed
Changes
2
Hide whitespace changes
Inline
Side-by-side
where/app.py
View file @
c0a86139
...
...
@@ -5,6 +5,14 @@ from where.model.field_types import FieldType
app
=
Flask
(
__name__
)
def
create_resource
(
session
,
model_cls
,
data
,
get_function
):
model
=
model_cls
(
**
data
)
session
.
add
(
model
)
session
.
commit
()
response
=
make_response
(
jsonify
(
model
.
as_json
()),
201
)
response
.
headers
[
'Location'
]
=
url_for
(
get_function
,
id
=
model
.
id
)
return
response
@
app
.
route
(
'/'
)
def
index
():
...
...
@@ -122,23 +130,11 @@ def get_point(session, id):
@
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
data
[
'category'
]
=
session
.
query
(
Category
).
get
(
data
[
'category'
])
data
[
'parent_id'
]
=
data
.
pop
(
'parent'
,
None
)
return
create_resource
(
session
,
Point
,
data
,
'get_point'
)
@
app
.
route
(
'/point'
,
methods
=
[
'GET'
])
@
with_session
...
...
where/model/__init__.py
View file @
c0a86139
...
...
@@ -61,12 +61,20 @@ class Point(Base):
parent
=
relationship
(
'Point'
,
remote_side
=
[
id
])
children
=
relationship
(
'Point'
)
def
__init__
(
self
,
**
kwargs
):
# Need to load category first or else attribute validation will fail
if
'category'
in
kwargs
:
self
.
category
=
kwargs
.
pop
(
'category'
)
super
(
Point
,
self
).
__init__
(
**
kwargs
)
@
validates
(
'attributes'
)
def
validate_data
(
self
,
_
,
data
):
if
data
is
None
:
return
fields
=
self
.
category
.
fields
print
(
fields
[
0
].
slug
)
for
key
in
data
:
# Find Field object that corresponds to this key
...
...
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