summaryrefslogtreecommitdiff
path: root/initialize_database.py
blob: 3fad7678e2cac57f2a64caa7a8e8ebf1f558f2ce (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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