-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_db.py
54 lines (42 loc) · 1.94 KB
/
create_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import argparse
from app import app, db
from datetime import datetime
from db import User, Year, Organization
def create_database(name, first_name, email):
with app.app_context():
db.create_all()
add_first_admin(name, first_name, email)
initialize_configuration()
create_organizations()
print("Database tables created successfully.")
def add_first_admin(name, first_name, email):
admin_exists = db.session.query(User).filter_by(is_admin=True).first()
if admin_exists is None:
first_admin = User(first_name=first_name, name=name, email=email, is_admin=True)
db.session.add(first_admin)
db.session.commit()
print(f"Admin account for {first_name} {name} created successfully.")
else:
print("Admin account already exists.")
# Add the first year corresponding to the current year
def initialize_configuration():
current_year = datetime.now().year
existing_year = Year.query.filter_by(year=current_year).first()
if existing_year is None:
config = Year(year=current_year, is_current_year=True)
db.session.add(config)
db.session.commit()
# Create organizations
def create_organizations():
if db.session.query(Organization).count() == 0:
ORGANIZATIONS = ["SST", "ICTM", "ELEN", "EPL", "INMA", "SSH", "IMMC", "IMAQ", "INGI", "SSS", "IONS", "ELI",
"LDRI"]
db.session.add_all([Organization(name=org) for org in ORGANIZATIONS])
db.session.commit()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Create the database and add the first admin.")
parser.add_argument('--name', required=True, help="Name of the admin.")
parser.add_argument('--first_name', required=True, help="First name of the admin.")
parser.add_argument('--email', required=True, help="Email of the admin.")
args = parser.parse_args()
create_database(args.name, args.first_name, args.email)