Commit 48e5fd07 authored by Willard's avatar Willard

Implement FormView, rewrite new_dish using it

parent 121466d5
...@@ -3,30 +3,30 @@ import os.path ...@@ -3,30 +3,30 @@ 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_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: app.add_url_rule('/stalls/<int:stall_id>/dishes/new', view_func=NewDishView.as_view('new_dish'))
return render_template('newdish.html', form=form, stall=stall)
@app.route('/stalls/<int:stall_id>/dishes/<int:dish_id>', methods=["GET"]) @app.route('/stalls/<int:stall_id>/dishes/<int:dish_id>', methods=["GET"])
@login_required @login_required
......
...@@ -2,10 +2,27 @@ import os.path ...@@ -2,10 +2,27 @@ 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
class FormView(View):
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)
elif request.method == 'GET':
return self.render_get(**kwargs)
def get_form(self):
raise NotImplementedError()
def render_post(self, **kwargs):
return ''
def render_get(self, **kwargs):
return ''
@app.route('/') @app.route('/')
def index(): def index():
if current_user.is_authenticated: if current_user.is_authenticated:
......
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