app init, database schema, and database functions
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import os
|
||||
|
||||
from flask import Flask
|
||||
|
||||
def create_app(test_config=None):
|
||||
# create and configure app
|
||||
app = Flask(__name__, instance_relative_config=True)
|
||||
app.config.from_mapping(
|
||||
SECRET_KEY='dev',
|
||||
DATABASE=os.path.join(app.instance_path, 'flaskfdx.sqlite'),
|
||||
)
|
||||
|
||||
if test_config is None:
|
||||
# load the instance config, if it exists, when not testing
|
||||
app.config.from_pyfile('config.py', silent=True)
|
||||
else:
|
||||
# load the test config if passed in
|
||||
app.config.from_mapping(test_config)
|
||||
|
||||
# ensure the instance folder exists
|
||||
try:
|
||||
os.makedirs(app.instance_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
# simple hello world page
|
||||
@app.route('/hello')
|
||||
def hello():
|
||||
return 'Hello World!'
|
||||
|
||||
from . import db
|
||||
db.init_app(app)
|
||||
|
||||
return app
|
||||
@@ -0,0 +1,38 @@
|
||||
import sqlite3
|
||||
|
||||
import click
|
||||
from flask import current_app, g
|
||||
from flask.cli import with_appcontext
|
||||
|
||||
def get_db():
|
||||
if 'db' not in g:
|
||||
g.db = sqlite3.connect(
|
||||
current_app.config['DATABASE'],
|
||||
detect_types=sqlite3.PARSE_DECLTYPES
|
||||
)
|
||||
g.db.row_factory = sqlite3.Row
|
||||
|
||||
return g.db
|
||||
|
||||
def close_db(e=None):
|
||||
db = g.pop('db', None)
|
||||
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
def init_db():
|
||||
db = get_db()
|
||||
|
||||
with current_app.open_resource('schema.sql') as f:
|
||||
db.executescript(f.read().decode('utf8'))
|
||||
|
||||
@click.command('init-db')
|
||||
@with_appcontext
|
||||
def init_db_command():
|
||||
"""Create tables if they don't exist."""
|
||||
init_db()
|
||||
click.echo('initialized the database')
|
||||
|
||||
def init_app(app):
|
||||
app.teardown_appcontext(close_db)
|
||||
app.cli.add_command(init_db_command)
|
||||
@@ -0,0 +1,43 @@
|
||||
CREATE TABLE IF NOT EXISTS dsw (
|
||||
csa INTEGER,
|
||||
date TEXT NOT NULL,
|
||||
wa TEXT NOT NULL,
|
||||
driver TEXT,
|
||||
totalod TEXT,
|
||||
nextod TEXT,
|
||||
delstops INTEGER,
|
||||
delpkgs INTEGER,
|
||||
pustops INTEGER,
|
||||
pupkgs INTEGER,
|
||||
van INTEGER,
|
||||
miles INTEGER,
|
||||
ils TEXT,
|
||||
dna INTEGER,
|
||||
allstatus INTEGER,
|
||||
PRIMARY KEY (date,wa)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS totals (
|
||||
entity TEXT NOT NULL,
|
||||
date TEXT NOT NULL,
|
||||
prepkgs INTEGER,
|
||||
prestops INTEGER,
|
||||
prepustops INTEGER,
|
||||
delstops INTEGER,
|
||||
delpkgs INTEGER,
|
||||
pustops INTEGER,
|
||||
pupkgs INTEGER,
|
||||
ils TEXT,
|
||||
dna INTEGER,
|
||||
allstatus INTEGER,
|
||||
sig INTEGER,
|
||||
eve INTEGER,
|
||||
appt INTEGER,
|
||||
PRIMARY KEY(entity,date)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
userid INTEGER NOT NULL PRIMARY KEY,
|
||||
email TEXT UNIQUE NOT NULL,
|
||||
password TEXT NOT NULL
|
||||
);
|
||||
Reference in New Issue
Block a user