-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathseed.py
412 lines (375 loc) · 13.5 KB
/
seed.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# -*- coding: utf-8 -*-
"""
mslib.mscolab.seed
~~~~~~~~~~~~~~~~~~~~
Seeder utility for database
This file is part of MSS.
:copyright: Copyright 2019 Shivashis Padhi
:copyright: Copyright 2019-2024 by the MSS team, see AUTHORS.
:license: APACHE-2.0, see LICENSE for details.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import logging
import fs
import git
from sqlalchemy.exc import IntegrityError
from mslib.mscolab.conf import mscolab_settings
from mslib.mscolab.models import User, db, Permission, Operation
from mslib.mscolab.server import APP as app
# Todo: refactor move to mscolab.utils
def add_all_users_to_all_operations(access_level='collaborator'):
""" on db level we add all users as collaborator to all operations """
app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
with app.app_context():
all_operations = Operation.query.all()
all_path = [operation.path for operation in all_operations]
db.session.close()
for path in all_path:
if path == "TEMPLATE":
access_level = 'admin'
add_all_users_default_operation(path=path, access_level=access_level)
def add_all_users_default_operation(path='TEMPLATE', description="Operation to keep all users", access_level='admin'):
""" on db level we add all users to the operation TEMPLATE for user handling"""
app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
with app.app_context():
operation_available = Operation.query.filter_by(path=path).first()
if not operation_available:
operation = Operation(path, description)
db.session.add(operation)
db.session.commit()
with fs.open_fs(mscolab_settings.OPERATIONS_DATA) as file_dir:
if not file_dir.exists(path):
file_dir.makedir(path)
file_dir.writetext(f'{path}/main.ftml', mscolab_settings.STUB_CODE)
# initiate git
r = git.Repo.init(fs.path.join(mscolab_settings.DATA_DIR, 'filedata', path))
r.git.clear_cache()
r.index.add(['main.ftml'])
r.index.commit("initial commit")
operation = Operation.query.filter_by(path=path).first()
op_id = operation.id
user_list = User.query \
.join(Permission, (User.id == Permission.u_id) & (Permission.op_id == op_id), isouter=True) \
.add_columns(User.id, User.username) \
.filter(Permission.u_id.is_(None))
new_u_ids = [user.id for user in user_list]
new_permissions = []
for u_id in new_u_ids:
if Permission.query.filter_by(u_id=u_id, op_id=op_id).first() is None:
new_permissions.append(Permission(u_id, operation.id, access_level))
db.session.add_all(new_permissions)
try:
db.session.commit()
return True
except IntegrityError as err:
db.session.rollback()
logging.debug("Error writing to db: %s", err)
db.session.close()
def delete_user(email):
app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
with app.app_context():
user = User.query.filter_by(emailid=str(email)).first()
if user:
logging.info("User: %s deleted from db", email)
db.session.delete(user)
db.session.commit()
db.session.close()
return True
db.session.close()
return False
def add_user(email, username, password, fullname):
"""
on db level we add a user
"""
app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
with app.app_context():
user_email_exists = User.query.filter_by(emailid=str(email)).first()
user_name_exists = User.query.filter_by(username=str(username)).first()
if not user_email_exists and not user_name_exists:
db_user = User(email, username, password, fullname)
db.session.add(db_user)
db.session.commit()
db.session.close()
logging.info("Userdata: %s %s %s %s", email, username, password, fullname)
return True
else:
logging.info("%s already in db", user_name_exists)
return False
def get_user(email):
with app.app_context():
return User.query.filter_by(emailid=str(email)).first()
def get_operation(operation_name):
with app.app_context():
return Operation.query.filter_by(path=operation_name).first()
def add_operation(operation_name, description):
app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
with app.app_context():
operation_available = Operation.query.filter_by(path=operation_name).first()
if not operation_available:
operation = Operation(operation_name, description)
db.session.add(operation)
db.session.commit()
with fs.open_fs(mscolab_settings.OPERATIONS_DATA) as file_dir:
if not file_dir.exists(operation_name):
file_dir.makedir(operation_name)
file_dir.writetext(f'{operation_name}/main.ftml', mscolab_settings.STUB_CODE)
# initiate git
r = git.Repo.init(fs.path.join(mscolab_settings.DATA_DIR, 'filedata', operation_name))
r.git.clear_cache()
r.index.add(['main.ftml'])
r.index.commit("initial commit")
return True
else:
return False
def delete_operation(operation_name):
app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
with app.app_context():
operation = Operation.query.filter_by(path=operation_name).first()
if operation:
db.session.delete(operation)
db.session.commit()
db.session.close()
return True
db.session.close()
return False
def add_user_to_operation(path=None, access_level='admin', emailid=None):
""" on db level we add all users to the operation TEMPLATE for user handling"""
if None in (path, emailid):
return False
app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
with app.app_context():
operation = Operation.query.filter_by(path=path).first()
if operation:
user = User.query.filter_by(emailid=emailid).first()
if user:
new_permissions = [Permission(user.id, operation.id, access_level)]
db.session.add_all(new_permissions)
try:
db.session.commit()
return True
except IntegrityError as err:
db.session.rollback()
logging.debug("Error writing to db: %s", err)
db.session.close()
return False
def archive_operation(path=None, emailid=None):
""" this archives an existing operation """
if None in (path, emailid):
return False
app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
with app.app_context():
operation = Operation.query.filter_by(path=path).first()
if operation:
user = User.query.filter_by(emailid=emailid).first()
if user:
perm = Permission.query.filter_by(u_id=user.id, op_id=operation.id).first()
if perm is None:
return False
elif perm.access_level not in ["admin", "creator"]:
return False
operation.active = False
db.session.commit()
def seed_data():
# create users
users = [{
'username': 'a',
'id': 8,
'password': 'a',
'emailid': 'a@notexisting.org',
'fullname': 'A User'
}, {
'username': 'b',
'id': 9,
'password': 'b',
'emailid': 'b@notexisting.org',
'fullname': 'B User'
}, {
'username': 'c',
'id': 10,
'password': 'c',
'emailid': 'c@notexisting.org',
'fullname': 'C User'
}, {
'username': 'd',
'id': 11,
'password': 'd',
'emailid': 'd@notexisting.org',
'fullname': 'D User'
}, {
'username': 'test1',
'id': 12,
'password': 'test1',
'emailid': 'test1@notexisting.org',
'fullname': 'Test User one'
}, {
'username': 'test2',
'id': 13,
'password': 'test2',
'emailid': 'test2@notexisting.org',
'fullname': 'Test User two'
}, {
'username': 'test3',
'id': 14,
'password': 'test3',
'emailid': 'test3@notexisting.org',
'fullname': 'Test User three'
}, {
'username': 'test4',
'id': 15,
'password': 'test4',
'emailid': 'test4@notexisting.org',
'fullname': 'Test User four'
}, {
'username': 'mscolab_user',
'id': 16,
'password': 'password',
'emailid': 'mscolab_user@notexisting.org',
'fullname': 'mscolab user'
}, {
'username': 'merge_waypoints_user',
'id': 17,
'password': 'password',
'emailid': 'merge_waypoints_user@notexisting.org',
'fullname': 'merge waypoints user'
}]
for user in users:
db_user = User(user['emailid'], user['username'], user['password'], user['fullname'])
db_user.id = user['id']
db.session.add(db_user)
# create operations
operations = [{
'id': 1,
'path': 'one',
'description': 'a, b',
'category': 'default'
}, {
'id': 2,
'path': 'two',
'description': 'b, c',
'category': 'default'
}, {
'id': 3,
'path': 'three',
'description': 'a, c',
'category': 'default'
}, {
'id': 4,
'path': 'four',
'description': 'd',
'category': 'default'
}, {
'id': 5,
'path': 'Admin_Test',
'description': 'Operation for testing admin window',
'category': 'default'
}, {
'id': 6,
'path': 'test_mscolab',
'description': 'Operation for testing mscolab main window',
'category': 'default'
}]
for operation in operations:
db_operation = Operation(operation['path'], operation['description'], operation['category'])
db_operation.id = operation['id']
db.session.add(db_operation)
# create permissions
permissions = [{
'u_id': 8,
'op_id': 1,
'access_level': "creator"
}, {
'u_id': 9,
'op_id': 1,
'access_level': "collaborator"
}, {
'u_id': 9,
'op_id': 2,
'access_level': "creator"
}, {
'u_id': 10,
'op_id': 2,
'access_level': "collaborator"
}, {
'u_id': 10,
'op_id': 3,
'access_level': "creator"
}, {
'u_id': 8,
'op_id': 3,
'access_level': "collaborator"
}, {
'u_id': 10,
'op_id': 1,
'access_level': "viewer"
}, {
'u_id': 11,
'op_id': 4,
'access_level': 'creator'
}, {
'u_id': 8,
'op_id': 4,
'access_level': 'admin'
}, {
'u_id': 13,
'op_id': 3,
'access_level': 'viewer'
}, {
'u_id': 12,
'op_id': 5,
'access_level': 'creator'
}, {
'u_id': 12,
'op_id': 3,
'access_level': 'collaborator'
}, {
'u_id': 15,
'op_id': 5,
'access_level': 'viewer'
}, {
'u_id': 14,
'op_id': 3,
'access_level': 'collaborator'
}, {
'u_id': 15,
'op_id': 3,
'access_level': 'collaborator'
}, {
'u_id': 16,
'op_id': 6,
'access_level': 'creator'
}, {
'u_id': 17,
'op_id': 6,
'access_level': 'admin'
}]
for perm in permissions:
db_perm = Permission(perm['u_id'], perm['op_id'], perm['access_level'])
db.session.add(db_perm)
db.session.commit()
db.session.close()
with fs.open_fs(mscolab_settings.OPERATIONS_DATA) as file_dir:
file_paths = ['one', 'two', 'three', 'four', 'Admin_Test', 'test_mscolab']
for file_path in file_paths:
file_dir.makedir(file_path)
file_dir.writetext(f'{file_path}/main.ftml', mscolab_settings.STUB_CODE)
# initiate git
r = git.Repo.init(fs.path.join(mscolab_settings.DATA_DIR, 'filedata', file_path))
r.git.clear_cache()
r.index.add(['main.ftml'])
r.index.commit("initial commit")