Commit db9ce1d4 authored by Willard's avatar Willard

Merge pluggable to master

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