Commit 621bf165 authored by Mathis Dedial's avatar Mathis Dedial
Browse files

Implement burger creation server-side

parent 81e21d3e
from flask import Flask
from flask import Flask, g
from .models import db
from flask_simpleldap import LDAP
# Set up Flask app and load configuration
app = Flask(__name__)
app.config.from_pyfile('./config.py')
app.secret_key = app.config['SECRET_KEY']
# Initialize LDAP auth
ldap = LDAP(app)
# Bind SQLAlchemy to Flask app
......
......@@ -2,6 +2,8 @@
Essential app configuration
'''
SECRET_KEY = 'THIS_SHOULD_BE_RANDOM_AND_SECRET'
MYSQL_USERNAME = 'amivburger'
MYSQL_PWD = 'amivburgerpassword'
MYSQL_HOST = 'localhost'
......
......@@ -7,9 +7,12 @@
<title>AMIV Burger</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="static/scripts/script.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="static/styles/image-picker.css">
......@@ -20,6 +23,13 @@
<body>
<div id="container">
<h1>{% block title %}{% endblock %}</h1>
{% with messages = get_flashed_messages() %}
{% for message in messages %}
<div class="alert alert-danger" role="alert">
{{ message }}
</div>
{% endfor %}
{% endwith %}
{% block content %}{% endblock %}
</div>
</body>
......
......@@ -8,7 +8,7 @@
</p>
<!-- SELECT BREAD -->
<form>
<form method="post">
<h3>Bread</h3>
<p class="highlight"></p>
<div class="picker">
......@@ -24,12 +24,12 @@
</select>
</div>
<!-- SELECT PATTIE -->
<h3>Burger Pattie</h3>
<!-- SELECT PATTY -->
<h3>Burger Patty</h3>
<p class="highlight"></p>
<div class="picker">
<select name="pattie" class="image-picker show-labels">
<select name="patty" class="image-picker show-labels">
<option data-img-src="static/images/P1.png" value="1">Beef Burger</option>
<option data-img-src="static/images/P2.png" value="2">Pulled Pork</option>
<option data-img-src="static/images/P3.png" value="3">Poulet Burger</option>
......@@ -76,7 +76,7 @@
</div>
<!-- SELECT SIDE -->
<h3>Select Side</h3>
<h3>Side</h3>
<p class="highlight"></p>
<div class="picker">
<select name="side" class="image-picker show-labels">
......@@ -86,11 +86,21 @@
<option data-img-src="static/images/S4.png" value="4">Onion Rings</option>
</select>
</div>
<h3>Name</h3>
<h3>Name &amp; Description</h3>
<p class="highlight"></p>
<div>
<input type="text">
<div class="form-group">
<label for="input_name">
Name
</label>
<input class="form-control" id="input_name" name="name" type="text">
</div>
<div class="form-group">
<label for="input_description">
Description
</label>
<input class="form-control" id="input_description" name="description" type="text">
</div>
<input type="submit">
<input class="btn btn-primary" type="submit">
</form>
{% endblock %}
......@@ -2,8 +2,88 @@
Contains all views
'''
from app import app
from flask import render_template
from datetime import datetime
from .models import db, Burger
from flask import flash, g, redirect, render_template, request, url_for
@app.route('/')
def index():
def vote():
'''
Front page. Shows a list of all burgers
'''
pass
@app.route('/new', methods=['GET', 'POST'])
def create():
'''
Burger creator
'''
# TODO: Debug only, remove!!
g.user = 'mdedial'
def die(message):
'''
Display an error-level flash message and return
'''
flash(message, 'error')
return render_template('creator.html')
if request.method == 'POST':
# A new burger was submitted
# Perform field validation
bread = request.form.get('bread')
if not bread:
return die('Please select a bread for your burger.')
patty = request.form.get('patty')
if not patty:
return die('Please select a patty for your burger.')
toppings = request.form.getlist('toppings')
if not toppings:
return die('Please select at least one topping for your burger.')
side = request.form.get('side')
if not side:
return die('Please select a side for your burger.')
name = request.form.get('name')
if not name:
return die('Please give your burger a name.')
description = request.form.get('description')
if not description:
return die('Please describe your burger in a few short sentences.')
# Check that the user hasn't already created a burger
if Burger.query.filter_by(nethz=g.user).first():
return die('You can only create one burger.')
# All set, now save the burger in the database
burger = Burger(
name=name,
description=description,
nethz=g.user,
timestamp=datetime.now(),
bread=bread,
patty=patty,
toppings=', '.join(toppings),
side=side,
)
db.session.add(burger)
db.session.commit()
# Redirect the user to the detail page of the new burger
flash('Congrats on your new burger!', 'success')
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