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.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():
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):
app.add_url_rule('/stalls', view_func=StallListView.as_view('stalls'), methods=['GET'])
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'])
@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():
app.add_url_rule('/stalls/<int:stall_id>', view_func=StallView.as_view('view_stall'), methods=['GET'])
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))
else:
flash_form_errors(form)
return redirect(url_for('new_stall'))
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
return render_template('newstall.html', form=form)
app.add_url_rule('/stalls/new', view_func=NewStallView.as_view('new_stall'))
@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)
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
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
app.add_url_rule('/stalls/<int:stall_id>/edit', view_func=EditStallView.as_view('edit_stall'))
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):
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'))
\ No newline at end of file
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