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
48e5fd07
Commit
48e5fd07
authored
Nov 10, 2016
by
Willard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement FormView, rewrite new_dish using it
parent
121466d5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
20 deletions
+37
-20
dish_views.py
canteeneo/dish_views.py
+20
-20
views.py
canteeneo/views.py
+17
-0
No files found.
canteeneo/dish_views.py
View file @
48e5fd07
...
@@ -3,30 +3,30 @@ import os.path
...
@@ -3,30 +3,30 @@ import os.path
from
canteeneo
import
app
,
db
from
canteeneo
import
app
,
db
from
canteeneo.models
import
Stall
,
Dish
from
canteeneo.models
import
Stall
,
Dish
from
canteeneo.forms
import
DishRegisterForm
from
canteeneo.forms
import
DishRegisterForm
from
canteeneo.views
import
flash_form_errors
,
stall_validate
,
dish_validate
from
canteeneo.views
import
flash_form_errors
,
stall_validate
,
dish_validate
,
FormView
from
flask
import
render_template
,
redirect
,
url_for
,
request
from
flask
import
render_template
,
redirect
,
url_for
,
request
from
flask_login
import
login_required
from
flask_login
import
login_required
from
werkzeug.utils
import
secure_filename
from
werkzeug.utils
import
secure_filename
@
app
.
route
(
'/stalls/<int:stall_id>/dishes/new'
,
methods
=
[
'GET'
,
'POST'
])
class
NewDishView
(
FormView
):
@
login_required
decorators
=
[
login_required
,
stall_validate
]
@
stall_validate
def
dispatch_request
(
self
,
stall_id
):
def
new_dish
(
stall_id
):
self
.
stall
=
Stall
.
query
.
get
(
stall_id
)
stall
=
Stall
.
query
.
get
(
stall_id
)
return
FormView
.
dispatch_request
(
self
,
stall_id
)
form
=
DishRegisterForm
()
def
render_post
(
self
,
stall_id
):
if
request
.
method
==
'POST'
:
filename
=
secure_filename
(
form
.
image
.
data
.
filename
)
if
form
.
validate
():
dish
=
Dish
(
form
.
name
.
data
,
form
.
description
.
data
,
form
.
price
.
data
,
self
.
stall
.
id
,
filename
)
filename
=
secure_filename
(
form
.
image
.
data
.
filename
)
form
.
image
.
data
.
save
(
os
.
path
.
join
(
app
.
config
[
'UPLOAD_FOLDER'
],
filename
))
dish
=
Dish
(
form
.
name
.
data
,
form
.
description
.
data
,
form
.
price
.
data
,
stall
.
id
,
filename
)
db
.
session
.
add
(
dish
)
form
.
image
.
data
.
save
(
os
.
path
.
join
(
app
.
config
[
'UPLOAD_FOLDER'
],
filename
))
db
.
session
.
commit
()
db
.
session
.
add
(
dish
)
return
redirect
(
url_for
(
'view_stall'
,
stall_id
=
stall
.
id
))
db
.
session
.
commit
()
def
render_get
(
self
,
stall_id
):
return
redirect
(
url_for
(
'view_stall'
,
stall_id
=
stall
.
id
))
return
render_template
(
'newdish.html'
,
form
=
self
.
get_form
(),
stall
=
self
.
stall
)
else
:
def
get_form
(
self
):
flash_form_errors
(
form
)
return
DishRegisterForm
()
return
redirect
(
url_for
(
'new_dish'
,
stall_id
=
stall
.
id
))
else
:
app
.
add_url_rule
(
'/stalls/<int:stall_id>/dishes/new'
,
view_func
=
NewDishView
.
as_view
(
'new_dish'
))
return
render_template
(
'newdish.html'
,
form
=
form
,
stall
=
stall
)
@
app
.
route
(
'/stalls/<int:stall_id>/dishes/<int:dish_id>'
,
methods
=
[
"GET"
])
@
app
.
route
(
'/stalls/<int:stall_id>/dishes/<int:dish_id>'
,
methods
=
[
"GET"
])
@
login_required
@
login_required
...
...
canteeneo/views.py
View file @
48e5fd07
...
@@ -2,10 +2,27 @@ import os.path
...
@@ -2,10 +2,27 @@ import os.path
from
functools
import
wraps
from
functools
import
wraps
from
canteeneo
import
app
,
login_manager
,
db
from
canteeneo
import
app
,
login_manager
,
db
from
flask
import
flash
,
redirect
,
render_template
,
request
,
url_for
from
flask
import
flash
,
redirect
,
render_template
,
request
,
url_for
from
flask.views
import
View
from
flask_login
import
login_required
,
login_user
,
logout_user
,
current_user
from
flask_login
import
login_required
,
login_user
,
logout_user
,
current_user
from
forms
import
OwnerLoginForm
,
OwnerRegisterForm
from
forms
import
OwnerLoginForm
,
OwnerRegisterForm
from
models
import
Owner
,
Stall
,
Dish
from
models
import
Owner
,
Stall
,
Dish
class
FormView
(
View
):
def
dispatch_request
(
self
,
**
kwargs
):
form
=
self
.
get_form
()
if
request
.
method
==
'POST'
:
if
form
.
validate
():
return
self
.
render_post
(
**
kwargs
)
flash_form_errors
(
form
)
elif
request
.
method
==
'GET'
:
return
self
.
render_get
(
**
kwargs
)
def
get_form
(
self
):
raise
NotImplementedError
()
def
render_post
(
self
,
**
kwargs
):
return
''
def
render_get
(
self
,
**
kwargs
):
return
''
@
app
.
route
(
'/'
)
@
app
.
route
(
'/'
)
def
index
():
def
index
():
if
current_user
.
is_authenticated
:
if
current_user
.
is_authenticated
:
...
...
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