Browse Source

Changes admin_insert to use WTForms

Jose Reyes 6 years ago
parent
commit
1923ebcf79
4 changed files with 22 additions and 14 deletions
  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 View File

4
 from werkzeug.exceptions import abort
4
 from werkzeug.exceptions import abort
5
 
5
 
6
 from app.db import get_db
6
 from app.db import get_db
7
+from app.forms import StationForm
7
 
8
 
8
 bp = Blueprint('admin_insert', __name__)
9
 bp = Blueprint('admin_insert', __name__)
9
 
10
 
10
 @bp.route('/admin_insert', methods=('GET', 'POST'))
11
 @bp.route('/admin_insert', methods=('GET', 'POST'))
11
 def admin():
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
         db = get_db()
15
         db = get_db()
17
         db.execute(
16
         db.execute(
18
             'INSERT INTO station (nombre_empleado, oficina, last_turn)'
17
             'INSERT INTO station (nombre_empleado, oficina, last_turn)'
19
             ' VALUES (?, ?, ?)',
18
             ' VALUES (?, ?, ?)',
20
-            (eName, office, 0)
19
+            (form.eName.data, form.office.data, 0)
21
         )
20
         )
22
         db.commit()
21
         db.commit()
22
+
23
+        flash('Added station for  {} ({})'.format(form.eName.data, form.office.data))
24
+
23
         return redirect(url_for('admin_insert'))
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 View File

5
 class TurnForm(FlaskForm):
5
 class TurnForm(FlaskForm):
6
     cName = StringField('Name', validators=[DataRequired()])
6
     cName = StringField('Name', validators=[DataRequired()])
7
     cEmail = StringField('Email', validators=[DataRequired()])
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 View File

5
 {% endblock %}
5
 {% endblock %}
6
 
6
 
7
 {% block content %}
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
     </form>
15
     </form>
15
 {% endblock %}
16
 {% endblock %}

+ 1
- 1
app/templates/index.html View File

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>
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
         {% endfor %}
17
         {% endfor %}
18
 
18
 
19
-        <input type="submit" value="Get ticket">
19
+        {{ form.submit }}
20
     </form>
20
     </form>
21
 {% endblock %}
21
 {% endblock %}