Commit 84bb3cab authored by Willard's avatar Willard

Revert 76154157

parent 590008f8
......@@ -2,7 +2,7 @@
{% block title %}Edit Stall Info{% endblock %}
{% block content %}
<h1>Stall Info</h1>
<form action="{{ url_for('stall', stall_id=stall.id) }}" method="POST">
<form action="{{ url_for('edit_stall', stall_id=stall.id) }}" method="POST">
{{ form.csrf_token }}
<div id="new-stall-form">
<div class="form-input">
......@@ -17,7 +17,6 @@
</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('stalls')}}" method="POST">
<form action="{{url_for('new_stall')}}" method="POST">
{{ form.csrf_token }}
<div id="new-stall-form">
<div class="form-input">
......
......@@ -19,8 +19,12 @@
</div>
<div class="dashboard-row">
<a href="{{ url_for('stall', stall_id=stall.id) }}"><button class="list-button">Manage Stall</button></a>
<<<<<<< HEAD
<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" />
=======
<form action="{{ url_for('delete_stall', stall_id=stall.id) }}" method="POST" onsubmit="return confirm('Are you sure you want to delete {{stall.name}}?');">
>>>>>>> parent of 7615415... Implement stall methods in a MethodView
<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
......@@ -53,36 +51,27 @@ def register():
return render_template('register.html', form=form)
class StallView(MethodView):
def valid_stall(f):
@wraps(f)
def decorated_function(*args, **kwargs):
stall_id = kwargs.get('stall_id')
kwargs.pop('stall_id')
kwargs['stall'] = None
if stall_id is None:
return f(*args, **kwargs)
stall = Stall.query.filter_by(id=int(stall_id)).first()
if stall is None or stall.owner != current_user:
return redirect(url_for('stalls'))
kwargs['stall'] = stall
return f(*args, **kwargs)
return decorated_function
decorators = [login_required, valid_stall]
def get(self, stall):
if stall 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')
@login_required
def stalls():
return render_template('stalls.html', owner=current_user, stalls=current_user.stalls.all())
def post(self):
form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
@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'))
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():
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)
......@@ -91,10 +80,20 @@ class StallView(MethodView):
else:
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'))
form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()]
def put(self, stall):
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,40 +102,26 @@ class StallView(MethodView):
return redirect(url_for('stalls'))
else:
flash_form_errors(form)
return redirect(url_for('edit_stall', values=[('stall_id', stall.id)]))
def delete(self, stall):
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', 'POST', 'PUT', 'DELETE'])
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.route('/stalls/new', methods=['GET'])
@login_required
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)
return render_template('editstall.html', form=form, stall=stall)
@app.route('/stalls/<int:stall_id>/edit', methods=['GET'])
@app.route('/stalls/<int:stall_id>/delete', methods=['POST'])
@login_required
def edit_stall(stall_id):
def delete_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'))
return redirect(url_for('stalls'))
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
db.session.delete(stall)
db.session.commit()
return render_template('editstall.html', form=form, stall=stall)
return redirect(url_for('stalls'))
@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