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
8b8ab285
Commit
8b8ab285
authored
Feb 23, 2020
by
Zach Perkins
Browse files
added some basic argument validation to spice things up
parent
8f3ce9f1
Changes
2
Hide whitespace changes
Inline
Side-by-side
where/app.py
View file @
8b8ab285
from
flask
import
Flask
,
redirect
,
jsonify
,
abort
,
request
,
url_for
,
make_response
from
webargs.flaskparser
import
use_args
from
webargs
import
fields
from
where.model
import
with_session
,
Point
,
Category
,
Field
from
where.model.field_types
import
FieldType
...
...
@@ -7,7 +9,6 @@ from where.validation import PointSchema, CategorySchema, FieldSchema
app
=
Flask
(
__name__
)
# Endpoints:
...
...
@@ -112,24 +113,25 @@ def get_category(session, id):
@
app
.
route
(
'/category/<id>/children'
)
@
with_session
def
get_category_children
(
session
,
id
):
def
get_category_children
(
data
,
session
,
id
):
data
=
dict
(
request
.
args
)
data
[
'parent_id'
]
=
id
return
search_resource
(
session
,
Point
,
data
)
@
app
.
route
(
'/point'
,
methods
=
[
'GET'
])
@
use_args
({
'parent_id'
:
fields
.
Int
(),
'category_id'
:
fields
.
Int
(
required
=
True
)})
@
with_session
def
search_point
(
session
):
return
search_resource
(
session
,
Point
,
dict
(
request
.
args
)
)
def
search_point
(
session
,
args
):
return
search_resource
(
session
,
Point
,
args
)
@
app
.
route
(
'/point'
,
methods
=
[
'POST'
])
def
create_point
(
session
):
data
=
request
.
get_json
()
data
[
'category'
]
=
session
.
query
(
Category
).
get
(
data
.
pop
(
'category_id'
))
return
create_resource
(
session
,
Point
,
data
,
'get_point'
)
@
use_args
(
PointSchema
)
@
with_session
def
create_point
(
session
,
args
):
args
[
'category'
]
=
session
.
query
(
Category
).
get
(
args
.
pop
(
'category_id'
))
return
create_resource
(
session
,
Point
,
args
,
'get_point'
)
@
app
.
route
(
'/point/<id>'
,
methods
=
[
'GET'
])
...
...
where/validation.py
View file @
8b8ab285
from
marshmallow_sqlalchemy
import
SQLAlchemyAutoSchema
from
marshmallow_sqlalchemy
import
SQLAlchemyAutoSchema
,
auto_field
from
.model
import
Point
,
Category
,
Field
class
PointSchema
(
SQLAlchemyAutoSchema
):
class
Meta
:
model
=
Point
include_relationships
=
True
load_instance
=
True
include_fk
=
True
include_relationships
=
False
load_instance
=
False
class
CategorySchema
(
SQLAlchemyAutoSchema
):
class
Meta
:
model
=
Category
include_relationships
=
Tru
e
load_instance
=
Tru
e
include_relationships
=
Fals
e
load_instance
=
Fals
e
class
FieldSchema
(
SQLAlchemyAutoSchema
):
class
Meta
:
model
=
Field
include_relationships
=
True
load_instance
=
True
\ No newline at end of file
include_relationships
=
False
load_instance
=
False
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