[Flask] Simple ERP for a farm.
Database init and placeholdler value insertion for testing.
initialize_database.py
@@ -0,0 +1,150 @@
1
Added:
# -*- mode: python; -*-
2
Added:
3
Added:
import os
4
Added:
from datetime import datetime
5
Added:
import app.models as model
6
Added:
from app import create_app, db
7
Added:
8
Added:
9
Added:
app = create_app()
10
Added:
db_name = "test.db"
11
Added:
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + db_name
12
Added:
db_path = f"app/{db_name}"
13
Added:
14
Added:
15
Added:
if os.path.exists(db_path):
16
Added:
os.remove(db_path)
17
Added:
print(f"Existing database {db_path} has been deleted successfully.\n")
18
Added:
else:
19
Added:
print(f"Database {db_path} does not exist yet, creating now.\n")
20
Added:
21
Added:
with app.app_context():
22
Added:
db.create_all()
23
Added:
24
Added:
25
Added:
modules = [
26
Added:
{
27
Added:
"name": "calculator",
28
Added:
"description": "Determine fertilizer recipes based on your farm model and desired crops for the season.",
29
Added:
},
30
Added:
{
31
Added:
"name": "creator",
32
Added:
"description": "Create a model of your farm and get an idea of baseline water usage.",
33
Added:
},
34
Added:
{
35
Added:
"name": "customers",
36
Added:
"description": "View and add customers.",
37
Added:
},
38
Added:
{
39
Added:
"name": "ferti",
40
Added:
"description": "Record all your measurements and plot trends.",
41
Added:
},
42
Added:
{
43
Added:
"name": "invoices",
44
Added:
"description": "Create invoices populated with customer and order informations.",
45
Added:
},
46
Added:
{
47
Added:
"name": "orders",
48
Added:
"description": "Log and filter orders and their payment status.",
49
Added:
},
50
Added:
{
51
Added:
"name": "products",
52
Added:
"description": "View and add products made by the farm.",
53
Added:
},
54
Added:
{
55
Added:
"name": "stock",
56
Added:
"description": "Keep track of your inventory.",
57
Added:
},
58
Added:
]
59
Added:
60
Added:
61
Added:
users = [
62
Added:
{
63
Added:
# "primary_key":
64
Added:
"username": "dingles",
65
Added:
"hashed_password": "foo",
66
Added:
"name_first": "Dingles",
67
Added:
"name_last": "Dingleberry",
68
Added:
"email": "dingleberryd@example.com",
69
Added:
"phone_mobile": 888_420_8888,
70
Added:
"phone_alternative": 694_206_9420,
71
Added:
},
72
Added:
{
73
Added:
# "primary_key":
74
Added:
"username": "dingles",
75
Added:
"hashed_password": "foo",
76
Added:
"name_first": "Dingles",
77
Added:
"name_last": "Dingleberry",
78
Added:
"email": "dingleberryd@example.com",
79
Added:
"phone_mobile": 888_420_8888,
80
Added:
"phone_alternative": 694_206_9420,
81
Added:
},
82
Added:
{
83
Added:
# "primary_key":
84
Added:
"username": "dingles",
85
Added:
"hashed_password": "foo",
86
Added:
"name_first": "Dingles",
87
Added:
"name_last": "Dingleberry",
88
Added:
"email": "dingleberryd@example.com",
89
Added:
"phone_mobile": 888_420_8888,
90
Added:
"phone_alternative": 694_206_9420,
91
Added:
},
92
Added:
{
93
Added:
# "primary_key":
94
Added:
"username": "dingles",
95
Added:
"hashed_password": "foo",
96
Added:
"name_first": "Dingles",
97
Added:
"name_last": "Dingleberry",
98
Added:
"email": "dingleberryd@example.com",
99
Added:
"phone_mobile": 888_420_8888,
100
Added:
"phone_alternative": 694_206_9420,
101
Added:
},
102
Added:
{
103
Added:
# "primary_key":
104
Added:
"username": "dingles",
105
Added:
"hashed_password": "foo",
106
Added:
"name_first": "Dingles",
107
Added:
"name_last": "Dingleberry",
108
Added:
"email": "dingleberryd@example.com",
109
Added:
"phone_mobile": 888_420_8888,
110
Added:
"phone_alternative": 694_206_9420,
111
Added:
},
112
Added:
]
113
Added:
114
Added:
customers = 5 * [
115
Added:
{
116
Added:
"name": "Intermarché",
117
Added:
"name_alternative": "Le grand magasin",
118
Added:
"code_customer": "101010",
119
Added:
"code_accounting": "foobar200",
120
Added:
"address": "123 rue du Marché",
121
Added:
"postal_code": "01630",
122
Added:
"city": "Challex",
123
Added:
"country": "France",
124
Added:
"phone": 888_000_8888,
125
Added:
"website": "lesupermarché@example.com",
126
Added:
"email": "lesupermarché@example.com",
127
Added:
"professional_id_1": "1",
128
Added:
"professional_id_2": "2",
129
Added:
"tax_id": "IMPOTS",
130
Added:
"payment_terms": "immédiatement",
131
Added:
}
132
Added:
]
133
Added:
134
Added:
135
Added:
def populate_table(table, items):
136
Added:
"""The table is a class inheriting from db.Model. The items are a
137
Added:
list of dictionaries each representing an entry."""
138
Added:
for item in items:
139
Added:
new_record = table(**item)
140
Added:
print(f"Adding {item}")
141
Added:
db.session.add(new_record)
142
Added:
db.session.commit()
143
Added:
144
Added:
print(f"Successfully committed all items to {table.__tablename__}.\n")
145
Added:
146
Added:
147
Added:
with app.app_context():
148
Added:
populate_table(model.Module, modules)
149
Added:
populate_table(model.User, users)
150
Added:
populate_table(model.Customer, customers)