Commit 76154157 authored by Willard's avatar Willard

Implement stall methods in a MethodView

parent 4bdab473
......@@ -2,7 +2,7 @@
{% block title %}Edit Stall Info{% endblock %}
{% block content %}
<h1>Stall Info</h1>
<form action="{{ url_for('edit_stall', stall_id=stall.id) }}" method="POST">
<form action="{{ url_for('stall', stall_id=stall.id) }}" method="POST">
{{ form.csrf_token }}
<div id="new-stall-form">
<div class="form-input">
......@@ -17,6 +17,7 @@
</div>
</div>
<div class="dashboard-row">
<input type="hidden" name="_method" value="PUT">
<button type="submit">Submit</button>
<a href="{{ url_for('stall', stall_id=stall.id) }}"><button type="button">Back</button></a>
</div>
......
......@@ -2,7 +2,7 @@
{% block title %}Register your Stall{% endblock %}
{% block content %}
<h1>Stall Info</h1>
<form action="{{url_for('new_stall')}}" method="POST">
<form action="{{url_for('stalls')}}" method="POST">
{{ form.csrf_token }}
<div id="new-stall-form">
<div class="form-input">
......
......@@ -19,7 +19,8 @@
</div>
<div class="dashboard-row">
<a href="{{ url_for('stall', stall_id=stall.id) }}"><button class="list-button">Manage Stall</button></a>
<form action="{{ url_for('delete_stall', stall_id=stall.id) }}" method="POST" onsubmit="return confirm('Are you sure you want to delete {{stall.name}}?');">
<form action="{{ url_for('stall', stall_id=stall.id) }}" method="POST" onsubmit="return confirm('Are you sure you want to delete {{stall.name}}?');">
<input type="hidden" name="_method" value="DELETE" >
<button type="submit" class="list-button button-delete">Delete</button>
</form>
</div>
......
import os.path
from functools import wraps
from canteeneo import app, login_manager, db
from flask import flash, redirect, render_template, request, url_for
from flask.views import MethodView
from flask_login import login_required, login_user, logout_user, current_user
from forms import OwnerLoginForm, OwnerRegisterForm, StallRegisterForm, DishRegisterForm
from models import Owner, Stall, Location, Dish
......@@ -51,27 +53,34 @@ def register():
return render_template('register.html', form=form)
@app.route('/stalls')
@login_required
def stalls():
return render_template('stalls.html', owner=current_user, stalls=current_user.stalls.all())
class StallView(MethodView):
def valid_stall(f):
@wraps(f)
def decorated_function(*args, **kwargs):
stall_id = kwargs.get('stall_id')
if stall_id is None:
return f(*args, **kwargs)
@app.route('/stalls/<int:stall_id>')
@login_required
def stall(stall_id):
stall = Stall.query.filter_by(id=int(stall_id)).first()
if stall is None or stall.owner != current_user:
return redirect(url_for('stalls'))
return f(*args, **kwargs)
return decorated_function
decorators = [login_required, valid_stall]
def get(self, stall_id):
if stall_id is None:
return render_template('stalls.html', owner=current_user, stalls=current_user.stalls.all())
else:
dishes = stall.dishes.all()
return render_template('stall.html', stall=stall, dishes=dishes, upload_folder=app.config['UPLOAD_FOLDER'])
@app.route('/stalls/new', methods=['GET','POST'])
@login_required
def new_stall():
def post(self):
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)
db.session.add(stall)
......@@ -81,19 +90,9 @@ def new_stall():
flash_form_errors(form)
return redirect(url_for('new_stall'))
return render_template('newstall.html', form=form)
@app.route('/stalls/<int:stall_id>/edit', methods=['GET', 'POST'])
@login_required
def edit_stall(stall_id):
stall = Stall.query.filter_by(id=int(stall_id)).first()
if stall is None or stall.owner != current_user:
return redirect(url_to('stalls'))
def put(self, stall_id):
form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
if request.method == 'POST':
if form.validate(editing=True):
stall.name = form.name.data
stall.description = form.description.data
......@@ -103,25 +102,39 @@ def edit_stall(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)
def delete(self, stall_id):
db.session.delete(stall)
db.session.commit()
return redirect(url_for('stalls'))
stalls = StallView.as_view('stalls')
stall = StallView.as_view('stall')
app.add_url_rule('/stalls/', view_func=stalls, defaults={'stall_id': None}, methods=['GET', 'POST'])
app.add_url_rule('/stalls/', view_func=stalls, methods=['POST'])
app.add_url_rule('/stalls/<int:stall_id>', view_func=stall, methods=['GET', 'PUT', 'DELETE'])
@app.route('/stalls/<int:stall_id>/delete', methods=['POST'])
@app.route('/stalls/new', methods=['GET'])
@login_required
def delete_stall(stall_id):
stall = Stall.query.filter_by(id=int(stall_id)).first()
def new_stall():
form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
return render_template('newstall.html', form=form)
@app.route('/stalls/<int:stall_id>/edit', methods=['GET'])
@login_required
def edit_stall(stall_id):
stall = Stall.query.filter_by(id=int(stall_id)).first()
if stall is None or stall.owner != current_user:
return redirect(url_for('stalls'))
return redirect(url_to('stalls'))
db.session.delete(stall)
db.session.commit()
form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
form.name.data = stall.name
form.description.data = stall.description
form.location.data = stall.location_id
return redirect(url_for('stalls'))
return render_template('editstall.html', form=form, stall=stall)
@app.route('/stalls/<int:stall_id>/dish/new', methods=['GET','POST'])
@login_required
......
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