Commit 976825e0 authored by Willard's avatar Willard

Implement stall views as View and FormView classes

parent 03656886
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>') app.add_url_rule('/stalls', view_func=StallListView.as_view('stalls'), methods=['GET'])
@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'])
@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) app.add_url_rule('/stalls/<int:stall_id>', view_func=StallView.as_view('view_stall'), methods=['GET'])
@app.route('/stalls/<int:stall_id>/edit', methods=['GET', 'POST']) class NewStallView(FormView):
@login_required decorators = [login_required]
@stall_validate def render_post(self):
def edit_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)
form = StallRegisterForm() db.session.add(stall)
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()] db.session.commit()
form.editing = True return redirect(url_for('view_stall', stall_id=stall.id))
def render_get(self):
if request.method == 'POST': return render_template('newstall.html', form=self.get_form())
if form.validate(): def get_form(self):
stall.name = form.name.data form = StallRegisterForm()
stall.description = form.description.data form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
stall.location_id = form.location.data return form
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/new', view_func=NewStallView.as_view('new_stall'))
@app.route('/stalls/<int:stall_id>/delete', methods=['POST']) class EditStallView(FormView):
@login_required decorators = [login_required, stall_validate]
@stall_validate def dispatch_request(self, stall_id):
def delete_stall(stall_id): self.stall = Stall.query.get(stall_id)
stall = Stall.query.get(stall_id) return FormView.dispatch_request(self, stall_id)
db.session.delete(stall) def render_post(self, stall_id):
db.session.commit() form = self.get_form()
return redirect(url_for('view_stall')) self.stall.name = form.name.data
\ No newline at end of file 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.add_url_rule('/stalls/<int:stall_id>/edit', view_func=EditStallView.as_view('edit_stall'))
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)
app.add_url_rule('/stalls/<int:stall_id>/delete', view_func=DeleteStallView.as_view('delete_stall'), methods=['POST'])
\ 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