95 lines
2.7 KiB
Python
95 lines
2.7 KiB
Python
from subprocess import run
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask import Flask
|
|
from datetime import datetime
|
|
from constants import DB_NAME, DB_USER, DB_PW
|
|
|
|
|
|
def create_database():
|
|
script = "database/mariadb_setup.sh"
|
|
output = run([script, DB_NAME, DB_USER, DB_PW])
|
|
if output.returncode != 0:
|
|
print("Error: couldn't 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")
|
|
annual_change = db.relationship("Annual_Change")
|
|
|
|
def __init__(self, uid, country, name):
|
|
self.uid = uid
|
|
self.country = country
|
|
self.name = name
|
|
|
|
|
|
class Annual_Data(db.Model):
|
|
year = db.Column(db.Integer, primary_key=True)
|
|
uid = db.Column(db.String(5), db.ForeignKey("glacier.uid"), primary_key=True)
|
|
surface = db.Column(db.Integer)
|
|
length = db.Column(db.Integer)
|
|
elevation = db.Column(db.Integer)
|
|
|
|
def __init__(self, year, surface, length, elevation):
|
|
self.year = year
|
|
self.surface = surface
|
|
self.length = length
|
|
self.elevation = elevation
|
|
|
|
|
|
class Annual_Change(db.Model):
|
|
year = db.Column(db.Integer, db.ForeignKey("annual__data.year"), primary_key=True)
|
|
uid = db.Column(db.String(5), db.ForeignKey("glacier.uid"), primary_key=True)
|
|
surface_change = db.Column(db.Integer)
|
|
length_change = db.Column(db.Integer)
|
|
elevation_change = db.Column(db.Integer)
|
|
|
|
def __init__(self, surface_change, length_change, elevation_change):
|
|
self.surface_change = surface_change
|
|
self.length_change = length_change
|
|
self.elevation_change = elevation_change
|
|
|
|
|
|
class User(db.Model):
|
|
uid = db.Column(db.Integer, primary_key=True)
|
|
registration_date = db.Column(db.DateTime, nullable=False)
|
|
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.registration_date = datetime.today()
|
|
self.username = username
|
|
self.password = password
|
|
|
|
|
|
def create_tables():
|
|
db.create_all()
|
|
|
|
|
|
def main():
|
|
create_database()
|
|
create_tables()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|