10 Commits

Author SHA1 Message Date
  Jose Reyes 1923ebcf79 Changes admin_insert to use WTForms 6 years ago
  Jose Reyes 497337f51c Adds admin_insert blueprint and removes unneeded code from admin_insert 6 years ago
  Tatiana Castro 794425f8ca Delete 'app/templates/admin.html' 6 years ago
  Tatiana Castro b2fb37a79c Delete 'app/admin.py' 6 years ago
  Tatiana Castro b883087b3b arreglos 6 years ago
  Tatiana Castro 55373b279b arregle un error 6 years ago
  Tatiana Castro 5adadbbbf8 Merge branch 'develop' of https://git.ccom.uprrp.edu/jose.reyes46/upr_espera into develop 6 years ago
  Tatiana Castro 02b4cab6bc para admin, ahora mismo solo se puede insertar no se puede update ni remover 6 years ago
  Jose Reyes ccbefff3aa Changes template formatting 6 years ago
  Jose Reyes 1f4b1416d9 Changes plain forms to WTForms and cleans up some of index.py 6 years ago
7 changed files with 84 additions and 24 deletions
  1. 4
    0
      app/__init__.py
  2. 27
    0
      app/admin_insert.py
  3. 13
    0
      app/forms.py
  4. 15
    16
      app/index.py
  5. 16
    0
      app/templates/admin_insert.html
  6. 1
    1
      app/templates/base.html
  7. 8
    7
      app/templates/index.html

+ 4
- 0
app/__init__.py View File

@@ -25,4 +25,8 @@ def create_app(test_config=None):
25 25
     app.register_blueprint(index.bp)
26 26
     app.add_url_rule('/', endpoint='index')
27 27
 
28
+    from . import admin_insert
29
+    app.register_blueprint(admin_insert.bp)
30
+    app.add_url_rule('/admin_insert', endpoint='admin_insert')
31
+
28 32
     return app

+ 27
- 0
app/admin_insert.py View File

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

+ 13
- 0
app/forms.py View File

@@ -0,0 +1,13 @@
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')
9
+
10
+class StationForm(FlaskForm):
11
+    eName = StringField('Name', validators=[DataRequired()])
12
+    office = StringField('Office', validators=[DataRequired()])
13
+    submit = SubmitField('Insert Station')

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

+ 16
- 0
app/templates/admin_insert.html View File

@@ -0,0 +1,16 @@
1
+{% extends 'base.html' %}
2
+
3
+{% block header %}
4
+    <h1>{% block title %}UPR Queue: Insert Station{% endblock %}</h1>
5
+{% endblock %}
6
+
7
+{% block content %}
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 }}
15
+    </form>
16
+{% endblock %}

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

@@ -6,7 +6,7 @@
6 6
         {% block header %}{% endblock %}
7 7
     </header>
8 8
     {% for message in get_flashed_messages() %}
9
-        <div class="flash">{{ message }}</div>
9
+        <div class="flash">{{ message }}</div><br>
10 10
     {% endfor %}
11 11
     {% block content %}{% endblock %}
12 12
 </section>

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

@@ -5,16 +5,17 @@
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'] }} required> {{ "{} ({})".format(station['nombre_empleado'],station['oficina']) }} </input><br>
16 17
         {% endfor %}
17 18
 
18
-        <input type="submit" value="Get ticket">
19
+        {{ form.submit }}
19 20
     </form>
20 21
 {% endblock %}