Commit 84bb3cab authored by Willard's avatar Willard

Revert 76154157

parent 590008f8
...@@ -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('stall', stall_id=stall.id) }}" method="POST"> <form action="{{ url_for('edit_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,7 +17,6 @@ ...@@ -17,7 +17,6 @@
</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('stalls')}}" method="POST"> <form action="{{url_for('new_stall')}}" 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,8 +19,12 @@ ...@@ -19,8 +19,12 @@
</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>
<<<<<<< HEAD
<form action="{{ url_for('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" /> <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> <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
...@@ -53,36 +51,27 @@ def register(): ...@@ -53,36 +51,27 @@ def register():
return render_template('register.html', form=form) return render_template('register.html', form=form)
class StallView(MethodView): @app.route('/stalls')
def valid_stall(f): @login_required
@wraps(f) def stalls():
def decorated_function(*args, **kwargs): return render_template('stalls.html', owner=current_user, stalls=current_user.stalls.all())
stall_id = kwargs.get('stall_id')
kwargs.pop('stall_id')
kwargs['stall'] = None
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() 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_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() dishes = stall.dishes.all()
return render_template('stall.html', stall=stall, dishes=dishes, upload_folder=app.config['UPLOAD_FOLDER']) return render_template('stall.html', stall=stall, dishes=dishes, upload_folder=app.config['UPLOAD_FOLDER'])
def post(self): @app.route('/stalls/new', methods=['GET','POST'])
@login_required
def new_stall():
form = StallRegisterForm() form = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()] 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)
...@@ -92,9 +81,19 @@ class StallView(MethodView): ...@@ -92,9 +81,19 @@ class StallView(MethodView):
flash_form_errors(form) flash_form_errors(form)
return redirect(url_for('new_stall')) return redirect(url_for('new_stall'))
def put(self, 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 = StallRegisterForm()
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()] 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,40 +102,26 @@ class StallView(MethodView): ...@@ -103,40 +102,26 @@ class StallView(MethodView):
return redirect(url_for('stalls')) return redirect(url_for('stalls'))
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:
def delete(self, stall): form.name.data = stall.name
db.session.delete(stall) form.description.data = stall.description
db.session.commit() form.location.data = stall.location_id
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'])
@app.route('/stalls/new', methods=['GET']) return render_template('editstall.html', form=form, stall=stall)
@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)
@app.route('/stalls/<int:stall_id>/edit', methods=['GET']) @app.route('/stalls/<int:stall_id>/delete', methods=['POST'])
@login_required @login_required
def edit_stall(stall_id): def delete_stall(stall_id):
stall = Stall.query.filter_by(id=int(stall_id)).first() 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_to('stalls')) return redirect(url_for('stalls'))
form = StallRegisterForm() db.session.delete(stall)
form.location.choices = [(loc.id, loc.name) for loc in Location.query.all()] db.session.commit()
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) return redirect(url_for('stalls'))
@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