Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CS123-Canteeneo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Willard Torres
CS123-Canteeneo
Commits
da2d39ff
Commit
da2d39ff
authored
Nov 23, 2016
by
Willard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement favorites and reviews using MethodView
parent
6e74f17d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
91 additions
and
39 deletions
+91
-39
api.py
canteeneo/api.py
+91
-39
No files found.
canteeneo/api.py
View file @
da2d39ff
from
canteeneo
import
app
,
db
,
auth
from
canteeneo
import
app
,
db
,
auth
from
flask
import
jsonify
,
request
,
g
from
flask
import
jsonify
,
request
,
g
from
flask.views
import
MethodView
from
models
import
Dish
,
Stall
,
Location
,
User
,
DishReview
,
StallReview
from
models
import
Dish
,
Stall
,
Location
,
User
,
DishReview
,
StallReview
from
datetime
import
datetime
from
datetime
import
datetime
from
itsdangerous
import
TimedJSONWebSignatureSerializer
as
Serializer
,
BadSignature
,
SignatureExpired
from
itsdangerous
import
TimedJSONWebSignatureSerializer
as
Serializer
,
BadSignature
,
SignatureExpired
...
@@ -93,47 +94,98 @@ def get_auth_token():
...
@@ -93,47 +94,98 @@ def get_auth_token():
token
=
generate_token
(
g
.
user
)
token
=
generate_token
(
g
.
user
)
return
jsonify
({
'token'
:
token
.
decode
(
'ascii'
),
'duration'
:
600
})
return
jsonify
({
'token'
:
token
.
decode
(
'ascii'
),
'duration'
:
600
})
@
app
.
route
(
'/api/dishes/<int:dish_id>/favorites'
,
methods
=
[
'GET'
])
class
DishFavoriteResource
(
MethodView
):
@
auth
.
login_required
decorators
=
[
auth
.
login_required
]
def
favorite_dish
(
dish_id
):
dish
=
Dish
.
query
.
get
(
dish_id
)
if
dish
not
in
g
.
user
.
dish_favorites
:
g
.
user
.
dish_favorites
.
append
(
dish
)
else
:
g
.
user
.
dish_favorites
.
remove
(
dish
)
db
.
session
.
commit
()
return
str
(
dish
in
g
.
user
.
dish_favorites
)
@
app
.
route
(
'/api/dishes/<int:dish_id>/reviews'
,
methods
=
[
'POST'
])
def
get
(
self
,
dish_id
):
@
auth
.
login_required
return
str
(
Dish
.
query
.
get
(
dish_id
)
in
g
.
user
.
dish_favorites
)
def
review_dish
(
dish_id
):
user_id
=
g
.
user
.
id
def
post
(
self
,
dish_id
):
title
=
request
.
form
[
'title'
]
dish
=
Dish
.
query
.
get
(
dish_id
)
body
=
request
.
form
[
'body'
]
favorited
=
request
.
form
[
'favorited'
]
==
u'1'
rating
=
int
(
request
.
form
[
'rating'
])
if
dish
not
in
g
.
user
.
dish_favorites
and
favorited
:
g
.
user
.
dish_favorites
.
append
(
dish
)
elif
dish
in
g
.
user
.
dish_favorites
and
not
favorited
:
g
.
user
.
dish_favorites
.
remove
(
dish
)
db
.
session
.
commit
()
return
str
(
dish
in
g
.
user
.
dish_favorites
)
db
.
session
.
add
(
DishReview
(
title
,
body
,
rating
,
user_id
,
dish_id
))
class
DishReviewResource
(
MethodView
):
db
.
session
.
commit
()
decorators
=
[
auth
.
login_required
]
return
'Review posted'
@
app
.
route
(
'/api/stalls/<int:stall_id>/favorites'
,
methods
=
[
'GET'
])
def
get
(
self
,
dish_id
):
@
auth
.
login_required
review
=
DishReview
.
query
.
filter_by
(
user_id
=
g
.
user
.
id
,
dish_id
=
dish_id
)
.
first
()
def
favorite_stall
(
stall_id
):
print
(
g
.
user
.
id
,
dish_id
)
stall
=
Stall
.
query
.
get
(
stall_id
)
return
self
.
review_json
(
review
)
if
stall
not
in
g
.
user
.
stall_favorites
:
g
.
user
.
stall_favorites
.
append
(
stall
)
else
:
g
.
user
.
stall_favorites
.
remove
(
stall
)
db
.
session
.
commit
()
return
str
(
stall
in
g
.
user
.
stall_favorites
)
@
app
.
route
(
'/api/stalls/<int:stall_id>/reviews'
,
methods
=
[
'POST'
])
def
post
(
self
,
dish_id
):
@
auth
.
login_required
user_id
=
g
.
user
.
id
def
review_stall
(
stall_id
):
title
=
request
.
form
[
'title'
]
user_id
=
g
.
user
.
id
body
=
request
.
form
[
'body'
]
title
=
request
.
form
[
'title'
]
rating
=
int
(
request
.
form
[
'rating'
])
body
=
request
.
form
[
'body'
]
review
=
DishReview
(
title
,
body
,
rating
,
user_id
,
dish_id
)
rating
=
int
(
request
.
form
[
'rating'
])
db
.
session
.
add
(
review
)
db
.
session
.
commit
()
return
self
.
review_json
(
review
)
db
.
session
.
add
(
StallReview
(
title
,
body
,
rating
,
user_id
,
stall_id
))
def
review_json
(
self
,
review
):
db
.
session
.
commit
()
if
review
is
None
:
return
''
review
=
{
'title'
:
review
.
title
,
'body'
:
review
.
body
,
'rating'
:
review
.
rating
,
'user_id'
:
review
.
user_id
,
'dish_id'
:
review
.
dish_id
}
return
jsonify
(
review
)
class
StallFavoriteResource
(
MethodView
):
decorators
=
[
auth
.
login_required
]
def
get
(
self
,
stall_id
):
return
str
(
Stall
.
query
.
get
(
stall_id
)
in
g
.
user
.
stall_favorites
)
def
post
(
self
,
stall_id
):
stall
=
Stall
.
query
.
get
(
stall_id
)
favorited
=
request
.
form
[
'favorited'
]
==
u'1'
if
stall
not
in
g
.
user
.
stall_favorites
and
favorited
:
g
.
user
.
stall_favorites
.
append
(
stall
)
elif
stall
in
g
.
user
.
stall_favorites
and
not
favorited
:
g
.
user
.
stall_favorites
.
remove
(
stall
)
db
.
session
.
commit
()
return
str
(
stall
in
g
.
user
.
stall_favorites
)
class
StallReviewResource
(
MethodView
):
decorators
=
[
auth
.
login_required
]
def
get
(
self
,
stall_id
):
review
=
StallReview
.
query
.
filter_by
(
user_id
=
g
.
user
.
id
,
stall_id
=
stall_id
)
.
first
()
return
self
.
review_json
(
review
)
def
post
(
self
,
stall_id
):
user_id
=
g
.
user
.
id
title
=
request
.
form
[
'title'
]
body
=
request
.
form
[
'body'
]
rating
=
int
(
request
.
form
[
'rating'
])
review
=
StallReview
(
title
,
body
,
rating
,
user_id
,
stall_id
)
db
.
session
.
add
(
review
)
db
.
session
.
commit
()
return
self
.
review_json
(
review
)
def
review_json
(
self
,
review
):
if
review
is
None
:
return
''
review
=
{
'title'
:
review
.
title
,
'body'
:
review
.
body
,
'rating'
:
review
.
rating
,
'user_id'
:
review
.
user_id
,
'dish_id'
:
review
.
dish_id
}
return
jsonify
(
review
)
app
.
add_url_rule
(
'/api/dishes/<int:dish_id>/favorites'
,
view_func
=
DishFavoriteResource
.
as_view
(
'dish_favorite'
))
app
.
add_url_rule
(
'/api/dishes/<int:dish_id>/reviews'
,
view_func
=
DishReviewResource
.
as_view
(
'dish_review'
))
app
.
add_url_rule
(
'/api/stalls/<int:stall_id>/favorites'
,
view_func
=
StallFavoriteResource
.
as_view
(
'stall_favorite'
))
app
.
add_url_rule
(
'/api/stalls/<int:stall_id>/reviews'
,
view_func
=
StallReviewResource
.
as_view
(
'stall_review'
))
\ No newline at end of file
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