Commit db9ce1d4 authored by Willard's avatar Willard

Merge pluggable to master

parents 85d322b3 d519beb9
......@@ -3,70 +3,67 @@ 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 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
@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('view_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)
class NewDishView(FormView):
decorators = [login_required, stall_validate]
def dispatch_request(self, stall_id):
self.stall = Stall.query.get(stall_id)
return FormView.dispatch_request(self, stall_id)
def render_post(self, stall_id):
filename = secure_filename(form.image.data.filename)
dish = Dish(form.name.data, form.description.data, form.price.data, self.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('view_stall', stall_id=stall.id))
def render_get(self, stall_id):
return render_template('newdish.html', form=self.get_form(), stall=self.stall)
def get_form(self):
return DishRegisterForm()
@app.route('/stalls/<int:stall_id>/dishes/<int:dish_id>', methods=["GET"])
@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))
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
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('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)
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'':
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))
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):
dish = Dish.query.get(dish_id)
db.session.delete(dish)
db.session.commit()
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))
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'])
......@@ -10,15 +10,15 @@ class OwnerLoginForm(FlaskForm):
def validate(self):
if not FlaskForm.validate(self):
return False, None
return False
owner = Owner.query.filter_by(username=self.username.data).first()
if owner is None:
flash('User does not exist!')
return False, None
return False
if not owner.check_password(self.password.data):
flash('Wrong password!')
return False, None
return False
return True, owner
......
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 canteeneo.views import flash_form_errors, stall_validate, FormView
from flask import render_template, redirect, url_for, request
from flask.views import View
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())
class StallListView(View):
decorators = [login_required]
def dispatch_request(self):
return render_template('stalls.html', owner=current_user, stalls=current_user.stalls.all())
@app.route('/stalls/<int:stall_id>')
@login_required
@stall_validate
def view_stall(stall_id):
stall = Stall.query.get(stall_id)
dishes = stall.dishes.all()
return render_template('viewstall.html', stall=stall, dishes=dishes, upload_folder=app.config['UPLOAD_FOLDER'])
class NewStallView(FormView):
decorators = [login_required]
def render_post(self):
form = self.get_form()
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('view_stall', stall_id=stall.id))
def render_get(self):
return render_template('newstall.html', form=self.get_form())
def get_form(self):
form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
return form
@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('view_stall', stall_id=stall.id))
else:
flash_form_errors(form)
return redirect(url_for('new_stall'))
class StallView(View):
decorators = [login_required, stall_validate]
def dispatch_request(self, stall_id):
stall = Stall.query.get(stall_id)
dishes = stall.dishes.all()
return render_template('viewstall.html', stall=stall, dishes=dishes, upload_folder=app.config['UPLOAD_FOLDER'])
return render_template('newstall.html', form=form)
class EditStallView(FormView):
decorators = [login_required, stall_validate]
def dispatch_request(self, stall_id):
self.stall = Stall.query.get(stall_id)
return FormView.dispatch_request(self, stall_id)
def render_post(self, stall_id):
form = self.get_form()
self.stall.name = form.name.data
self.stall.description = form.description.data
self.stall.location_id = form.location.data
db.session.connect()
return redirect(url_for('view_stall', stall_id=stall_id))
def render_get(self, stall_id):
form = self.get_form()
form.name.data = self.stall.name
form.description.data = self.stall.description
form.location.data = self.stall.location_id
return render_template('editstall.html', form=form, stall=stall)
def get_form(self):
form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
form.editing = True
return 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()]
form.editing = True
if request.method == 'POST':
if form.validate():
stall.name = form.name.data
stall.description = form.description.data
stall.location_id = form.location.data
db.session.commit()
return redirect(url_for('view_stall', stall_id=stall_id))
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
class DeleteStallView(View):
decorators = [login_required, stall_validate]
def dispatch_request(self, stall_id):
stall = Stall.query.get(stall_id)
db.session.delete(stall)
db.session.commit()
return redirect(url_for('view_stall'), stall_id=stall_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('view_stall', stall_id=stall_id))
app.add_url_rule('/stalls', view_func=StallListView.as_view('stalls'), methods=['GET'])
app.add_url_rule('/stalls/new', view_func=NewStallView.as_view('new_stall'))
app.add_url_rule('/stalls/<int:stall_id>', view_func=StallView.as_view('view_stall'), methods=['GET'])
app.add_url_rule('/stalls/<int:stall_id>/edit', view_func=EditStallView.as_view('edit_stall'))
app.add_url_rule('/stalls/<int:stall_id>/delete', view_func=DeleteStallView.as_view('delete_stall'), methods=['POST'])
......@@ -2,55 +2,71 @@ import os.path
from functools import wraps
from canteeneo import app, login_manager, db
from flask import flash, redirect, render_template, request, url_for
from flask.views import View
from flask_login import login_required, login_user, logout_user, current_user
from forms import OwnerLoginForm, OwnerRegisterForm
from models import Owner, Stall, Dish
@app.route('/')
def index():
if current_user.is_authenticated:
return redirect(url_for('stalls'))
else:
return render_template('landing.html')
class FormView(View):
methods=['GET', 'POST']
def dispatch_request(self, **kwargs):
form = self.get_form()
if request.method == 'POST':
if form.validate():
return self.render_post(**kwargs)
flash_form_errors(form)
return self.render_get(**kwargs)
def get_form(self):
raise NotImplementedError()
def render_post(self, **kwargs):
return ''
def render_get(self, **kwargs):
return ''
def not_logged_in(f):
@wraps(f)
def wrapper(*args, **kwargs):
if current_user.is_authenticated:
return redirect(url_for('stalls'))
return f(*args, **kwargs)
return wrapper
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
class LoginView(FormView):
decorators=[not_logged_in]
def render_post(self):
login_user(Owner.query.filter_by(username=self.get_form().username.data).first())
return redirect(url_for('stalls'))
def render_get(self):
return render_template('login.html', form=self.get_form())
def get_form(self):
return OwnerLoginForm()
form = OwnerLoginForm()
if request.method == 'POST':
is_valid, owner = form.validate()
if is_valid:
login_user(owner)
return redirect(url_for('stalls'))
else:
flash_form_errors(form)
class RegisterView(FormView):
decorators=[not_logged_in]
def render_post(self):
form = self.get_form()
owner = Owner(form.name.data, form.username.data, form.email.data, form.password.data)
db.session.add(owner)
db.session.commit()
return redirect(url_for('login'))
def render_get(self):
return render_template('register.html', form=form)
def get_form(self):
return OwnerRegisterForm()
app.add_url_rule('/login', view_func=LoginView.as_view('login'))
app.add_url_rule('/register', view_func=RegisterView.as_view('register'))
return render_template('login.html', form=form)
@app.route('/')
@not_logged_in
def index():
return render_template('landing.html')
@app.route('/logout')
def logout():
logout_user()
return redirect('/')
@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('stalls'))
form = OwnerRegisterForm()
if request.method == 'POST':
if form.validate():
owner = Owner(form.name.data, form.username.data, form.email.data, form.password.data)
db.session.add(owner)
db.session.commit()
return redirect(url_for('login'))
else:
flash_form_errors(form)
return render_template('register.html', form=form)
def stall_validate(f):
@wraps(f)
def wrapper(*args, **kwargs):
......
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