views.py 4.44 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
from datetime import datetime
from flask import flash, g, redirect, render_template, request, url_for
Mathis Dedial's avatar
Mathis Dedial committed
7
8
9
from sqlalchemy.sql import text
from .mappings import BREAD, PATTY, TOPPINGS, SIDE
from .models import db, Burger, Vote
Sandro Lutz's avatar
Sandro Lutz committed
10

Mathis Dedial's avatar
Mathis Dedial committed
11
@app.route('/', methods=['GET', 'POST'])
12
13
14
15
def vote():
  '''
  Front page. Shows a list of all burgers 
  '''
Mathis Dedial's avatar
Mathis Dedial committed
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

  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()
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

@app.route('/new', methods=['GET', 'POST'])
def create():
  '''
  Burger creator
  '''

  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
145
  return render_template('creator.html')