Commit cc19865e authored by Mathis Dedial's avatar Mathis Dedial
Browse files

Implement vote view

parent 09e3771f
'''
Contains dictionaries mapping IDs from the burger creator form
to the corresponding ingredients
'''
BREAD = {
1: 'Sesam Bun',
2: 'Brioche Bun',
3: 'Brezel Bun',
4: 'Farmer Bun',
5: 'Red Bun',
6: 'White Bun',
7: 'Black Bun',
8: 'Yellow Bun',
}
PATTY = {
1: 'Beef Burger',
2: 'Pulled Pork',
3: 'Poulet Burger',
4: 'Chicken Katsu',
5: 'Vegane Burger',
6: 'Vegetable Burger',
7: 'Quinoa Chili Burger',
8: 'Falafel Burger',
}
TOPPINGS = {
1: 'Lettuce',
2: 'Cole Slaw',
3: 'Cheddar',
4: 'Feta',
5: 'Onions',
6: 'Roasted Onions',
7: 'Avocado',
8: 'Jalapenos',
9: 'Olives',
10: 'Tomato',
11: 'Pickle',
12: 'Rucola',
13: 'Egg',
14: 'Bacon',
15: 'Chorizo',
16: 'Grilled Vegetables',
17: 'Guacamole',
18: 'Honey-Mustard Sauce',
19: 'Hot-Ketchup',
20: 'Truffle Mayonnaise',
21: 'Cocktail Sauce',
22: 'Tartare Sauce',
23: 'BBQ Sauce',
24: 'Pesto',
}
SIDE = {
1: 'Pommes Frites',
2: 'Country Cuts',
3: 'Chips',
4: 'Onion Rings',
}
......@@ -23,13 +23,20 @@
<body>
<div id="container">
<h1>{% block title %}{% endblock %}</h1>
{% with messages = get_flashed_messages() %}
{% with messages = get_flashed_messages(category_filter=['error']) %}
{% for message in messages %}
<div class="alert alert-danger" role="alert">
{{ message }}
</div>
{% endfor %}
{% endwith %}
{% with messages = get_flashed_messages(category_filter=['success']) %}
{% for message in messages %}
<div class="alert alert-success" role="alert">
{{ message }}
</div>
{% endfor %}
{% endwith %}
{% block content %}{% endblock %}
</div>
</body>
......
{% extends 'base.html' %} {% block content %}
<div class="list-group">
{% for burger in burger_list %}
<li class="list-group-item d-flex align-items-center justify-content-between">
<div>
<b>{{ burger.name }}</b>
<br>
<span>{{ burger.description }}</span>
</div>
<div>
<ul>
<li>
<b>Bread:</b> {{ bread.get(burger.bread) }}</li>
<li>
<b>Patty:</b> {{ patty.get(burger.patty) }}</li>
<li>
<b>Toppings:</b>
<ul>
{% for topping in burger.toppings.split(', ') %}
<li>{{ toppings.get(topping|int) }}</li>
{% endfor %}
</ul>
</li>
<li>
<b>Side:</b> {{ side.get(burger.side) }}</li>
</ul>
</div>
<span class="badge badge-success badge-pill">{{ burger.vote_count }}</span>
<form method="POST">
{% if not burger.vote_id %}
<button class="btn btn-success" type="submit">Like</button>
<input type="hidden" name="action" value="like">
{% else %}
<button class="btn btn-danger" type="submit">Unlike</button>
<input type="hidden" name="action" value="unlike">
{% endif %}
<input type="hidden" name="burger_id" value="{{ burger.id }}">
</form>
</li>
{% endfor %}
</div>
{% endblock content %}
\ No newline at end of file
......@@ -3,15 +3,81 @@ Contains all views
'''
from app import app
from datetime import datetime
from .models import db, Burger
from flask import flash, g, redirect, render_template, request, url_for
from sqlalchemy.sql import text
from .mappings import BREAD, PATTY, TOPPINGS, SIDE
from .models import db, Burger, Vote
@app.route('/')
@app.route('/', methods=['GET', 'POST'])
def vote():
'''
Front page. Shows a list of all burgers
'''
pass
def render_list():
'''
Helper function which gets the latest state from the database before rendering the template.
Useful since we don't want to send multiple queries for a single request.
'''
# Fetch list of burgers with corresponding vote info
# Could probably also be done with SQLAlchemy but too lazy to look up how
querystring = text('SELECT burger.id AS id, name, description, burger.nethz AS nethz, timestamp, bread, patty, toppings, side, COUNT(vote_1.id) AS vote_count, vote_2.id AS vote_id '
'FROM burger '
'LEFT JOIN vote AS vote_1 ON vote_1.burger_id=burger.id '
'LEFT JOIN vote AS vote_2 ON vote_2.burger_id=burger.id AND vote_2.nethz = :nethz '
'ORDER BY vote_count ')
burger_list = db.engine.execute(querystring, nethz=g.user).fetchall()
return render_template('vote.html',
burger_list=burger_list,
bread=BREAD,
patty=PATTY,
toppings=TOPPINGS,
side=SIDE)
def die(message):
'''
Display an error-level flash message and return
'''
flash(message, 'error')
return render_list()
# Handle like / unlike actions first
if request.method == 'POST':
# Check if the burger_id is valid
burger_id = request.form.get('burger_id')
if not burger_id or not Burger.query.get(burger_id):
return die('Invalid burger ID')
# User is casting their vote
if request.form.get('action') == 'like':
# Check if the user hasn't already voted for the same burger
if Vote.query.filter_by(nethz=g.user, burger_id=burger_id).first():
return die('You have already liked this burger.')
# Add vote
vote = Vote(
nethz=g.user,
burger_id=burger_id,
)
db.session.add(vote)
db.session.commit()
flash('Thanks for voting!', 'success')
# User is revoking their vote
elif request.form.get('action') == 'unlike':
# Find the vote
vote = Vote.query.filter_by(nethz=g.user, burger_id=burger_id).first()
if vote:
# Delete the vote
db.session.delete(vote)
db.session.commit()
flash('Your like has been removed.', 'success')
return render_list()
@app.route('/new', methods=['GET', 'POST'])
def create():
......@@ -19,9 +85,6 @@ def create():
Burger creator
'''
# TODO: Debug only, remove!!
g.user = 'mdedial'
def die(message):
'''
Display an error-level flash message and return
......@@ -80,10 +143,3 @@ def create():
return redirect(url_for('detail', burger_id=burger.id))
return render_template('creator.html')
@app.route('/detail/<int:burger_id>')
def detail(burger_id):
'''
Detail view of a certain burger
'''
pass
Supports Markdown
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