Commit daf16ac4 authored by Willard's avatar Willard

Move stall & dish specific views to separate files

parent 380b8c23
import os.path
from canteeneo import app, db
from canteeneo.models import Stall, Dish
from canteeneo.forms import DishRegisterForm
from canteeneo.views import flash_form_errors, stall_validate, dish_validate
from flask import render_template, redirect, url_for, request
from flask_login import login_required
from werkzeug.utils import secure_filename
@app.route('/stalls/<int:stall_id>/dishes/new', methods=['GET','POST'])
@login_required
@stall_validate
def new_dish(stall_id):
stall = Stall.query.get(stall_id)
form = DishRegisterForm()
if request.method == 'POST':
if form.validate():
filename = secure_filename(form.image.data.filename)
dish = Dish(form.name.data, form.description.data, form.price.data, stall.id, filename)
form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
db.session.add(dish)
db.session.commit()
return redirect(url_for('stall', stall_id=stall.id))
else:
flash_form_errors(form)
return redirect(url_for('new_dish', stall_id=stall.id))
else:
return render_template('newdish.html', form=form, stall=stall)
@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
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))
db.session.commit()
return redirect(url_for('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'])
@login_required
@dish_validate
def delete_dish(stall_id, dish_id):
dish = Dish.query.get(dish_id)
db.session.delete(dish)
db.session.commit()
return redirect(url_for('stall', stall_id=stall_id))
\ No newline at end of file
from canteeneo import app, db
from canteeneo.models import Stall, Location
from canteeneo.forms import StallRegisterForm
from canteeneo.views import flash_form_errors, stall_validate
from flask import render_template, redirect, url_for, request
from flask_login import login_required, current_user
@app.route('/stalls')
@login_required
def stalls():
return render_template('stalls.html', owner=current_user, stalls=current_user.stalls.all())
@app.route('/stalls/<int:stall_id>')
@login_required
@stall_validate
def stall(stall_id):
stall = Stall.query.get(stall_id)
dishes = stall.dishes.all()
return render_template('stall.html', stall=stall, dishes=dishes, upload_folder=app.config['UPLOAD_FOLDER'])
@app.route('/stalls/new', methods=['GET','POST'])
@login_required
def new_stall():
form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
if request.method == 'POST':
if form.validate():
stall = Stall(form.name.data, form.description.data, current_user.id, form.location.data)
db.session.add(stall)
db.session.commit()
return redirect(url_for('stalls'))
else:
flash_form_errors(form)
return redirect(url_for('new_stall'))
return render_template('newstall.html', form=form)
@app.route('/stalls/<int:stall_id>/edit', methods=['GET', 'POST'])
@login_required
@stall_validate
def edit_stall(stall_id):
stall = Stall.query.get(stall_id)
form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
if request.method == 'POST':
if form.validate(editing=True):
stall.name = form.name.data
stall.description = form.description.data
stall.location_id = form.location.data
db.session.commit()
return redirect(url_for('stalls'))
else:
flash_form_errors(form)
return redirect(url_for('edit_stall', values=[('stall_id', stall_id)]))
else:
form.name.data = stall.name
form.description.data = stall.description
form.location.data = stall.location_id
return render_template('editstall.html', form=form, stall=stall)
@app.route('/stalls/<int:stall_id>/delete', methods=['POST'])
@login_required
@stall_validate
def delete_stall(stall_id):
stall = Stall.query.get(stall_id)
db.session.delete(stall)
db.session.commit()
return redirect(url_for('stalls'))
\ No newline at end of file
...@@ -3,9 +3,8 @@ from functools import wraps ...@@ -3,9 +3,8 @@ 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
from forms import OwnerLoginForm, OwnerRegisterForm, StallRegisterForm, DishRegisterForm from forms import OwnerLoginForm, OwnerRegisterForm
from models import Owner, Stall, Location, Dish from models import Owner, Stall, Dish
from werkzeug.utils import secure_filename
@app.route('/') @app.route('/')
def index(): def index():
...@@ -76,128 +75,6 @@ def dish_validate(f): ...@@ -76,128 +75,6 @@ def dish_validate(f):
return f(*args, **kwargs) return f(*args, **kwargs)
return wrapper return wrapper
@app.route('/stalls')
@login_required
def stalls():
return render_template('stalls.html', owner=current_user, stalls=current_user.stalls.all())
@app.route('/stalls/<int:stall_id>')
@login_required
@stall_validate
def stall(stall_id):
stall = Stall.query.get(stall_id)
dishes = stall.dishes.all()
return render_template('stall.html', stall=stall, dishes=dishes, upload_folder=app.config['UPLOAD_FOLDER'])
@app.route('/stalls/new', methods=['GET','POST'])
@login_required
def new_stall():
form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
if request.method == 'POST':
if form.validate():
stall = Stall(form.name.data, form.description.data, current_user.id, form.location.data)
db.session.add(stall)
db.session.commit()
return redirect(url_for('stalls'))
else:
flash_form_errors(form)
return redirect(url_for('new_stall'))
return render_template('newstall.html', form=form)
@app.route('/stalls/<int:stall_id>/edit', methods=['GET', 'POST'])
@login_required
@stall_validate
def edit_stall(stall_id):
stall = Stall.query.get(stall_id)
form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
if request.method == 'POST':
if form.validate(editing=True):
stall.name = form.name.data
stall.description = form.description.data
stall.location_id = form.location.data
db.session.commit()
return redirect(url_for('stalls'))
else:
flash_form_errors(form)
return redirect(url_for('edit_stall', values=[('stall_id', stall_id)]))
else:
form.name.data = stall.name
form.description.data = stall.description
form.location.data = stall.location_id
return render_template('editstall.html', form=form, stall=stall)
@app.route('/stalls/<int:stall_id>/delete', methods=['POST'])
@login_required
@stall_validate
def delete_stall(stall_id):
stall = Stall.query.get(stall_id)
db.session.delete(stall)
db.session.commit()
return redirect(url_for('stalls'))
@app.route('/stalls/<int:stall_id>/dishes/new', methods=['GET','POST'])
@login_required
@stall_validate
def new_dish(stall_id):
stall = Stall.query.get(stall_id)
form = DishRegisterForm()
if request.method == 'POST':
if form.validate():
filename = secure_filename(form.image.data.filename)
dish = Dish(form.name.data, form.description.data, form.price.data, stall.id, filename)
form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
db.session.add(dish)
db.session.commit()
return redirect(url_for('stall', stall_id=stall.id))
else:
flash_form_errors(form)
return redirect(url_for('new_dish', stall_id=stall.id))
else:
return render_template('newdish.html', form=form, stall=stall)
@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
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))
db.session.commit()
return redirect(url_for('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'])
@login_required
@dish_validate
def delete_dish(stall_id, dish_id):
dish = Dish.query.get(dish_id)
db.session.delete(dish)
db.session.commit()
return redirect(url_for('stall', stall_id=stall_id))
@login_manager.user_loader @login_manager.user_loader
def load_user(owner_id): def load_user(owner_id):
return Owner.query.filter_by(id=int(owner_id)).first() return Owner.query.filter_by(id=int(owner_id)).first()
...@@ -211,3 +88,6 @@ def flash_form_errors(form): ...@@ -211,3 +88,6 @@ def flash_form_errors(form):
for field, errors in form.errors.items(): for field, errors in form.errors.items():
for error in errors: for error in errors:
flash(u'Error in the %s field - %s' % (getattr(form, field).label.text, error)) flash(u'Error in the %s field - %s' % (getattr(form, field).label.text, error))
import stall_views
import dish_views
\ No newline at end of file
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