瀏覽代碼

Fixes INTEGER in schema, adds mock index function and templates

Jose Reyes 6 年之前
父節點
當前提交
7cf50790c0
共有 5 個文件被更改,包括 89 次插入13 次删除
  1. 39
    3
      __init__.py
  2. 0
    3
      db.py
  3. 10
    7
      schema.sql
  4. 25
    0
      templates/base.html
  5. 15
    0
      templates/index.html

+ 39
- 3
__init__.py 查看文件

@@ -1,8 +1,44 @@
1
-def create_app():
2
-    app = ...
3
-    # existing code omitted
1
+import os
2
+from flask import Flask
3
+
4
+def create_app(test_config=None):
5
+    app = Flask(__name__, instance_relative_config=True)
6
+    app.config.from_mapping(
7
+        SECRET_KEY='dev',
8
+        DATABASE=os.path.join(app.instance_path, 'upr_espera.sqlite'),
9
+    )
10
+
11
+    if test_config is None:
12
+        app.config.from_pyfile('config.py', silent=True)
13
+    else:
14
+        app.config.from_mapping(test_config)
15
+
16
+    try:
17
+        os.makedirs(app.instance_path)
18
+    except OSError:
19
+        pass
4 20
 
5 21
     from . import db
6 22
     db.init_app(app)
7 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
+            error = None
30
+
31
+            if not cName:
32
+                error = 'Name is required.'
33
+            elif not cEmail:
34
+                error = 'Email is required'
35
+            else:
36
+                db = get_db()
37
+                db.execute(
38
+                    'INSERT INTO turno (cName, cEmail, timeArrival, station)'
39
+                    ' VALUES (?, ?, ?, (SELECT s_id FROM station WHERE nombre_empleado=?))',
40
+                    (cName, cEmail, 'testin', 'Fulano de Tal')
41
+                )
42
+            db.commit()
43
+
8 44
     return app

+ 0
- 3
db.py 查看文件

@@ -4,7 +4,6 @@ import click
4 4
 from flask import current_app, g
5 5
 from flask.cli import with_appcontext
6 6
 
7
-
8 7
 def get_db():
9 8
     if 'db' not in g:
10 9
         g.db = sqlite3.connect(
@@ -15,7 +14,6 @@ def get_db():
15 14
 
16 15
     return g.db
17 16
 
18
-
19 17
 def close_db(e=None):
20 18
     db = g.pop('db', None)
21 19
 
@@ -27,7 +25,6 @@ def init_db():
27 25
     with current_app.open_resource('schema.sql') as f:
28 26
         db.executescript(f.read().decode('utf8'))
29 27
 
30
-
31 28
 @click.command('init-db')
32 29
 @with_appcontext
33 30
 def init_db_command():

+ 10
- 7
schema.sql 查看文件

@@ -1,26 +1,29 @@
1
+DROP TABLE IF EXISTS station;
2
+DROP TABLE IF EXISTS user;
3
+DROP TABLE IF EXISTS turn;
1 4
 
2 5
 CREATE TABLE station (
3
-  s_id INT PRIMARY KEY AUTOINCREMENT,
6
+  s_id INTEGER PRIMARY KEY AUTOINCREMENT,
4 7
   nombre_empleado CHAR(20)  NOT NULL,
5 8
   oficina CHAR(20) NOT NULL,
6
-  last_turn INT NOT NULL
9
+  last_turn INTEGER NOT NULL
7 10
 );
8 11
 
9 12
 CREATE TABLE user (
10
-  u_id INT PRIMARY KEY AUTOINCREMENT,
13
+  u_id INTEGER PRIMARY KEY AUTOINCREMENT,
11 14
   uName CHAR(20) NOT NULL,
12 15
   isAdmin BIT NOT NULL,
13 16
   email CHAR(30) NOT NULL
14 17
 );
15 18
 
16
-CREATE TABLE turno(
17
- t_id INT PRIMARY KEY AUTOINCREMENT,
19
+CREATE TABLE turno (
20
+ t_id INTEGER PRIMARY KEY AUTOINCREMENT,
18 21
  cName CHAR(20) NOT NULL, 
19 22
  cEmail CHAR(30) NOT NULL, 
20 23
  purpose CHAR(40), 
21 24
  timeArrival CHAR(5) NOT NULL, 
22 25
  timeEnter CHAR(5),
23
- station INT NOT NULL, 
24
- FOREIGN KEY (station) REFERENCES station (id) 
26
+ station INTEGER NOT NULL, 
27
+ FOREIGN KEY (station) REFERENCES station (s_id) 
25 28
 );
26 29
 

+ 25
- 0
templates/base.html 查看文件

@@ -0,0 +1,25 @@
1
+<!doctype html>
2
+<title>{% block title %}{% endblock %} - UPR Queue</title>
3
+<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
4
+<nav>
5
+    <h1>UPR Queue</h1>
6
+<!--     <ul>
7
+        {% if g.user %}
8
+            <li><span>{{ g.user['username'] }}</span>
9
+            <li><a href="{{ url_for('auth.logout') }}">Log Out</a>
10
+        {% else %}
11
+            <li><a href="{{ url_for('auth.register') }}">Register</a>
12
+            <li><a href="{{ url_for('auth.login') }}">Log In</a>
13
+        {% endif %}
14
+    </ul> -->
15
+</nav>
16
+<section class="content">
17
+    <header>
18
+        {% block header %}{% endblock %}
19
+    </header>
20
+    {% for message in get_flashed_messages() %}
21
+        <div class="flash">{{ message }}</div>
22
+    {% endfor %}
23
+    {% block content %}{% endblock %}
24
+</section>
25
+

+ 15
- 0
templates/index.html 查看文件

@@ -0,0 +1,15 @@
1
+{% extends 'base.html' %}
2
+
3
+{% block header %}
4
+    <h1>{% block title %}UPR Queue{% endblock %}</h1>
5
+{% endblock %}
6
+
7
+{% block content %}
8
+    <form method="post">
9
+        <label for="name">Name</label>
10
+        <input name="name" id="name" value="{{ request.form['cName'] }}" required>
11
+		<label for="email">email</label>
12
+        <input name="email" id="email" value="{{ request.form['cEmail'] }}" required>
13
+        <input type="submit" value="Get ticket">
14
+    </form>
15
+{% endblock %}