views.py 2.21 KB
Newer Older
Mathis Dedial's avatar
Mathis Dedial committed
1
2
3
'''
Contains all views
'''
Sandro Lutz's avatar
Sandro Lutz committed
4
from app import app
5
6
7
from datetime import datetime
from .models import db, Burger
from flask import flash, g, redirect, render_template, request, url_for
Sandro Lutz's avatar
Sandro Lutz committed
8
9

@app.route('/')
10
11
12
13
def vote():
  '''
  Front page. Shows a list of all burgers 
  '''
Sandro Lutz's avatar
Sandro Lutz committed
14
  return render_template('index.html')
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

@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))

Sandro Lutz's avatar
Sandro Lutz committed
82
  return render_template('creator.html')
83
84
85
86
87
88
89

@app.route('/detail/<int:burger_id>')
def detail(burger_id):
  '''
  Detail view of a certain burger
  '''
  pass