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]
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>', methods=["GET"]) class EditDishView(FormView):
@login_required decorators=[login_required, dish_validate]
@dish_validate def dispatch_request(self, stall_id, dish_id):
def dish(stall_id, dish_id): self.stall = Stall.query.get(stall_id)
return render_template('viewdish.html', stall=Stall.query.get(stall_id), dish=Dish.query.get(dish_id)) self.dish = Dish.query.get(dish_id)
return FormView.dispatch_request(self, stall_id, dish_id)
@app.route('/stalls/<int:stall_id>/dishes/<int:dish_id>/edit', methods=['GET', 'POST']) def render_post(self, stall_id, dish_id):
@login_required self.dish.name = form.name.data
@dish_validate self.dish.description = form.description.data
def edit_dish(stall_id, dish_id): self.dish.price = form.price.data
stall = Stall.query.get(stall_id) if form.image.data.filename is not u'':
dish = Dish.query.get(dish_id) self.dish.image_path = secure_filename(form.image.data.filename)
form = DishRegisterForm() form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], self.dish.image_path))
if request.method == 'POST': db.session.commit()
if form.validate(editing=True): return redirect(url_for('view_stall', stall_id=stall.id))
dish.name = form.name.data def render_get(self, stall_id, dish_id):
dish.description = form.description.data form.name.data = self.dish.name
dish.price = form.price.data form.description.data = self.dish.description
if form.image.data.filename is not u'': form.price.data = self.dish.price
dish.image_path = secure_filename(form.image.data.filename) return render_template('editdish.html', form=form, stall=self.stall, dish=self.dish)
form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], dish.image_path)) def get_form(self):
db.session.commit() return DishRegisterForm()
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)
@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'))
\ No newline at end of file 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