Commit 76154157 authored by Willard's avatar Willard

Implement stall methods in a MethodView

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