Selaa lähdekoodia

Fixes INTEGER in schema, adds mock index function and templates

Jose Reyes 5 vuotta sitten
vanhempi
commit
7cf50790c0
5 muutettua tiedostoa jossa 89 lisäystä ja 13 poistoa
  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 Näytä tiedosto

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
     from . import db
21
     from . import db
6
     db.init_app(app)
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
     return app
44
     return app

+ 0
- 3
db.py Näytä tiedosto

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

+ 10
- 7
schema.sql Näytä tiedosto

1
+DROP TABLE IF EXISTS station;
2
+DROP TABLE IF EXISTS user;
3
+DROP TABLE IF EXISTS turn;
1
 
4
 
2
 CREATE TABLE station (
5
 CREATE TABLE station (
3
-  s_id INT PRIMARY KEY AUTOINCREMENT,
6
+  s_id INTEGER PRIMARY KEY AUTOINCREMENT,
4
   nombre_empleado CHAR(20)  NOT NULL,
7
   nombre_empleado CHAR(20)  NOT NULL,
5
   oficina CHAR(20) NOT NULL,
8
   oficina CHAR(20) NOT NULL,
6
-  last_turn INT NOT NULL
9
+  last_turn INTEGER NOT NULL
7
 );
10
 );
8
 
11
 
9
 CREATE TABLE user (
12
 CREATE TABLE user (
10
-  u_id INT PRIMARY KEY AUTOINCREMENT,
13
+  u_id INTEGER PRIMARY KEY AUTOINCREMENT,
11
   uName CHAR(20) NOT NULL,
14
   uName CHAR(20) NOT NULL,
12
   isAdmin BIT NOT NULL,
15
   isAdmin BIT NOT NULL,
13
   email CHAR(30) NOT NULL
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
  cName CHAR(20) NOT NULL, 
21
  cName CHAR(20) NOT NULL, 
19
  cEmail CHAR(30) NOT NULL, 
22
  cEmail CHAR(30) NOT NULL, 
20
  purpose CHAR(40), 
23
  purpose CHAR(40), 
21
  timeArrival CHAR(5) NOT NULL, 
24
  timeArrival CHAR(5) NOT NULL, 
22
  timeEnter CHAR(5),
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 Näytä tiedosto

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 Näytä tiedosto

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 %}