Ver código fonte

Changes admin_insert to use WTForms

Jose Reyes 6 anos atrás
pai
commit
1923ebcf79
4 arquivos alterados com 22 adições e 14 exclusões
  1. 8
    6
      app/admin_insert.py
  2. 6
    1
      app/forms.py
  3. 7
    6
      app/templates/admin_insert.html
  4. 1
    1
      app/templates/index.html

+ 8
- 6
app/admin_insert.py Ver arquivo

@@ -4,22 +4,24 @@ from flask import (
4 4
 from werkzeug.exceptions import abort
5 5
 
6 6
 from app.db import get_db
7
+from app.forms import StationForm
7 8
 
8 9
 bp = Blueprint('admin_insert', __name__)
9 10
 
10 11
 @bp.route('/admin_insert', methods=('GET', 'POST'))
11 12
 def admin():
12
-    if request.method == 'POST':
13
-        eName = request.form['eName']
14
-        office = request.form['oficina']
15
-
13
+    form = StationForm()
14
+    if form.validate_on_submit():
16 15
         db = get_db()
17 16
         db.execute(
18 17
             'INSERT INTO station (nombre_empleado, oficina, last_turn)'
19 18
             ' VALUES (?, ?, ?)',
20
-            (eName, office, 0)
19
+            (form.eName.data, form.office.data, 0)
21 20
         )
22 21
         db.commit()
22
+
23
+        flash('Added station for  {} ({})'.format(form.eName.data, form.office.data))
24
+
23 25
         return redirect(url_for('admin_insert'))
24 26
 
25
-    return render_template('admin_insert.html')
27
+    return render_template('admin_insert.html', form=form)

+ 6
- 1
app/forms.py Ver arquivo

@@ -5,4 +5,9 @@ from wtforms.validators import DataRequired
5 5
 class TurnForm(FlaskForm):
6 6
     cName = StringField('Name', validators=[DataRequired()])
7 7
     cEmail = StringField('Email', validators=[DataRequired()])
8
-    submit = SubmitField('Get turn')
8
+    submit = SubmitField('Get turn')
9
+
10
+class StationForm(FlaskForm):
11
+    eName = StringField('Name', validators=[DataRequired()])
12
+    office = StringField('Office', validators=[DataRequired()])
13
+    submit = SubmitField('Insert Station')

+ 7
- 6
app/templates/admin_insert.html Ver arquivo

@@ -5,11 +5,12 @@
5 5
 {% endblock %}
6 6
 
7 7
 {% block content %}
8
-    <form method="post">
9
-        <label for="eName">Name</label>
10
-        <input name="eName" id="name" required></input><br>
11
-        <label for="eName">Oficina</label>
12
-        <input name="oficina" id="oficina" required></input><br>
13
-        <input type="submit" value="Insert Station">
8
+    <form action="" method="post">
9
+        {{ form.hidden_tag() }}
10
+        {{ form.eName.label }}<br>
11
+        {{ form.eName }}<br>
12
+        {{ form.office.label }}<br>
13
+        {{ form.office }}<br>
14
+        {{ form.submit }}
14 15
     </form>
15 16
 {% endblock %}

+ 1
- 1
app/templates/index.html Ver arquivo

@@ -16,6 +16,6 @@
16 16
             <input type="radio" name="station" id={{ "station{}".format(station['s_id']) }} value={{ station['s_id'] }} required> {{ "{} ({})".format(station['nombre_empleado'],station['oficina']) }} </input><br>
17 17
         {% endfor %}
18 18
 
19
-        <input type="submit" value="Get ticket">
19
+        {{ form.submit }}
20 20
     </form>
21 21
 {% endblock %}