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
Show 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
):
def
get
(
self
,
dish_id
):
return
str
(
Dish
.
query
.
get
(
dish_id
)
in
g
.
user
.
dish_favorites
)
def
post
(
self
,
dish_id
):
dish
=
Dish
.
query
.
get
(
dish_id
)
dish
=
Dish
.
query
.
get
(
dish_id
)
if
dish
not
in
g
.
user
.
dish_favorites
:
favorited
=
request
.
form
[
'favorited'
]
==
u'1'
if
dish
not
in
g
.
user
.
dish_favorites
and
favorited
:
g
.
user
.
dish_favorites
.
append
(
dish
)
g
.
user
.
dish_favorites
.
append
(
dish
)
else
:
elif
dish
in
g
.
user
.
dish_favorites
and
not
favorited
:
g
.
user
.
dish_favorites
.
remove
(
dish
)
g
.
user
.
dish_favorites
.
remove
(
dish
)
db
.
session
.
commit
()
db
.
session
.
commit
()
return
str
(
dish
in
g
.
user
.
dish_favorites
)
return
str
(
dish
in
g
.
user
.
dish_favorites
)
@
app
.
route
(
'/api/dishes/<int:dish_id>/reviews'
,
methods
=
[
'POST'
])
class
DishReviewResource
(
MethodView
):
@
auth
.
login_required
decorators
=
[
auth
.
login_required
]
def
review_dish
(
dish_id
):
def
get
(
self
,
dish_id
):
review
=
DishReview
.
query
.
filter_by
(
user_id
=
g
.
user
.
id
,
dish_id
=
dish_id
)
.
first
()
print
(
g
.
user
.
id
,
dish_id
)
return
self
.
review_json
(
review
)
def
post
(
self
,
dish_id
):
user_id
=
g
.
user
.
id
user_id
=
g
.
user
.
id
title
=
request
.
form
[
'title'
]
title
=
request
.
form
[
'title'
]
body
=
request
.
form
[
'body'
]
body
=
request
.
form
[
'body'
]
rating
=
int
(
request
.
form
[
'rating'
])
rating
=
int
(
request
.
form
[
'rating'
])
review
=
DishReview
(
title
,
body
,
rating
,
user_id
,
dish_id
)
db
.
session
.
add
(
DishReview
(
title
,
body
,
rating
,
user_id
,
dish_id
)
)
db
.
session
.
add
(
review
)
db
.
session
.
commit
()
db
.
session
.
commit
()
return
'Review posted'
return
self
.
review_json
(
review
)
@
app
.
route
(
'/api/stalls/<int:stall_id>/favorites'
,
methods
=
[
'GET'
])
def
review_json
(
self
,
review
):
@
auth
.
login_required
if
review
is
None
:
def
favorite_stall
(
stall_id
):
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
)
stall
=
Stall
.
query
.
get
(
stall_id
)
if
stall
not
in
g
.
user
.
stall_favorites
:
favorited
=
request
.
form
[
'favorited'
]
==
u'1'
if
stall
not
in
g
.
user
.
stall_favorites
and
favorited
:
g
.
user
.
stall_favorites
.
append
(
stall
)
g
.
user
.
stall_favorites
.
append
(
stall
)
else
:
elif
stall
in
g
.
user
.
stall_favorites
and
not
favorited
:
g
.
user
.
stall_favorites
.
remove
(
stall
)
g
.
user
.
stall_favorites
.
remove
(
stall
)
db
.
session
.
commit
()
db
.
session
.
commit
()
return
str
(
stall
in
g
.
user
.
stall_favorites
)
return
str
(
stall
in
g
.
user
.
stall_favorites
)
@
app
.
route
(
'/api/stalls/<int:stall_id>/reviews'
,
methods
=
[
'POST'
])
class
StallReviewResource
(
MethodView
):
@
auth
.
login_required
decorators
=
[
auth
.
login_required
]
def
review_stall
(
stall_id
):
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
user_id
=
g
.
user
.
id
title
=
request
.
form
[
'title'
]
title
=
request
.
form
[
'title'
]
body
=
request
.
form
[
'body'
]
body
=
request
.
form
[
'body'
]
rating
=
int
(
request
.
form
[
'rating'
])
rating
=
int
(
request
.
form
[
'rating'
])
review
=
StallReview
(
title
,
body
,
rating
,
user_id
,
stall_id
)
db
.
session
.
add
(
StallReview
(
title
,
body
,
rating
,
user_id
,
stall_id
)
)
db
.
session
.
add
(
review
)
db
.
session
.
commit
()
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