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': filename = secure_filename(form.image.data.filename)
if form.validate(): dish = Dish(form.name.data, form.description.data, form.price.data, self.stall.id, filename)
filename = secure_filename(form.image.data.filename) form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
dish = Dish(form.name.data, form.description.data, form.price.data, stall.id, filename) db.session.add(dish)
form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) db.session.commit()
db.session.add(dish) return redirect(url_for('view_stall', stall_id=stall.id))
db.session.commit() def render_get(self, stall_id):
return redirect(url_for('view_stall', stall_id=stall.id)) return render_template('newdish.html', form=self.get_form(), stall=self.stall)
else: def get_form(self):
flash_form_errors(form) return DishRegisterForm()
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>', 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 if form.image.data.filename is not u'':
dish.price = form.price.data self.dish.image_path = secure_filename(form.image.data.filename)
if form.image.data.filename is not u'': form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], self.dish.image_path))
dish.image_path = secure_filename(form.image.data.filename) db.session.commit()
form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], dish.image_path)) return redirect(url_for('view_stall', stall_id=stall.id))
db.session.commit() def render_get(self, stall_id, dish_id):
return redirect(url_for('view_stall', stall_id=stall_id)) form.name.data = self.dish.name
else: form.description.data = self.dish.description
flash_form_errors(form) form.price.data = self.dish.price
return redirect(url_for('edit_dish', stall_id=stall_id, dish_id=dish_id)) return render_template('editdish.html', form=form, stall=self.stall, dish=self.dish)
else: def get_form(self):
form.name.data = dish.name return DishRegisterForm()
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'))
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) stall = Stall(form.name.data, form.description.data, current_user.id, form.location.data)
dishes = stall.dishes.all() db.session.add(stall)
return render_template('viewstall.html', stall=stall, dishes=dishes, upload_folder=app.config['UPLOAD_FOLDER']) 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']) class StallView(View):
@login_required decorators = [login_required, stall_validate]
def new_stall(): def dispatch_request(self, stall_id):
form = StallRegisterForm() stall = Stall.query.get(stall_id)
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()] dishes = stall.dishes.all()
if request.method == 'POST': return render_template('viewstall.html', stall=stall, dishes=dishes, upload_folder=app.config['UPLOAD_FOLDER'])
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'))
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']) class DeleteStallView(View):
@login_required decorators = [login_required, stall_validate]
@stall_validate def dispatch_request(self, stall_id):
def edit_stall(stall_id): stall = Stall.query.get(stall_id)
stall = Stall.query.get(stall_id) db.session.delete(stall)
form = StallRegisterForm() db.session.commit()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()] return redirect(url_for('view_stall'), stall_id=stall_id)
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
return render_template('editstall.html', form=form, stall=stall) 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.route('/stalls/<int:stall_id>/delete', methods=['POST']) app.add_url_rule('/stalls/<int:stall_id>', view_func=StallView.as_view('view_stall'), methods=['GET'])
@login_required app.add_url_rule('/stalls/<int:stall_id>/edit', view_func=EditStallView.as_view('edit_stall'))
@stall_validate app.add_url_rule('/stalls/<int:stall_id>/delete', view_func=DeleteStallView.as_view('delete_stall'), methods=['POST'])
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))
...@@ -2,55 +2,71 @@ import os.path ...@@ -2,55 +2,71 @@ 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: if request.method == 'POST':
return render_template('landing.html') 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']) class LoginView(FormView):
def login(): decorators=[not_logged_in]
if current_user.is_authenticated: def render_post(self):
login_user(Owner.query.filter_by(username=self.get_form().username.data).first())
return redirect(url_for('stalls')) 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() class RegisterView(FormView):
if request.method == 'POST': decorators=[not_logged_in]
is_valid, owner = form.validate() def render_post(self):
if is_valid: form = self.get_form()
login_user(owner) owner = Owner(form.name.data, form.username.data, form.email.data, form.password.data)
return redirect(url_for('stalls')) db.session.add(owner)
else: db.session.commit()
flash_form_errors(form) 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') @app.route('/logout')
def logout(): def logout():
logout_user() logout_user()
return redirect('/') 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): def stall_validate(f):
@wraps(f) @wraps(f)
def wrapper(*args, **kwargs): 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