summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--initialize_database.py150
1 files changed, 150 insertions, 0 deletions
diff --git a/initialize_database.py b/initialize_database.py
new file mode 100644
index 0000000..3fad767
--- /dev/null
+++ b/initialize_database.py
@@ -0,0 +1,150 @@
+# -*- mode: python; -*-
+
+import os
+from datetime import datetime
+import app.models as model
+from app import create_app, db
+
+
+app = create_app()
+db_name = "test.db"
+app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + db_name
+db_path = f"app/{db_name}"
+
+
+if os.path.exists(db_path):
+ os.remove(db_path)
+ print(f"Existing database {db_path} has been deleted successfully.\n")
+else:
+ print(f"Database {db_path} does not exist yet, creating now.\n")
+
+with app.app_context():
+ db.create_all()
+
+
+modules = [
+ {
+ "name": "calculator",
+ "description": "Determine fertilizer recipes based on your farm model and desired crops for the season.",
+ },
+ {
+ "name": "creator",
+ "description": "Create a model of your farm and get an idea of baseline water usage.",
+ },
+ {
+ "name": "customers",
+ "description": "View and add customers.",
+ },
+ {
+ "name": "ferti",
+ "description": "Record all your measurements and plot trends.",
+ },
+ {
+ "name": "invoices",
+ "description": "Create invoices populated with customer and order informations.",
+ },
+ {
+ "name": "orders",
+ "description": "Log and filter orders and their payment status.",
+ },
+ {
+ "name": "products",
+ "description": "View and add products made by the farm.",
+ },
+ {
+ "name": "stock",
+ "description": "Keep track of your inventory.",
+ },
+]
+
+
+users = [
+ {
+ # "primary_key":
+ "username": "dingles",
+ "hashed_password": "foo",
+ "name_first": "Dingles",
+ "name_last": "Dingleberry",
+ "email": "dingleberryd@example.com",
+ "phone_mobile": 888_420_8888,
+ "phone_alternative": 694_206_9420,
+ },
+ {
+ # "primary_key":
+ "username": "dingles",
+ "hashed_password": "foo",
+ "name_first": "Dingles",
+ "name_last": "Dingleberry",
+ "email": "dingleberryd@example.com",
+ "phone_mobile": 888_420_8888,
+ "phone_alternative": 694_206_9420,
+ },
+ {
+ # "primary_key":
+ "username": "dingles",
+ "hashed_password": "foo",
+ "name_first": "Dingles",
+ "name_last": "Dingleberry",
+ "email": "dingleberryd@example.com",
+ "phone_mobile": 888_420_8888,
+ "phone_alternative": 694_206_9420,
+ },
+ {
+ # "primary_key":
+ "username": "dingles",
+ "hashed_password": "foo",
+ "name_first": "Dingles",
+ "name_last": "Dingleberry",
+ "email": "dingleberryd@example.com",
+ "phone_mobile": 888_420_8888,
+ "phone_alternative": 694_206_9420,
+ },
+ {
+ # "primary_key":
+ "username": "dingles",
+ "hashed_password": "foo",
+ "name_first": "Dingles",
+ "name_last": "Dingleberry",
+ "email": "dingleberryd@example.com",
+ "phone_mobile": 888_420_8888,
+ "phone_alternative": 694_206_9420,
+ },
+]
+
+customers = 5 * [
+ {
+ "name": "Intermarché",
+ "name_alternative": "Le grand magasin",
+ "code_customer": "101010",
+ "code_accounting": "foobar200",
+ "address": "123 rue du Marché",
+ "postal_code": "01630",
+ "city": "Challex",
+ "country": "France",
+ "phone": 888_000_8888,
+ "website": "lesupermarché@example.com",
+ "email": "lesupermarché@example.com",
+ "professional_id_1": "1",
+ "professional_id_2": "2",
+ "tax_id": "IMPOTS",
+ "payment_terms": "immédiatement",
+ }
+]
+
+
+def populate_table(table, items):
+ """The table is a class inheriting from db.Model. The items are a
+ list of dictionaries each representing an entry."""
+ for item in items:
+ new_record = table(**item)
+ print(f"Adding {item}")
+ db.session.add(new_record)
+ db.session.commit()
+
+ print(f"Successfully committed all items to {table.__tablename__}.\n")
+
+
+with app.app_context():
+ populate_table(model.Module, modules)
+ populate_table(model.User, users)
+ populate_table(model.Customer, customers)
Copyright 2019--2024 Marius PETER