From f84d71616adb8bdeeea4d96d440ea323cf67a4ec Mon Sep 17 00:00:00 2001 From: coolneng Date: Wed, 8 Jan 2020 21:31:00 +0100 Subject: [PATCH] Create DB when executing 'flask run' --- code/app/__init__.py | 7 ++++- code/app/models.py | 44 +++++++++++++++++++++++++++ code/config.py | 6 ++++ code/database/__init__.py | 0 code/database/constants.py | 4 +++ code/database/db_setup.py | 62 ++------------------------------------ code/database/export.py | 2 +- code/database/parser.py | 2 +- code/igdb.py | 5 +++ 9 files changed, 69 insertions(+), 63 deletions(-) create mode 100644 code/app/models.py create mode 100644 code/config.py create mode 100644 code/database/__init__.py diff --git a/code/app/__init__.py b/code/app/__init__.py index 96c8ef5..6d6c9a3 100644 --- a/code/app/__init__.py +++ b/code/app/__init__.py @@ -1,5 +1,10 @@ from flask import Flask +from config import Config +from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) +app.config.from_object(Config) +db = SQLAlchemy(app) -from app import routes + +from app import routes, models diff --git a/code/app/models.py b/code/app/models.py new file mode 100644 index 0000000..0fc8dfd --- /dev/null +++ b/code/app/models.py @@ -0,0 +1,44 @@ +from app import db +from subprocess import run +from database.constants import DB_USER, DB_PW, DB_NAME + + +class Glacier(db.Model): + uid = db.Column(db.String(5), primary_key=True) + country = db.Column(db.String(60)) + name = db.Column(db.String(60)) + annual_data = db.relationship("Annual_Data") + + def __init__(self, uid, country, name): + self.uid = uid + self.country = country + self.name = name + + +class Annual_Data(db.Model): + __tablename__ = "annual_data" + year = db.Column(db.Integer, primary_key=True) + uid = db.Column(db.ForeignKey("glacier.uid"), primary_key=True) + surface = db.Column(db.Float) + length = db.Column(db.Float) + elevation = db.Column(db.Float) + + def __init__(self, year, surface, length, elevation): + self.year = year + self.surface = surface + self.length = length + self.elevation = elevation + + +class User(db.Model): + uid = db.Column(db.Integer, primary_key=True) + registration_date = db.Column( + db.DateTime, nullable=False, server_default=db.func.now() + ) + username = db.Column(db.String(20), nullable=False, unique=True) + password = db.Column(db.String(60)) + + def __init__(self, uid, username, password): + self.uid = uid + self.username = username + self.password = password diff --git a/code/config.py b/code/config.py new file mode 100644 index 0000000..aef351a --- /dev/null +++ b/code/config.py @@ -0,0 +1,6 @@ +from database.constants import CONNECTION_URI + + +class Config(object): + SQLALCHEMY_DATABASE_URI = CONNECTION_URI + SQLALCHEMY_TRACK_MODIFICATIONS = False diff --git a/code/database/__init__.py b/code/database/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/database/constants.py b/code/database/constants.py index cffb6df..dd8d583 100644 --- a/code/database/constants.py +++ b/code/database/constants.py @@ -2,3 +2,7 @@ DB_NAME = "igdb" DB_USER = "igdb" DB_PW = "agentorange" ADMIN_PW = "fuckmonsanto" +HOST = "localhost:3306" +CONNECTION_URI = "mysql+pymysql://{user}:{pw}@{url}/{db}".format( + user=DB_USER, pw=DB_PW, url=HOST, db=DB_NAME +) diff --git a/code/database/db_setup.py b/code/database/db_setup.py index 7ed56f5..d6997bb 100644 --- a/code/database/db_setup.py +++ b/code/database/db_setup.py @@ -1,8 +1,6 @@ +from app import db +from database.constants import DB_NAME, DB_USER, DB_PW from subprocess import run -from flask_sqlalchemy import SQLAlchemy -from datetime import datetime -from flask import Flask -from constants import DB_NAME, DB_USER, DB_PW def create_database(): @@ -13,62 +11,6 @@ def create_database(): exit() -def create_connection(): - host = "localhost:3306" - connection_uri = "mysql+pymysql://{user}:{pw}@{url}/{db}".format( - user=DB_USER, pw=DB_PW, url=host, db=DB_NAME - ) - app = Flask(__name__) - app.config["SQLALCHEMY_DATABASE_URI"] = connection_uri - app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False - db = SQLAlchemy(app) - return db - - -db = create_connection() - - -class Glacier(db.Model): - uid = db.Column(db.String(5), primary_key=True) - country = db.Column(db.String(60)) - name = db.Column(db.String(60)) - annual_data = db.relationship("Annual_Data") - - def __init__(self, uid, country, name): - self.uid = uid - self.country = country - self.name = name - - -class Annual_Data(db.Model): - __tablename__ = "annual_data" - year = db.Column(db.Integer, primary_key=True) - uid = db.Column(db.ForeignKey("glacier.uid"), primary_key=True) - surface = db.Column(db.Float) - length = db.Column(db.Float) - elevation = db.Column(db.Float) - - def __init__(self, year, surface, length, elevation): - self.year = year - self.surface = surface - self.length = length - self.elevation = elevation - - -class User(db.Model): - uid = db.Column(db.Integer, primary_key=True) - registration_date = db.Column( - db.DateTime, nullable=False, server_default=db.func.now() - ) - username = db.Column(db.String(20), nullable=False, unique=True) - password = db.Column(db.String(60)) - - def __init__(self, uid, username, password): - self.uid = uid - self.username = username - self.password = password - - def create_tables(): db.create_all() diff --git a/code/database/export.py b/code/database/export.py index 5a72a65..266cb06 100644 --- a/code/database/export.py +++ b/code/database/export.py @@ -1,4 +1,4 @@ -from constants import DB_NAME, DB_PW, DB_USER +from database.constants import DB_NAME, DB_PW, DB_USER from pandas import DataFrame, read_csv from sqlalchemy import create_engine, engine diff --git a/code/database/parser.py b/code/database/parser.py index b79e043..fa66be8 100644 --- a/code/database/parser.py +++ b/code/database/parser.py @@ -1,7 +1,7 @@ from iso3166 import countries as co from pandas import DataFrame, concat, read_csv from csv import QUOTE_NONNUMERIC -from constants import ADMIN_PW +from database.constants import ADMIN_PW def country_conversion(political_unit) -> str: diff --git a/code/igdb.py b/code/igdb.py index d099b92..d42dfa6 100644 --- a/code/igdb.py +++ b/code/igdb.py @@ -1 +1,6 @@ from app import app +from database import db_setup, export, parser + +db_setup.main() +parser.main() +export.main()