2026-01-01 21:07:34 +01:00
|
|
|
import sqlalchemy as sa
|
2026-07-19 11:29:10 +02:00
|
|
|
|
|
|
|
|
from lapp import create_app
|
|
|
|
|
from models import Group, Item, ListOfItems, Shared, User, db
|
2026-07-19 09:58:27 +02:00
|
|
|
from secrets_config import load_secrets
|
2026-05-14 21:16:51 +02:00
|
|
|
|
2026-01-01 21:07:34 +01:00
|
|
|
|
2026-07-19 11:29:10 +02:00
|
|
|
def has_rows(model):
|
|
|
|
|
primary_key = next(iter(model.__table__.primary_key.columns))
|
|
|
|
|
return db.session.scalar(sa.select(primary_key).limit(1)) is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(app):
|
|
|
|
|
initial_data = load_secrets(app.instance_path)["initial_data"]
|
|
|
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
|
with db.session.begin():
|
|
|
|
|
if has_rows(User):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
partially_populated = [
|
|
|
|
|
model.__tablename__
|
|
|
|
|
for model in (Group, ListOfItems, Item, Shared)
|
|
|
|
|
if has_rows(model)
|
|
|
|
|
]
|
|
|
|
|
if partially_populated:
|
|
|
|
|
tables = ", ".join(partially_populated)
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
"Refusing to initialize a partially populated database. "
|
|
|
|
|
f"These tables contain data while user is empty: {tables}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
admin_group = Group(secret=initial_data["admin_group"])
|
|
|
|
|
user_group = Group(secret=initial_data["user_group"])
|
|
|
|
|
db.session.add_all([admin_group, user_group])
|
|
|
|
|
db.session.flush()
|
|
|
|
|
|
|
|
|
|
admin = User(
|
|
|
|
|
name="admin",
|
|
|
|
|
group_id=admin_group.id,
|
|
|
|
|
is_admin=True,
|
|
|
|
|
is_approved=True,
|
|
|
|
|
is_private=True,
|
|
|
|
|
)
|
|
|
|
|
admin.set_password(initial_data["admin_password"])
|
|
|
|
|
|
|
|
|
|
user = User(
|
|
|
|
|
name="ignace",
|
|
|
|
|
group_id=user_group.id,
|
|
|
|
|
is_admin=False,
|
|
|
|
|
is_approved=True,
|
|
|
|
|
)
|
|
|
|
|
user.set_password(initial_data["user_password"])
|
|
|
|
|
db.session.add_all([admin, user])
|
|
|
|
|
db.session.flush()
|
|
|
|
|
|
|
|
|
|
groceries = ListOfItems(
|
|
|
|
|
owner_user_id=user.id,
|
|
|
|
|
name="Supermarkt",
|
|
|
|
|
is_active=True,
|
|
|
|
|
)
|
|
|
|
|
chores = ListOfItems(
|
|
|
|
|
owner_user_id=user.id,
|
|
|
|
|
name="Klusjes",
|
|
|
|
|
is_active=True,
|
|
|
|
|
)
|
|
|
|
|
db.session.add_all([groceries, chores])
|
|
|
|
|
db.session.flush()
|
|
|
|
|
|
|
|
|
|
db.session.add_all([
|
|
|
|
|
Item(listofitems_id=groceries.id, label="Stokbrood"),
|
|
|
|
|
Item(
|
|
|
|
|
listofitems_id=groceries.id,
|
|
|
|
|
label="Speltbroodje",
|
|
|
|
|
category="Brood",
|
|
|
|
|
),
|
|
|
|
|
Item(
|
|
|
|
|
listofitems_id=groceries.id,
|
|
|
|
|
label="Boter",
|
|
|
|
|
category="Zuivel",
|
|
|
|
|
unit="x",
|
|
|
|
|
quantity=1,
|
|
|
|
|
),
|
|
|
|
|
Item(
|
|
|
|
|
listofitems_id=groceries.id,
|
|
|
|
|
label="Notenbroodjes",
|
|
|
|
|
is_checked=True,
|
|
|
|
|
),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
app = create_app()
|
|
|
|
|
if initialize(app):
|
2026-07-19 09:58:27 +02:00
|
|
|
print("Created initial credentials from instance/secrets.yaml")
|
2026-07-19 11:29:10 +02:00
|
|
|
else:
|
|
|
|
|
print("Database already contains users; no initial data was added")
|
2026-05-14 21:16:51 +02:00
|
|
|
|
2026-07-19 11:29:10 +02:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|