Commit bee55882 authored by Willard's avatar Willard

Use decorators for stall and dish validation

parent 8695734d
import os.path import os.path
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_login import login_required, login_user, logout_user, current_user from flask_login import login_required, login_user, logout_user, current_user
...@@ -51,6 +52,30 @@ def register(): ...@@ -51,6 +52,30 @@ def register():
return render_template('register.html', form=form) return render_template('register.html', form=form)
def stall_validate(f):
@wraps(f)
def wrapper(*args, **kwargs):
stall_id = int(kwargs.get('stall_id'))
stall = Stall.query.get(stall_id)
if stall is None or stall.owner != current_user:
return redirect(url_for('stalls'))
return f(*args, **kwargs)
return wrapper
def dish_validate(f):
@wraps(f)
def wrapper(*args, **kwargs):
stall_id = int(kwargs.get('stall_id'))
dish_id = int(kwargs.get('dish_id'))
stall = Stall.query.get(stall_id)
if stall is None or stall.owner != current_user:
return redirect(url_for('stalls'))
dish = Dish.query.get(dish_id)
if dish is None or dish.stall != stall:
return redirect(url_for('stall', stall_id=stall_id))
return f(*args, **kwargs)
return wrapper
@app.route('/stalls') @app.route('/stalls')
@login_required @login_required
def stalls(): def stalls():
...@@ -58,11 +83,9 @@ def stalls(): ...@@ -58,11 +83,9 @@ def stalls():
@app.route('/stalls/<int:stall_id>') @app.route('/stalls/<int:stall_id>')
@login_required @login_required
@stall_validate
def stall(stall_id): def stall(stall_id):
stall = Stall.query.filter_by(id=int(stall_id)).first() stall = Stall.query.get(stall_id)
if stall is None or stall.owner != current_user:
return redirect(url_for('stalls'))
dishes = stall.dishes.all() dishes = stall.dishes.all()
return render_template('stall.html', stall=stall, dishes=dishes, upload_folder=app.config['UPLOAD_FOLDER']) return render_template('stall.html', stall=stall, dishes=dishes, upload_folder=app.config['UPLOAD_FOLDER'])
...@@ -85,11 +108,9 @@ def new_stall(): ...@@ -85,11 +108,9 @@ def new_stall():
@app.route('/stalls/<int:stall_id>/edit', methods=['GET', 'POST']) @app.route('/stalls/<int:stall_id>/edit', methods=['GET', 'POST'])
@login_required @login_required
@stall_validate
def edit_stall(stall_id): def edit_stall(stall_id):
stall = Stall.query.filter_by(id=int(stall_id)).first() stall = Stall.query.get(stall_id)
if stall is None or stall.owner != current_user:
return redirect(url_to('stalls'))
form = StallRegisterForm() form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()] form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
...@@ -112,12 +133,9 @@ def edit_stall(stall_id): ...@@ -112,12 +133,9 @@ def edit_stall(stall_id):
@app.route('/stalls/<int:stall_id>/delete', methods=['POST']) @app.route('/stalls/<int:stall_id>/delete', methods=['POST'])
@login_required @login_required
@stall_validate
def delete_stall(stall_id): def delete_stall(stall_id):
stall = Stall.query.filter_by(id=int(stall_id)).first() stall = Stall.query.get(stall_id)
if stall is None or stall.owner != current_user:
return redirect(url_for('stalls'))
db.session.delete(stall) db.session.delete(stall)
db.session.commit() db.session.commit()
...@@ -125,11 +143,9 @@ def delete_stall(stall_id): ...@@ -125,11 +143,9 @@ def delete_stall(stall_id):
@app.route('/stalls/<int:stall_id>/dish/new', methods=['GET','POST']) @app.route('/stalls/<int:stall_id>/dish/new', methods=['GET','POST'])
@login_required @login_required
@stall_validate
def new_dish(stall_id): def new_dish(stall_id):
stall = Stall.query.filter_by(id=int(stall_id)).first() stall = Stall.query.get(stall_id)
if stall is None or stall.owner != current_user:
return redirect(url_for('stalls'))
form = DishRegisterForm() form = DishRegisterForm()
if request.method == 'POST': if request.method == 'POST':
if form.validate(): if form.validate():
...@@ -147,15 +163,10 @@ def new_dish(stall_id): ...@@ -147,15 +163,10 @@ def new_dish(stall_id):
@app.route('/stalls/<int:stall_id>/dish/<int:dish_id>/edit', methods=['GET', 'POST']) @app.route('/stalls/<int:stall_id>/dish/<int:dish_id>/edit', methods=['GET', 'POST'])
@login_required @login_required
@dish_validate
def edit_dish(stall_id, dish_id): def edit_dish(stall_id, dish_id):
stall = Stall.query.filter_by(id=int(stall_id)).first() stall = Stall.query.get(stall_id)
if stall is None or stall.owner != current_user: dish = Dish.query.get(dish_id)
return redirect(url_for('stalls'))
dish = Dish.query.filter_by(id=dish_id).first()
if dish is None or dish.stall != stall:
return redirect(url_for('stall', stall_id=stall_id))
form = DishRegisterForm() form = DishRegisterForm()
if request.method == 'POST': if request.method == 'POST':
if form.validate(editing=True): if form.validate(editing=True):
...@@ -179,15 +190,9 @@ def edit_dish(stall_id, dish_id): ...@@ -179,15 +190,9 @@ def edit_dish(stall_id, dish_id):
@app.route('/stalls/<int:stall_id>/dish/<int:dish_id>/delete', methods=['POST']) @app.route('/stalls/<int:stall_id>/dish/<int:dish_id>/delete', methods=['POST'])
@login_required @login_required
@dish_validate
def delete_dish(stall_id, dish_id): def delete_dish(stall_id, dish_id):
stall = Stall.query.filter_by(id=int(stall_id)).first() dish = Dish.query.get(dish_id)
if stall is None or stall.owner != current_user:
return redirect(url_for('stalls'))
dish = Dish.query.filter_by(id=dish_id).first()
if dish is None or dish.stall != stall:
return redirect(url_for('stall', stall_id=stall_id))
db.session.delete(dish) db.session.delete(dish)
db.session.commit() db.session.commit()
......
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