Przeglądaj źródła

Moves index stuff to seperate blueprint

Jose Reyes 5 lat temu
rodzic
commit
8e7e467e86
2 zmienionych plików z 46 dodań i 32 usunięć
  1. 3
    32
      app/__init__.py
  2. 43
    0
      app/index.py

+ 3
- 32
app/__init__.py Wyświetl plik

@@ -21,37 +21,8 @@ def create_app(test_config=None):
21 21
     from . import db
22 22
     db.init_app(app)
23 23
 
24
-    @app.route('/', methods=('GET', 'POST'))
25
-    def index():
26
-        if request.method == 'POST':
27
-            cName = request.form['cName']
28
-            cEmail = request.form['cEmail']
29
-            station_name = request.form['station']
30
-            error = None
31
-
32
-            if not cName:
33
-                error = 'Name is required.'
34
-            elif not cEmail:
35
-                error = 'Email is required'
36
-            else:
37
-                db = get_db()
38
-                station_id = db.execute(
39
-                    'SELECT s_id FROM station WHERE nombre_empleado=?',
40
-                    (station_name,)
41
-                ).fetchone()
42
-                db.execute(
43
-                    'UPDATE station SET last_turn = last_turn + 1 WHERE s_id=?',
44
-                    (station_id,)
45
-                )
46
-                turn = db.execute(
47
-                    'SELECT last_turn FROM station WHERE s_id=?',
48
-                    (station_id,)
49
-                ).fetchone()
50
-                db.execute(
51
-                    'INSERT INTO turno (cName, cEmail, turn, timeArrival, station)'
52
-                    ' VALUES (?, ?, ?, ?)',
53
-                    (cName, cEmail, turn, 'testin', station_id)
54
-                )
55
-            db.commit()
24
+    from . import index
25
+    app.register_blueprint(index.bp)
26
+    app.add_url_rule('/', endpoint='index')
56 27
 
57 28
     return app

+ 43
- 0
app/index.py Wyświetl plik

@@ -0,0 +1,43 @@
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
+
8
+bp = Blueprint('index', __name__)
9
+
10
+@bp.route('/', methods=('GET', 'POST'))
11
+def index():
12
+    if request.method == 'POST':
13
+        cName = request.form['cName']
14
+        cEmail = request.form['cEmail']
15
+        station_name = request.form['station']
16
+        error = None
17
+
18
+        if not cName:
19
+            error = 'Name is required.'
20
+        elif not cEmail:
21
+            error = 'Email is required'
22
+        else:
23
+            db = get_db()
24
+            station_id = db.execute(
25
+                'SELECT s_id FROM station WHERE nombre_empleado=?',
26
+                (station_name,)
27
+            ).fetchone()
28
+            db.execute(
29
+                'UPDATE station SET last_turn = last_turn + 1 WHERE s_id=?',
30
+                (station_id,)
31
+            )
32
+            turn = db.execute(
33
+                'SELECT last_turn FROM station WHERE s_id=?',
34
+                (station_id,)
35
+            ).fetchone()
36
+            db.execute(
37
+                'INSERT INTO turno (cName, cEmail, turn, timeArrival, station)'
38
+                ' VALUES (?, ?, ?, ?)',
39
+                (cName, cEmail, turn, 'testin', station_id)
40
+            )
41
+        db.commit()
42
+        return redirect(url_for('index.index'))
43
+    return render_template('index.html')