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
a471ce06
Commit
a471ce06
authored
Feb 18, 2020
by
Zach Perkins
Browse files
Added a decorator to make building endpoints easier
parent
ef44a098
Changes
2
Hide whitespace changes
Inline
Side-by-side
where/app.py
View file @
a471ce06
from
flask
import
Flask
,
redirect
,
jsonify
,
abort
from
flask
import
Flask
,
redirect
,
jsonify
,
abort
from
where.model.field_types
import
FieldType
from
where.model.field_types
import
FieldType
from
where.model.sa
import
Category
,
Point
,
Field
,
session
_context
from
where.model.sa
import
Category
,
Point
,
Field
,
with_
session
app
=
Flask
(
__name__
)
app
=
Flask
(
__name__
)
...
@@ -20,74 +20,79 @@ def index():
...
@@ -20,74 +20,79 @@ def index():
@
app
.
route
(
'/test_data'
)
@
app
.
route
(
'/test_data'
)
def
test_data
():
@
with_session
with
session_context
()
as
session
:
def
test_data
(
session
):
# session = Session()
# session = Session()
session
.
query
(
Point
).
delete
()
session
.
query
(
Point
).
delete
()
session
.
query
(
Field
).
delete
()
session
.
query
(
Field
).
delete
()
session
.
query
(
Category
).
delete
()
session
.
query
(
Category
).
delete
()
# Water Fountain, the class.
# Water Fountain, the class.
wf
=
Category
()
wf
=
Category
()
wf
.
name
=
"Water Fountain"
wf
.
name
=
"Water Fountain"
wf
.
icon
=
"https://karel.pw/water.png"
wf
.
icon
=
"https://karel.pw/water.png"
session
.
add
(
wf
)
session
.
add
(
wf
)
session
.
commit
()
session
.
commit
()
# coldness
# coldness
cd
=
Field
()
cd
=
Field
()
cd
.
name
=
"Coldness"
cd
.
name
=
"Coldness"
cd
.
slug
=
"coldness"
cd
.
slug
=
"coldness"
cd
.
type
=
FieldType
.
RATING
cd
.
type
=
FieldType
.
RATING
cd
.
category_id
=
wf
.
id
cd
.
category_id
=
wf
.
id
# filler
# filler
fl
=
Field
()
fl
=
Field
()
fl
.
slug
=
"bottle_filler"
fl
.
slug
=
"bottle_filler"
fl
.
name
=
"Has Bottle Filler"
fl
.
name
=
"Has Bottle Filler"
fl
.
type
=
FieldType
.
BOOLEAN
fl
.
type
=
FieldType
.
BOOLEAN
fl
.
category_id
=
wf
.
id
fl
.
category_id
=
wf
.
id
session
.
add
(
cd
)
session
.
add
(
cd
)
session
.
add
(
fl
)
session
.
add
(
fl
)
session
.
commit
()
session
.
commit
()
# an actual instance!
# an actual instance!
fn
=
Point
()
fn
=
Point
()
fn
.
name
=
None
fn
.
name
=
None
fn
.
lat
=
38.829791
fn
.
lat
=
38.829791
fn
.
lon
=
-
77.307043
fn
.
lon
=
-
77.307043
# fn.category_id = wf.id
# fn.category_id = wf.id
fn
.
category
=
wf
fn
.
category
=
wf
fn
.
parent_id
=
None
fn
.
parent_id
=
None
fn
.
attributes
=
{
fn
.
attributes
=
{
"coldness"
:
{
"coldness"
:
{
"num_reviews"
:
32
,
"num_reviews"
:
32
,
"average_rating"
:
0.5
"average_rating"
:
0.5
},
},
"bottle_filler"
:
{
"bottle_filler"
:
{
"value"
:
True
"value"
:
True
}
}
}
session
.
add
(
fn
)
}
session
.
commit
()
session
.
add
(
fn
)
return
redirect
(
'/'
)
session
.
commit
()
return
redirect
(
'/'
)
@
app
.
route
(
'/category/<id>'
)
@
app
.
route
(
'/category/<id>'
)
def
get_category
(
id
):
@
with_session
with
session_context
()
as
session
:
def
get_category
(
session
,
id
)
:
result
=
session
.
query
(
Category
).
filter_by
(
id
=
id
).
first
()
result
=
session
.
query
(
Category
).
filter_by
(
id
=
id
).
first
()
if
result
:
if
result
:
return
jsonify
(
result
.
as_json
())
return
jsonify
(
result
.
as_json
())
else
:
else
:
abort
(
404
)
abort
(
404
)
@
app
.
route
(
'/point/<id>'
)
@
app
.
route
(
'/point/<id>'
)
def
get_point
(
id
):
@
with_session
with
session_context
()
as
session
:
def
get_point
(
session
,
id
)
:
result
=
session
.
query
(
Point
).
filter_by
(
id
=
id
).
first
()
result
=
session
.
query
(
Point
).
filter_by
(
id
=
id
).
first
()
if
result
:
if
result
:
return
jsonify
(
result
.
as_json
())
return
jsonify
(
result
.
as_json
())
else
:
else
:
abort
(
404
)
abort
(
404
)
@
app
.
route
(
'/point'
,
methods
=
[
'POST'
])
@
with_session
def
query_point
():
pass
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
app
.
run
()
app
.
run
()
where/model/sa.py
View file @
a471ce06
...
@@ -21,6 +21,16 @@ def session_context():
...
@@ -21,6 +21,16 @@ def session_context():
finally
:
finally
:
session
.
close
()
session
.
close
()
# Decorator for convenience when building endpoints
def
with_session
(
func
):
def
wrapper
(
*
args
,
**
kwargs
):
with
session_context
()
as
session
:
func
(
session
,
*
args
,
**
kwargs
)
# Flask identifies endpoint handlers based on their name
wrapper
.
__name__
=
func
.
__name__
return
wrapper
@
as_declarative
()
@
as_declarative
()
class
Base
(
object
):
class
Base
(
object
):
...
...
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