Commit 0e07d5c1 authored by Willard's avatar Willard

Implement rest of dish_views as classes

parent 48e5fd07
......@@ -6,6 +6,7 @@ from canteeneo.forms import DishRegisterForm
from canteeneo.views import flash_form_errors, stall_validate, dish_validate, FormView
from flask import render_template, redirect, url_for, request
from flask.views import View
from flask_login import login_required
from werkzeug.utils import secure_filename
......@@ -26,47 +27,43 @@ class NewDishView(FormView):
def get_form(self):
return DishRegisterForm()
app.add_url_rule('/stalls/<int:stall_id>/dishes/new', view_func=NewDishView.as_view('new_dish'))
@app.route('/stalls/<int:stall_id>/dishes/<int:dish_id>', methods=["GET"])
@login_required
@dish_validate
def dish(stall_id, dish_id):
class DishView(View):
decorators=[login_required, dish_validate]
def dispatch_request(self, stall_id, dish_id):
return render_template('viewdish.html', stall=Stall.query.get(stall_id), dish=Dish.query.get(dish_id))
@app.route('/stalls/<int:stall_id>/dishes/<int:dish_id>/edit', methods=['GET', 'POST'])
@login_required
@dish_validate
def edit_dish(stall_id, dish_id):
stall = Stall.query.get(stall_id)
dish = Dish.query.get(dish_id)
form = DishRegisterForm()
if request.method == 'POST':
if form.validate(editing=True):
dish.name = form.name.data
dish.description = form.description.data
dish.price = form.price.data
class EditDishView(FormView):
decorators=[login_required, dish_validate]
def dispatch_request(self, stall_id, dish_id):
self.stall = Stall.query.get(stall_id)
self.dish = Dish.query.get(dish_id)
return FormView.dispatch_request(self, stall_id, dish_id)
def render_post(self, stall_id, dish_id):
self.dish.name = form.name.data
self.dish.description = form.description.data
self.dish.price = form.price.data
if form.image.data.filename is not u'':
dish.image_path = secure_filename(form.image.data.filename)
form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], dish.image_path))
self.dish.image_path = secure_filename(form.image.data.filename)
form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], self.dish.image_path))
db.session.commit()
return redirect(url_for('view_stall', stall_id=stall_id))
else:
flash_form_errors(form)
return redirect(url_for('edit_dish', stall_id=stall_id, dish_id=dish_id))
else:
form.name.data = dish.name
form.description.data = dish.description
form.price.data = dish.price
return render_template('editdish.html', form=form, stall=stall, dish=dish)
return redirect(url_for('view_stall', stall_id=stall.id))
def render_get(self, stall_id, dish_id):
form.name.data = self.dish.name
form.description.data = self.dish.description
form.price.data = self.dish.price
return render_template('editdish.html', form=form, stall=self.stall, dish=self.dish)
def get_form(self):
return DishRegisterForm()
@app.route('/stalls/<int:stall_id>/dishes/<int:dish_id>/delete', methods=['POST'])
@login_required
@dish_validate
def delete_dish(stall_id, dish_id):
class DeleteDishView(View):
decorators=[login_required, dish_validate]
def dispatch_request(self, stall_id, dish_id):
dish = Dish.query.get(dish_id)
db.session.delete(dish)
db.session.commit()
return redirect(url_for('view_stall', stall_id=stall_id))
app.add_url_rule('/stalls/<int:stall_id>/dishes/new', view_func=NewDishView.as_view('new_dish'))
app.add_url_rule('/stalls/<int:stall_id>/dishes/<int:dish_id>', view_func=DishView.as_view('view_dish'), methods=['GET'])
app.add_url_rule('/stalls/<int:stall_id>/dishes/<int:dish_id>/edit', view_func=EditDishView.as_view('edit_dish'))
app.add_url_rule('/stalls/<int:stall_id>/dishes/<int:dish_id>/delete', view_func=DeleteDishView.as_view('delete_dish'), methods=['POST'])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment