-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_create.py
257 lines (230 loc) · 12.1 KB
/
db_create.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from app.models import db, AppConfig, Modules, OmsConfig, CrmConfig, HrmConfig, AmsConfig, MmsConfig, User, Roles, Permissions, AppNotifications
from app.routes import Violations
from app.config import sk_generator
# from psycopg2 import IntegrityError
# from sqlalchemy.exc import IntegrityError
# from sqlalchemy import update
### Notes ###
# The following code is an example of a way that works to update a db value.
# user = User.query.filter_by(id='9').update(dict(username='newname'))
integrity_error = "* IntegrityError: (({})) - Exception occurred while trying to add 1+ default sets of values. Perhaps they already exist."
errors = []
# DB creation.
# - Creates DB schema if it does not already exist.
db.create_all()
def add_rows_to_config_table(table_name, table_class, table_rows):
commit_errors = False
for key, value, permission_level, active in table_rows:
try:
db.session.add(table_class(key, value, permission_level, active))
db.session.commit()
except:
commit_errors = True
db.session.rollback()
if commit_errors == True:
errors.append(integrity_error.format(table_name))
# App config initialization.
# IMPORTANT! - Post-deployment, you will want to make sure that you change the secret key value in your database.
app_config_rows = [["App Name", "Just-a-Dash", 1, True],
["App Icon", "glyphicon glyphicon-equalizer", 1, True],
["App Title", "Just-a-Dash Enterprise Management System", 1, True],
["App Short-Title", "Just-a-Dash", 1, True],
["Toggle Placeholders", "false", 1, True],
["Secret Key", sk_generator(size=24), 1, True]]
add_rows_to_config_table('App Config', AppConfig, app_config_rows)
# Module registry initialization.
# - By default, Just-a-Dash comes with the following pre-built modules: Operations, Customer Relations, Human Resources, Accounting, Marketing
modules_rows = [["Admin Control Panel", "App", "Administrative control panel.", True],
["Operations", "OMS", "Operations management system.", True],
["Customer Relations", "CRM", "Customer relationship management system.", True],
["Human Resources", "HRM", "Human resource management system.", True],
["Accounting", "AMS", "Accounting management system.", True],
["Marketing", "MMS", "Marketing management system.", True]]
add_rows_to_config_table('Modules', Modules, modules_rows)
# Module config initializations.
# - OMS
oms_config_rows = [["Module Name", "Operations", 1, True],
["Module Abbreviation", "OMS", 1, True],
["Module Icon", "fa fa-fort-awesome", 1, True],
["Module Title", "Operations Management System", 1, True],
["Module Short-Title", "Operations Management", 1, True],
["Twilio Account SID", "", 1, True],
["Twilio Auth Token", "", 1, True],
["Twilio Phone Number", "+10000000000", 1, True],
["Phone Number Visibility", 'false', 1, True],
["Call Response MP3", 'http://www.you-should-upload-an-mp3-to-some-file-storage-and-then-enter-the-url-address-here.com/some_sound_file.mp3', 1, True],
["Call Response MP3 Toggle", 'false', 1, True],
["Call Response Text-to-Speech", 'You have successfully checked in. Thank you, and have a wonderful day!', 1, True],
["Call Response Text-to-Speech Toggle", 'true', 1, True]]
add_rows_to_config_table('OMS Module Config', OmsConfig, oms_config_rows)
# - CRM
crm_config_rows = [["Module Name", "Customer Relations", 1, True],
["Module Abbreviation", "", 1, True],
["Module Icon", "ion-person-stalker", 1, True],
["Module Title", "Customer Relationship Management System", 1, True],
["Module Short-Title", "Customer Relations Management", 1, True]]
add_rows_to_config_table('CRM Module Config', CrmConfig, crm_config_rows)
# - HRM
hrm_config_rows = [["Module Name", "Human Resources", 1, True],
["Module Abbreviation", "", 1, True],
["Module Icon", "fa fa-users", 1, True],
["Module Title", "Human Resource Management System", 1, True],
["Module Short-Title", "Human Resources Management", 1, True]]
add_rows_to_config_table('HRM Module Config', HrmConfig, hrm_config_rows)
# - AMS
ams_config_rows = [["Module Name", "Accounting", 1, True],
["Module Abbreviation", "", 1, True],
["Module Icon", "fa fa-bar-chart", 1, True],
["Module Title", "Accounting Management System", 1, True],
["Module Short-Title", "Accounting Management", 1, True]]
add_rows_to_config_table('AMS Module Config', AmsConfig, ams_config_rows)
# - MMS
mms_config_rows = [["App Name", "Marketing", 1, True],
["Module Abbreviation", "", 1, True],
["App Icon", "fa fa-line-chart", 1, True],
["App Title", "Marketing Management System", 1, True],
["App Short-Title", "Marketing Management", 1, True]]
add_rows_to_config_table('MMS Module Config', MmsConfig, mms_config_rows)
# Default users initialization.
# - Initalizes the app with a user with master permissions. The app administrator should change the e-mail/password immediately. Also adds a basic admin and a basic user.
# - IMPORTANT! - Post-deployment, you will want to make sure that you change these passwords (at least for 'master') in your database.
# - Note: The below code seemed necessary when using Bcrypt, but am not using Werkzeug Security.
# for item in db.session:
# item.password = item.password.decode("utf-8")
user_rows = [["master", "[email protected]", "master", "master", "master", "master", "master", "master", "master"],
["super_admin", "[email protected]", "super_admin", "super", "super", "super", "super", "super", "super"],
["admin", "[email protected]", "admin", "basic", "basic", "basic", "basic", "basic", "basic"],
["user", "[email protected]", "user", "None", "None", "None", "None", "None", "None"],
["demo", "[email protected]", "demo", "basic", "basic", "basic", "basic", "basic", "basic"],
["oms_demo", "[email protected]", "oms_demo", "None", "basic", "None", "None", "None", "None"],
["crm_demo", "[email protected]", "crm_demo", "None", "None", "basic", "None", "None", "None"],
["hrm_demo", "[email protected]", "hrm_demo", "None", "None", "None", "basic", "None", "None"],
["ams_demo", "[email protected]", "ams_demo", "None", "None", "None", "None", "basic", "None"],
["mms_demo", "[email protected]", "mms_demo", "None", "None", "None", "None", "None", "basic"]]
user_errors = False
for username, email, password, admin_role, oms_role, crm_role, hrm_role, ams_role, mms_role in user_rows:
try:
db.session.add(User(username, email, password, admin_role, oms_role, crm_role, hrm_role, ams_role, mms_role))
db.session.commit()
except:
user_errors = True
db.session.rollback()
if user_errors == True:
errors.append(integrity_error.format('Users'))
# Roles initialization.
# - Initializes the app with admin/group roles.
roles_rows = [["App", "Master", 0],
["App", "Super", 1],
["App", "Basic", 2],
["App", "Custom1", 777],
["OMS", "Master", 0],
["OMS", "Super", 1],
["OMS", "Basic", 2],
["OMS", "Custom1", 777],
["CRM", "Master", 0],
["CRM", "Super", 1],
["CRM", "Basic", 2],
["CRM", "Custom1", 777],
["HRM", "Master", 0],
["HRM", "Super", 1],
["HRM", "Basic", 2],
["HRM", "Custom1", 777],
["AMS", "Master", 0],
["AMS", "Super", 1],
["AMS", "Basic", 2],
["AMS", "Custom1", 777],
["MMS", "Master", 0],
["MMS", "Super", 1],
["MMS", "Basic", 2],
["MMS", "Custom1", 777]]
roles_errors = False
for module_abbreviation, role, permission_level in roles_rows:
try:
db.session.add(Roles(module_abbreviation, role, permission_level))
db.session.commit()
except:
roles_errors = True
db.session.rollback()
if roles_errors == True:
errors.append(integrity_error.format('Roles'))
# Permissions initialization.
# - Initializes the app with admin roles.
# Parameters: module, role, permission, r(read), w(write), u(update), d(delete).
permissions_rows = [["App", "Master", "Master_All", True, True, True, True],
["App", "Super", "Super_All", True, True, True, True],
["App", "Basic", "Read_Only", True, False, False, False],
["App", "Read", "Can_Read", True, False, False, False],
["App", "Write", "Can_Write", False, True, False, False],
["App", "Update", "Can_Update", False, False, True, False],
["App", "Delete", "Can_Delete", False, False, False, True],
["OMS", "Master", "Master_All", True, True, True, True],
["OMS", "Super", "Super_All", True, True, True, True],
["OMS", "Basic", "Read_Only", True, False, False, False],
["OMS", "Read", "Can_Read", True, False, False, False],
["OMS", "Write", "Can_Write", False, True, False, False],
["OMS", "Update", "Can_Update", False, False, True, False],
["OMS", "Delete", "Can_Delete", False, False, False, True],
["CRM", "Master", "Master_All", True, True, True, True],
["CRM", "Super", "Super_All", True, True, True, True],
["CRM", "Basic", "Read_Only", True, False, False, False],
["CRM", "Read", "Can_Read", True, False, False, False],
["CRM", "Write", "Can_Write", False, True, False, False],
["CRM", "Update", "Can_Update", False, False, True, False],
["CRM", "Delete", "Can_Delete", False, False, False, True],
["HRM", "Master", "Master_All", True, True, True, True],
["HRM", "Super", "Super_All", True, True, True, True],
["HRM", "Basic", "Read_Only", True, False, False, False],
["HRM", "Read", "Can_Read", True, False, False, False],
["HRM", "Write","Can_Write", False, True, False, False],
["HRM", "Update", "Can_Update", False, False, True, False],
["HRM", "Delete", "Can_Delete", False, False, False, True],
["AMS", "Master", "Master_All", True, True, True, True],
["AMS", "Super", "Super_All", True, True, True, True],
["AMS", "Basic", "Read_Only", True, False, False, False],
["AMS", "Read", "Can_Read", True, False, False, False],
["AMS", "Write", "Can_Write", False, True, False, False],
["AMS", "Update", "Can_Update", False, False, True, False],
["AMS", "Delete", "Can_Delete", False, False, False, True],
["MMS", "Master", "Master_All", True, True, True, True],
["MMS", "Super", "Super_All", True, True, True, True],
["MMS", "Basic", "Read_Only", True, False, False, False],
["MMS", "Read", "Can_Read", True, False, False, False],
["MMS", "Write", "Can_Write", False, True, False, False],
["MMS", "Update", "Can_Update", False, False, True, False],
["MMS", "Delete", "Can_Delete", False, False, False, True]]
permissions_errors = False
for module, role, permission, r, w, u, d in permissions_rows:
try:
db.session.add(Permissions(module, role, permission, r, w, u, d))
db.session.commit()
except:
permissions_errors = True
db.session.rollback()
if permissions_errors == True:
errors.append(integrity_error.format('Permissions'))
# Tutorial messages initialization.
# - Initializes the database creation with a first message to start the user off with an app tutorial.
notifications_rows = [["Notification", "Tutorial", "Welcome to Just-a-Dash!",
"Welcome to Just-a-Dash, the minimalist's dashboard application. I hope that you enjoy your experience. To get in "
"touch with me for comment / question / feature request / whatever it may be, feel free to e-mail "
"[email protected], or check out my blog, joeflack.net. - Joe Flack, Creator of Just-a-Dash",
"Joe Flack", "Group:AllUsers", "WebApp, NativeApps, Email"]]
notifications_errors = False
for message_type, subcategory, title, body, author, destinations, delivery_methods in notifications_rows:
try:
db.session.add(AppNotifications(message_type, subcategory, title, body, author, destinations, delivery_methods))
db.session.commit()
except:
notifications_errors = True
db.session.rollback()
if notifications_errors == True:
errors.append(integrity_error.format('App Notifications'))
# - Summary
print("")
print("# # # Database creation and initialization complete. # # #")
print("")
if len(errors) > 0:
print("Summary of exceptions: ")
for error in errors:
print(error)
print("")