def insert_setting(user_id: int, data: dict): """ HTTP `POST` route for insert a single user setting resource. Args: user_id (int): Public ID of the user. data (UserModel.SCHEMA): Insert data of a new user. Raises: ManagerGetError: If the inserted user could not be found after inserting. ManagerInsertError: If something went wrong during insertion. Returns: InsertSingleResponse: Insert response with the new user and its identifier. """ settings_manager: UserSettingsManager = UserSettingsManager( database_manager=current_app.database_manager) try: settings_manager.insert(data) setting: UserSettingModel = settings_manager.get_user_setting( user_id=user_id, resource=data.get('identifier')) except ManagerGetError as err: return abort(404, err.message) except ManagerInsertError as err: return abort(400, err.message) api_response = InsertSingleResponse(raw=UserSettingModel.to_dict(setting), result_id=setting.resource, url=request.url, model=UserSettingModel.MODEL) return api_response.make_response( prefix=f'users/{user_id}/settings/{setting.resource}')
def insert_category(data: dict): """ HTTP `POST` route for insert a single category resource. Args: data (CategoryModel.SCHEMA): Insert data of a new category. Raises: ManagerGetError: If the inserted resource could not be found after inserting. ManagerInsertError: If something went wrong during insertion. Returns: InsertSingleResponse: Insert response with the new category and its public_id. """ category_manager: CategoryManager = CategoryManager( database_manager=current_app.database_manager) data.setdefault('creation_time', datetime.utcnow()) try: result_id: PublicID = category_manager.insert(data) raw_doc = category_manager.get(public_id=result_id) except ManagerGetError as err: return abort(404, err.message) except ManagerInsertError as err: return abort(400, err.message) api_response = InsertSingleResponse(result_id, raw=CategoryModel.to_json(raw_doc), url=request.url, model=CategoryModel.MODEL) return api_response.make_response(prefix='categories')
def insert_group(data: dict): """ HTTP `POST` route for insert a single group resource. Args: data (UserGroupModel.SCHEMA): Insert data of a new group. Raises: ManagerGetError: If the inserted group could not be found after inserting. ManagerInsertError: If something went wrong during insertion. Returns: InsertSingleResponse: Insert response with the new group and its public_id. """ group_manager: GroupManager = GroupManager(database_manager=current_app.database_manager, right_manager=RightManager(rights)) try: result_id: PublicID = group_manager.insert(data) group = group_manager.get(public_id=result_id) except ManagerGetError as err: return abort(404, err.message) except ManagerInsertError as err: return abort(400, err.message) api_response = InsertSingleResponse(result_id=result_id, raw=UserGroupModel.to_dict(group), url=request.url, model=UserGroupModel.MODEL) return api_response.make_response(prefix='groups')
def insert_user(data: dict): """ HTTP `POST` route for insert a single user resource. Args: data (UserModel.SCHEMA): Insert data of a new user. Raises: ManagerGetError: If the inserted user could not be found after inserting. ManagerInsertError: If something went wrong during insertion. Returns: InsertSingleResponse: Insert response with the new user and its public_id. """ user_manager: UserManager = UserManager( database_manager=current_app.database_manager) security_manager: SecurityManager = SecurityManager( database_manager=current_app.database_manager) try: data['password'] = security_manager.generate_hmac(data['password']) result_id: PublicID = user_manager.insert(data) user = user_manager.get(public_id=result_id) except ManagerGetError as err: return abort(404, err.message) except ManagerInsertError as err: return abort(400, err.message) api_response = InsertSingleResponse(result_id=result_id, raw=UserModel.to_dict(user), url=request.url, model=UserModel.MODEL) return api_response.make_response(prefix='users')
def add_new_file(request_user: UserModel): """ This method saves a file to the specified section of the document for storing workflow data. Any existing value that matches filename and the metadata is deleted. Before saving a value. GridFS document under the specified key is deleted. For Example: Create a unique media file element: - Folders in the same directory are unique. - The Folder-Name can exist in different directories Create sub-folders: - Selected folder is considered as parent This also applies for files File: File is stored under 'request.files.get('files')' Metadata: Metadata are stored under 'request.form["Metadata"]' Raises: MediaFileManagerGetError: If the file could not be found. MediaFileManagerInsertError: If something went wrong during insert. Args: request_user (UserModel): the instance of the started user Returns: New MediaFile. """ try: file = get_file_in_request('file') filter_metadata = generate_metadata_filter('metadata', request) filter_metadata.update({'filename': file.filename}) metadata = get_element_from_data_request('metadata', request) # Check if file exists is_exist_file = media_file_manager.exist_file(filter_metadata) exist = None if is_exist_file: exist = media_file_manager.get_file(filter_metadata) media_file_manager.delete_file(exist['public_id']) # If file exist overwrite the references from previous file if exist: metadata['reference'] = exist['metadata']['reference'] metadata['reference_type'] = exist['metadata']['reference_type'] metadata['author_id'] = request_user.public_id metadata['mime_type'] = file.mimetype result = media_file_manager.insert_file(data=file, metadata=metadata) except (MediaFileManagerInsertError, MediaFileManagerGetError): return abort(500) api_response = InsertSingleResponse(raw=result, result_id=result['public_id'], url=request.url) return api_response.make_response(prefix='library')
def insert_link(request_user: UserModel): link_manager = ObjectLinkManager( database_manager=current_app.database_manager) data = request.json try: result_id = link_manager.insert( data, user=request_user, permission=AccessControlPermission.CREATE) raw_doc = link_manager.get(public_id=result_id) except (ManagerGetError, ManagerInsertError) as err: return abort(400, err.message) except AccessDeniedError as err: return abort(403, err.message) api_response = InsertSingleResponse(result_id=result_id, raw=ObjectLinkModel.to_json(raw_doc), url=request.url, model=ObjectLinkModel.MODEL) return api_response.make_response(prefix='objects/links')