Browse Source

Changes plain forms to WTForms and cleans up some of index.py

Jose Reyes 6 years ago
parent
commit
1f4b1416d9
3 changed files with 30 additions and 22 deletions
  1. 8
    0
      app/forms.py
  2. 15
    16
      app/index.py
  3. 7
    6
      app/templates/index.html

+ 8
- 0
app/forms.py View File

@@ -0,0 +1,8 @@
1
+from flask_wtf import FlaskForm
2
+from wtforms import StringField, PasswordField, BooleanField, SubmitField
3
+from wtforms.validators import DataRequired
4
+
5
+class TurnForm(FlaskForm):
6
+    cName = StringField('Name', validators=[DataRequired()])
7
+    cEmail = StringField('Email', validators=[DataRequired()])
8
+    submit = SubmitField('Get turn')

+ 15
- 16
app/index.py View File

@@ -6,39 +6,38 @@ from flask import (
6 6
 from werkzeug.exceptions import abort
7 7
 
8 8
 from app.db import get_db
9
+from app.forms import TurnForm
9 10
 
10 11
 bp = Blueprint('index', __name__)
11 12
 
12 13
 @bp.route('/', methods=('GET', 'POST'))
13 14
 def index():
14
-    if request.method == 'POST':
15
-        cName = request.form['cName']
16
-        cEmail = request.form['cEmail']
17
-        station_name = request.form['station']
15
+    form = TurnForm()
16
+    if form.validate_on_submit():
17
+        station_id = request.form['station']
18 18
 
19 19
         db = get_db()
20
-        station_query_res = db.execute(
21
-            'SELECT s_id FROM station WHERE nombre_empleado=?',
22
-            (station_name,)
23
-        ).fetchone()
24
-        if station_query_res is not None:
25
-            station_id = station_query_res[0]
26 20
         db.execute(
27 21
             'UPDATE station SET last_turn = last_turn + 1 WHERE s_id=?',
28 22
             (station_id,)
29 23
         )
30
-        turn_query_res = db.execute(
24
+        turn = db.execute(
31 25
             'SELECT last_turn FROM station WHERE s_id=?',
32 26
             (station_id,)
33
-        ).fetchone()
34
-        if turn_query_res is not None:
35
-            turn = turn_query_res[0]
27
+        ).fetchone()[0]
36 28
         db.execute(
37 29
             'INSERT INTO turno (cName, cEmail, turn, timeArrival, station)'
38 30
             ' VALUES (?, ?, ?, ?, ?)',
39
-            (cName, cEmail, turn, '{0:%H:%M}'.format(datetime.now()), station_id)
31
+            (form.cName.data, form.cEmail.data, turn, '{0:%H:%M}'.format(datetime.now()), station_id)
40 32
         )
33
+        station_info = db.execute(
34
+            'SELECT nombre_empleado, oficina FROM station WHERE s_id=?',
35
+            (station_id,)
36
+        ).fetchone()
41 37
         db.commit()
38
+
39
+        flash('Your turn for {} ({}) is number {}'.format(station_info['nombre_empleado'], station_info['oficina'],  turn))
40
+
42 41
         return redirect(url_for('index'))
43 42
 
44 43
     db = get_db()
@@ -46,4 +45,4 @@ def index():
46 45
         'SELECT s_id, nombre_empleado, oficina FROM station'
47 46
         ' ORDER BY oficina ASC'
48 47
     ).fetchall()
49
-    return render_template('index.html', stations=stations)
48
+    return render_template('index.html', stations=stations, form=form)

+ 7
- 6
app/templates/index.html View File

@@ -5,14 +5,15 @@
5 5
 {% endblock %}
6 6
 
7 7
 {% block content %}
8
-    <form method="post">
9
-        <label for="cName">Name</label>
10
-        <input name="cName" id="name" required></input><br>
11
-		<label for="cEmail">email</label>
12
-        <input name="cEmail" id="email" required></input><br>
8
+    <form action="" method="post">
9
+        {{ form.hidden_tag() }}
10
+        {{ form.cName.label }}<br>
11
+        {{ form.cName }}<br>
12
+        {{ form.cEmail.label }}<br>
13
+        {{ form.cEmail }}<br>
13 14
 
14 15
         {% for station in stations %}
15
-            <input type="radio" name="station" id={{ "station{}".format(station['s_id']) }} value={{ station['nombre_empleado'] }}> {{ "{} ({})".format(station['nombre_empleado'],station['oficina']) }} </input><br>
16
+            <input type="radio" name="station" id={{ "station{}".format(station['s_id']) }} value={{ station['s_id'] }}> {{ "{} ({})".format(station['nombre_empleado'],station['oficina']) }} </input><br>
16 17
         {% endfor %}
17 18
 
18 19
         <input type="submit" value="Get ticket">