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
976825e0
Commit
976825e0
authored
Nov 15, 2016
by
Willard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement stall views as View and FormView classes
parent
03656886
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
59 deletions
+64
-59
stall_views.py
canteeneo/stall_views.py
+64
-59
No files found.
canteeneo/stall_views.py
View file @
976825e0
from
canteeneo
import
app
,
db
from
canteeneo.models
import
Stall
,
Location
from
canteeneo.forms
import
StallRegisterForm
from
canteeneo.views
import
flash_form_errors
,
stall_validate
from
canteeneo.views
import
flash_form_errors
,
stall_validate
,
FormView
from
flask
import
render_template
,
redirect
,
url_for
,
request
from
flask.views
import
View
from
flask_login
import
login_required
,
current_user
@
app
.
route
(
'/stalls'
)
@
login_required
def
stalls
(
):
class
StallListView
(
View
):
decorators
=
[
login_required
]
def
dispatch_request
(
self
):
return
render_template
(
'stalls.html'
,
owner
=
current_user
,
stalls
=
current_user
.
stalls
.
all
())
@
app
.
route
(
'/stalls/<int:stall_id>'
)
@
login_required
@
stall_validate
def
view_stall
(
stall_id
):
app
.
add_url_rule
(
'/stalls'
,
view_func
=
StallListView
.
as_view
(
'stalls'
),
methods
=
[
'GET'
])
class
StallView
(
View
):
decorators
=
[
login_required
,
stall_validate
]
def
dispatch_request
(
self
,
stall_id
):
stall
=
Stall
.
query
.
get
(
stall_id
)
dishes
=
stall
.
dishes
.
all
()
return
render_template
(
'viewstall.html'
,
stall
=
stall
,
dishes
=
dishes
,
upload_folder
=
app
.
config
[
'UPLOAD_FOLDER'
])
@
app
.
route
(
'/stalls/new'
,
methods
=
[
'GET'
,
'POST'
])
@
login_required
def
new_stall
():
form
=
StallRegisterForm
()
form
.
location
.
choices
=
[(
loc
.
id
,
loc
.
name
)
for
loc
in
Location
.
query
.
all
()]
if
request
.
method
==
'POST'
:
if
form
.
validate
():
app
.
add_url_rule
(
'/stalls/<int:stall_id>'
,
view_func
=
StallView
.
as_view
(
'view_stall'
),
methods
=
[
'GET'
])
class
NewStallView
(
FormView
):
decorators
=
[
login_required
]
def
render_post
(
self
):
form
=
self
.
get_form
()
stall
=
Stall
(
form
.
name
.
data
,
form
.
description
.
data
,
current_user
.
id
,
form
.
location
.
data
)
db
.
session
.
add
(
stall
)
db
.
session
.
commit
()
return
redirect
(
url_for
(
'view_stall'
,
stall_id
=
stall
.
id
))
else
:
flash_form_errors
(
form
)
return
redirect
(
url_for
(
'new_stall'
))
def
render_get
(
self
):
return
render_template
(
'newstall.html'
,
form
=
self
.
get_form
())
def
get_form
(
self
):
form
=
StallRegisterForm
()
form
.
location
.
choices
=
[(
loc
.
id
,
loc
.
name
)
for
loc
in
Location
.
query
.
all
()]
return
form
return
render_template
(
'newstall.html'
,
form
=
form
)
app
.
add_url_rule
(
'/stalls/new'
,
view_func
=
NewStallView
.
as_view
(
'new_stall'
)
)
@
app
.
route
(
'/stalls/<int:stall_id>/edit'
,
methods
=
[
'GET'
,
'POST'
])
@
login_required
@
stall_validate
def
edit_stall
(
stall_id
):
stall
=
Stall
.
query
.
get
(
stall_id
)
class
EditStallView
(
FormView
):
decorators
=
[
login_required
,
stall_validate
]
def
dispatch_request
(
self
,
stall_id
):
self
.
stall
=
Stall
.
query
.
get
(
stall_id
)
return
FormView
.
dispatch_request
(
self
,
stall_id
)
def
render_post
(
self
,
stall_id
):
form
=
self
.
get_form
()
self
.
stall
.
name
=
form
.
name
.
data
self
.
stall
.
description
=
form
.
description
.
data
self
.
stall
.
location_id
=
form
.
location
.
data
db
.
session
.
connect
()
return
redirect
(
url_for
(
'view_stall'
,
stall_id
=
stall_id
))
def
render_get
(
self
,
stall_id
):
form
=
self
.
get_form
()
form
.
name
.
data
=
self
.
stall
.
name
form
.
description
.
data
=
self
.
stall
.
description
form
.
location
.
data
=
self
.
stall
.
location_id
return
render_template
(
'editstall.html'
,
form
=
form
,
stall
=
stall
)
def
get_form
(
self
):
form
=
StallRegisterForm
()
form
.
location
.
choices
=
[(
loc
.
id
,
loc
.
name
)
for
loc
in
Location
.
query
.
all
()]
form
.
editing
=
True
return
form
if
request
.
method
==
'POST'
:
if
form
.
validate
():
stall
.
name
=
form
.
name
.
data
stall
.
description
=
form
.
description
.
data
stall
.
location_id
=
form
.
location
.
data
db
.
session
.
commit
()
return
redirect
(
url_for
(
'view_stall'
,
stall_id
=
stall_id
))
else
:
flash_form_errors
(
form
)
return
redirect
(
url_for
(
'edit_stall'
,
values
=
[(
'stall_id'
,
stall_id
)]))
else
:
form
.
name
.
data
=
stall
.
name
form
.
description
.
data
=
stall
.
description
form
.
location
.
data
=
stall
.
location_id
app
.
add_url_rule
(
'/stalls/<int:stall_id>/edit'
,
view_func
=
EditStallView
.
as_view
(
'edit_stall'
))
return
render_template
(
'editstall.html'
,
form
=
form
,
stall
=
stall
)
@
app
.
route
(
'/stalls/<int:stall_id>/delete'
,
methods
=
[
'POST'
])
@
login_required
@
stall_validate
def
delete_stall
(
stall_id
):
class
DeleteStallView
(
View
):
decorators
=
[
login_required
,
stall_validate
]
def
dispatch_request
(
self
,
stall_id
):
stall
=
Stall
.
query
.
get
(
stall_id
)
db
.
session
.
delete
(
stall
)
db
.
session
.
commit
()
return
redirect
(
url_for
(
'view_stall'
))
\ No newline at end of file
return
redirect
(
url_for
(
'view_stall'
),
stall_id
=
stall_id
)
app
.
add_url_rule
(
'/stalls/<int:stall_id>/delete'
,
view_func
=
DeleteStallView
.
as_view
(
'delete_stall'
),
methods
=
[
'POST'
])
\ 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