2020-01-05 02:51:14 +01:00
|
|
|
from subprocess import run
|
2020-01-05 01:00:06 +01:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
2020-01-05 02:51:14 +01:00
|
|
|
from datetime import datetime
|
2020-01-08 01:47:12 +01:00
|
|
|
from flask import Flask
|
2020-01-05 02:51:14 +01:00
|
|
|
from constants import DB_NAME, DB_USER, DB_PW
|
2020-01-05 01:00:06 +01:00
|
|
|
|
|
|
|
|
2020-01-05 02:51:14 +01:00
|
|
|
def create_database():
|
|
|
|
script = "database/mariadb_setup.sh"
|
|
|
|
output = run([script, DB_NAME, DB_USER, DB_PW])
|
|
|
|
if output.returncode != 0:
|
2020-01-05 01:00:06 +01:00
|
|
|
print("Error: couldn't create database")
|
|
|
|
exit()
|
|
|
|
|
|
|
|
|
2020-01-05 02:51:14 +01:00
|
|
|
def create_connection():
|
2020-01-05 01:00:06 +01:00
|
|
|
host = "localhost:3306"
|
|
|
|
connection_uri = "mysql+pymysql://{user}:{pw}@{url}/{db}".format(
|
2020-01-05 02:51:14 +01:00
|
|
|
user=DB_USER, pw=DB_PW, url=host, db=DB_NAME
|
2020-01-05 01:00:06 +01:00
|
|
|
)
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = connection_uri
|
|
|
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
|
|
|
db = SQLAlchemy(app)
|
2020-01-05 02:51:14 +01:00
|
|
|
return db
|
2020-01-05 01:00:06 +01:00
|
|
|
|
|
|
|
|
2020-01-05 02:51:14 +01:00
|
|
|
db = create_connection()
|
2020-01-05 01:00:06 +01:00
|
|
|
|
|
|
|
|
2020-01-05 02:51:14 +01:00
|
|
|
class Glacier(db.Model):
|
2020-01-08 01:23:00 +01:00
|
|
|
uid = db.Column(db.String(5), primary_key=True)
|
2020-01-06 06:34:35 +01:00
|
|
|
country = db.Column(db.String(60))
|
|
|
|
name = db.Column(db.String(60))
|
2020-01-08 01:23:00 +01:00
|
|
|
annual_data = db.relationship("Annual_Data")
|
2020-01-05 02:51:14 +01:00
|
|
|
|
|
|
|
def __init__(self, uid, country, name):
|
|
|
|
self.uid = uid
|
|
|
|
self.country = country
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
|
|
|
|
class Annual_Data(db.Model):
|
2020-01-08 01:47:12 +01:00
|
|
|
__tablename__ = "annual_data"
|
2020-01-08 01:23:00 +01:00
|
|
|
year = db.Column(db.Integer, primary_key=True)
|
2020-01-08 02:22:41 +01:00
|
|
|
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)
|
2020-01-05 02:51:14 +01:00
|
|
|
|
2020-01-08 01:23:00 +01:00
|
|
|
def __init__(self, year, surface, length, elevation):
|
|
|
|
self.year = year
|
2020-01-05 02:51:14 +01:00
|
|
|
self.surface = surface
|
2020-01-06 11:12:55 +01:00
|
|
|
self.length = length
|
|
|
|
self.elevation = elevation
|
2020-01-05 02:51:14 +01:00
|
|
|
|
2020-01-05 01:00:06 +01:00
|
|
|
|
2020-01-06 06:34:35 +01:00
|
|
|
class User(db.Model):
|
2020-01-05 02:51:14 +01:00
|
|
|
uid = db.Column(db.Integer, primary_key=True)
|
2020-01-08 01:47:12 +01:00
|
|
|
registration_date = db.Column(
|
|
|
|
db.DateTime, nullable=False, server_default=db.func.now()
|
|
|
|
)
|
2020-01-06 08:31:21 +01:00
|
|
|
username = db.Column(db.String(20), nullable=False, unique=True)
|
|
|
|
password = db.Column(db.String(60))
|
2020-01-05 02:51:14 +01:00
|
|
|
|
2020-01-06 08:31:21 +01:00
|
|
|
def __init__(self, uid, username, password):
|
2020-01-05 02:51:14 +01:00
|
|
|
self.uid = uid
|
2020-01-06 08:31:21 +01:00
|
|
|
self.username = username
|
|
|
|
self.password = password
|
2020-01-05 01:00:06 +01:00
|
|
|
|
2020-01-05 02:51:14 +01:00
|
|
|
|
|
|
|
def create_tables():
|
|
|
|
db.create_all()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
create_database()
|
|
|
|
create_tables()
|
2020-01-05 01:00:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|