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 ...@@ -6,6 +6,7 @@ from canteeneo.forms import DishRegisterForm
from canteeneo.views import flash_form_errors, stall_validate, dish_validate, FormView 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.views import View
from flask_login import login_required from flask_login import login_required
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
...@@ -26,47 +27,43 @@ class NewDishView(FormView): ...@@ -26,47 +27,43 @@ class NewDishView(FormView):
def get_form(self): def get_form(self):
return DishRegisterForm() return DishRegisterForm()
app.add_url_rule('/stalls/<int:stall_id>/dishes/new', view_func=NewDishView.as_view('new_dish')) class DishView(View):
decorators=[login_required, dish_validate]
@app.route('/stalls/<int:stall_id>/dishes/<int:dish_id>', methods=["GET"]) def dispatch_request(self, stall_id, dish_id):
@login_required
@dish_validate
def dish(stall_id, dish_id):
return render_template('viewdish.html', stall=Stall.query.get(stall_id), dish=Dish.query.get(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']) class EditDishView(FormView):
@login_required decorators=[login_required, dish_validate]
@dish_validate def dispatch_request(self, stall_id, dish_id):
def edit_dish(stall_id, dish_id): self.stall = Stall.query.get(stall_id)
stall = Stall.query.get(stall_id) self.dish = Dish.query.get(dish_id)
dish = Dish.query.get(dish_id) return FormView.dispatch_request(self, stall_id, dish_id)
form = DishRegisterForm() def render_post(self, stall_id, dish_id):
if request.method == 'POST': self.dish.name = form.name.data
if form.validate(editing=True): self.dish.description = form.description.data
dish.name = form.name.data self.dish.price = form.price.data
dish.description = form.description.data
dish.price = form.price.data
if form.image.data.filename is not u'': if form.image.data.filename is not u'':
dish.image_path = secure_filename(form.image.data.filename) self.dish.image_path = secure_filename(form.image.data.filename)
form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], dish.image_path)) form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], self.dish.image_path))
db.session.commit() db.session.commit()
return redirect(url_for('view_stall', stall_id=stall_id)) return redirect(url_for('view_stall', stall_id=stall.id))
else: def render_get(self, stall_id, dish_id):
flash_form_errors(form) form.name.data = self.dish.name
return redirect(url_for('edit_dish', stall_id=stall_id, dish_id=dish_id)) form.description.data = self.dish.description
else: form.price.data = self.dish.price
form.name.data = dish.name return render_template('editdish.html', form=form, stall=self.stall, dish=self.dish)
form.description.data = dish.description def get_form(self):
form.price.data = dish.price return DishRegisterForm()
return render_template('editdish.html', form=form, stall=stall, dish=dish)
@app.route('/stalls/<int:stall_id>/dishes/<int:dish_id>/delete', methods=['POST']) class DeleteDishView(View):
@login_required decorators=[login_required, dish_validate]
@dish_validate def dispatch_request(self, stall_id, dish_id):
def delete_dish(stall_id, dish_id):
dish = Dish.query.get(dish_id) dish = Dish.query.get(dish_id)
db.session.delete(dish) db.session.delete(dish)
db.session.commit() db.session.commit()
return redirect(url_for('view_stall', stall_id=stall_id)) 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