def add_operation(operation_name, description): app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) 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.MSCOLAB_DATA_DIR) 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 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 db.init_app(app) 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.MSCOLAB_DATA_DIR) 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(f"Error writing to db: {err}") db.session.close()
def create_operation(self, path, description, user, content=None, category="default"): """ path: path to the operation description: description of the operation """ # set codes on these later if path.find("/") != -1 or path.find("\\") != -1 or (" " in path): logging.debug("malicious request: %s", user) return False proj_available = Operation.query.filter_by(path=path).first() if proj_available is not None: return False operation = Operation(path, description, category) db.session.add(operation) db.session.flush() operation_id = operation.id # this is the only insertion with "creator" access_level perm = Permission(user.id, operation_id, "creator") db.session.add(perm) db.session.commit() data = fs.open_fs(self.data_dir) data.makedir(operation.path) operation_file = data.open( fs.path.combine(operation.path, 'main.ftml'), 'w') if content is not None: operation_file.write(content) else: operation_file.write(mscolab_settings.STUB_CODE) operation_path = fs.path.combine(self.data_dir, operation.path) r = git.Repo.init(operation_path) r.git.clear_cache() r.index.add(['main.ftml']) r.index.commit("initial commit") return True
def seed_data(): app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) with app.app_context(): # create users users = [{ 'username': '******', 'id': 8, 'password': '******', 'emailid': 'a' }, { 'username': '******', 'id': 9, 'password': '******', 'emailid': 'b' }, { 'username': '******', 'id': 10, 'password': '******', 'emailid': 'c' }, { 'username': '******', 'id': 11, 'password': '******', 'emailid': 'd' }, { 'username': '******', 'id': 12, 'password': '******', 'emailid': 'test1' }, { 'username': '******', 'id': 13, 'password': '******', 'emailid': 'test2' }, { 'username': '******', 'id': 14, 'password': '******', 'emailid': 'test3' }, { 'username': '******', 'id': 15, 'password': '******', 'emailid': 'test4' }, { 'username': '******', 'id': 16, 'password': '******', 'emailid': 'mscolab_user' }, { 'username': '******', 'id': 17, 'password': '******', 'emailid': 'merge_waypoints_user' }] for user in users: db_user = User(user['emailid'], user['username'], user['password']) 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.MSCOLAB_DATA_DIR) 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")