This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
341 lines (273 loc) · 9.63 KB
/
setup.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#! usr/bin/python3
# -*- coding: utf-8 -*-
import datetime
import json
from scripts.create_json import WriteConfigJson, config_file
from application import db, app
from application.flicket_admin.models.flicket_config import FlicketConfig
from application.flicket.models.flicket_models import (
FlicketStatus,
FlicketRequesterRole,
FlicketRequestStage,
FlicketProcedureStage,
FlicketInstitute,
FlicketDomain,
)
from application.flicket.models.flicket_user import FlicketUser, FlicketGroup
from application.flicket.scripts.hash_password import hash_password
admin = "admin"
# configuration defaults for flicket
flicket_config = {
"posts_per_page": 50,
"allowed_extensions": [
"txt",
"log",
"pdf",
"png",
"jpg",
"jpeg",
"gif",
"msg",
"doc",
"docx",
"ppt",
"pptx",
"xls",
"xlsx",
],
"ticket_upload_folder": "application/flicket/static/flicket_uploads",
"avatar_upload_folder": "application/flicket/static/flicket_avatars",
}
# Default domains
domains = [
"Bioinformatics",
"Database Management",
"Image data processing",
"Optics and biophotonics",
"Mechatronics",
"Software Development",
]
institutes = [
"IBDM",
"Institut Fresnel",
"CIML",
"INMED",
"CINAM",
"LAI",
"IRPHE",
"M2P2",
"TAGC",
"IUSTI",
"TAGC",
"Ciphe",
"CPT",
"IMM",
"Centrale Marseille",
"LIS",
"CENTURI",
]
procedure_stages = ["Consulting", "Short", "Validated", "Declined"]
request_stages = ["In progress", "Pending", "Finished"]
@app.cli.command("run_set_up")
def run_set_up(self):
WriteConfigJson().json_exists()
username, password, email = get_admin_details()
set_db_config_defaults()
set_email_config()
create_admin(username=username, password=password, email=email, job_title="admin")
create_notifier()
create_admin_group()
create_default_ticket_status()
create_default_requester_role_levels()
create_default_request_stage_levels()
create_default_procedure_stage_levels()
create_default_depts()
# commit changes to the database
db.session.commit()
def set_db_config_defaults(silent=False):
with open(config_file, "r") as f:
config_data = json.load(f)
base_url = config_data["base_url"]
count = FlicketConfig.query.count()
if count > 0:
if not silent:
print(
"Flicket Config database seems to already be populated. Check values via application."
)
return
set_config = FlicketConfig(
posts_per_page=flicket_config["posts_per_page"],
allowed_extensions=", ".join(flicket_config["allowed_extensions"]),
ticket_upload_folder=flicket_config["ticket_upload_folder"],
avatar_upload_folder=flicket_config["avatar_upload_folder"],
base_url=base_url,
application_title="Clicket",
mail_max_emails=10,
mail_port=465,
)
if not silent:
print("Adding config values to database.")
db.session.add(set_config)
db.session.commit()
def get_admin_details():
# todo: add some password validation to prevent easy passwords being entered
_username = admin
with open(config_file, "r") as f:
config_data = json.load(f)
email = config_data["admin_email"]
password = config_data["admin_password"]
return _username, password, email
def create_admin(username, password, email, job_title, silent=False):
""" creates flicket_admin user. """
query = FlicketUser.query.filter_by(username=username)
if query.count() == 0:
add_user = FlicketUser(
username=username,
name=username,
password=hash_password(password),
email=email,
job_title=job_title,
date_added=datetime.datetime.now(),
)
db.session.add(add_user)
if not silent:
print("Admin user added.")
else:
print("Admin user is already added.")
def create_notifier():
""" creates user for notifications """
query = FlicketUser.query.filter_by(username=app.config["NOTIFICATION"]["username"])
if query.count() == 0:
add_user = FlicketUser(
username=app.config["NOTIFICATION"]["username"],
name=app.config["NOTIFICATION"]["name"],
password=hash_password(app.config["NOTIFICATION"]["password"]),
email=app.config["NOTIFICATION"]["email"],
date_added=datetime.datetime.now(),
)
db.session.add(add_user)
print("Notification user added.")
else:
print("Notification user already added.")
def create_admin_group(silent=False):
""" creates flicket_admin and super_user group and assigns flicket_admin to group admin. """
query = FlicketGroup.query.filter_by(group_name=app.config["ADMIN_GROUP_NAME"])
if query.count() == 0:
add_group = FlicketGroup(group_name=app.config["ADMIN_GROUP_NAME"])
db.session.add(add_group)
if not silent:
print("Admin group added")
user = FlicketUser.query.filter_by(username=admin).first()
group = FlicketGroup.query.filter_by(
group_name=app.config["ADMIN_GROUP_NAME"]
).first()
in_group = False
# see if user flicket_admin is already in flicket_admin group.
for g in group.users:
if g.username == admin:
in_group = True
break
if not in_group:
group.users.append(user)
if not silent:
print("Added flicket_admin user to flicket_admin group.")
# create the super_user group
query = FlicketGroup.query.filter_by(group_name=app.config["SUPER_USER_GROUP_NAME"])
if query.count() == 0:
add_group = FlicketGroup(group_name=app.config["SUPER_USER_GROUP_NAME"])
db.session.add(add_group)
if not silent:
print("super_user group added")
# noinspection PyArgumentList
def create_default_ticket_status(silent=False):
""" set up default status levels """
sl = ["Open", "Closed"]
for s in sl:
status = FlicketStatus.query.filter_by(status=s).first()
if not status:
add_status = FlicketStatus(status=s)
db.session.add(add_status)
if not silent:
print("Added status level {}".format(s))
def create_default_requester_role_levels(silent=False):
""" set up default requester_role levels """
pl = ["PhD", "PostDoc", "Engineer", "Researcher"]
for p in pl:
requester_role = FlicketRequesterRole.query.filter_by(requester_role=p).first()
if not requester_role:
add_requester_role = FlicketRequesterRole(requester_role=p)
db.session.add(add_requester_role)
if not silent:
print("Added requester role level {}".format(p))
def create_default_request_stage_levels(silent=False):
""" set up default request_stage levels """
for level in request_stages:
request_stage = FlicketRequestStage.query.filter_by(request_stage=level).first()
if not request_stage:
add_request_stage = FlicketRequestStage(request_stage=level)
db.session.add(add_request_stage)
if not silent:
print("Added request stage level {}".format(level))
def create_default_procedure_stage_levels(silent=False):
""" set up default procedure_stage levels """
for level in procedure_stages:
procedure_stage = FlicketProcedureStage.query.filter_by(
procedure_stage=level
).first()
if not procedure_stage:
add_procedure_stage = FlicketProcedureStage(procedure_stage=level)
db.session.add(add_procedure_stage)
if not silent:
print("Added procedure stage level {}".format(level))
def create_default_depts(silent=False):
""" creates default institutes and domains. """
for institute in institutes:
query = FlicketInstitute.query.filter_by(institute=institute).first()
if not query:
add_institute = FlicketInstitute(institute=institute)
db.session.add(add_institute)
if not silent:
print("institute {} added.".format(institute))
for domain in domains:
query = FlicketDomain.query.filter_by(domain=domain).first()
if not query:
add_domain = FlicketDomain(domain=domain)
db.session.add(add_domain)
if silent is False:
print("domain {} added.".format(domain))
def set_email_config(silent=False):
"""
To stop mail send errors after initial set-up set the email configuration value for suppress
send will be set to True
:return:
"""
query = FlicketConfig.query.first()
if query.mail_server is None:
query.mail_debug = True
query.mail_suppress_send = True
db.session.commit()
if not silent:
print(
"Setting email settings to suppress sending. Change values via administration panel with in "
"Flicket."
)
class TestingSetUp:
@staticmethod
def set_db_config_defaults_testing(silent=False):
"""
Set config defaults. Only used for unit testing
:param silent:
:return:
"""
set_config = FlicketConfig(
posts_per_page=10,
allowed_extensions="txt, jpg",
ticket_upload_folder="tmp/uploads",
base_url="",
mail_debug=True,
mail_suppress_send=True,
)
if not silent:
print("Adding config values to database.")
db.session.add(set_config)
db.session.commit()