async def upload_file(history_id: str, extension: str, request: Request, Authorize: AuthJWT = Depends()): """ upload file to google cloud storage :param history_id: id of history in the database :param extension: name of file :param file_bytes: file to uploading :return: """ Authorize.jwt_required() current_role = Repository.get_user_role_by_email( Authorize.get_jwt_subject()) if current_role == 'doctor': if history_id in HistoriesCollection.get_ids(): if not Repository.get_history_by_id(history_id).get('file_name'): # with open(file.filename, "wb") as buffer: # shutil.copyfileobj(file.file, buffer) # new_filename = str(uuid.uuid4()) + '.' + extension body = b'' async for chunk in request.stream(): body += chunk f = open(new_filename, "wb") f.write(body) f.close() # os.renames(file.filename, new_filename) shutil.move(new_filename, "app/disease_storage/") file_uploader = FileUploader() file_uploader.upload_file( 'app/disease_storage/' + new_filename, new_filename) Repository.update_history(history_id, {'file_name': new_filename}) return {'description': 'Success add file', 'result': True} else: return {'description': 'File already added', 'result': False} return { 'description': 'Can\'n found history by this id', 'result': False } return {'description': 'You need to be a doctor', 'result': False}
async def delete_file(history_id: str): filename = Repository.get_history_by_id(history_id).get('file_name') if filename: file_uploader = FileUploader() if filename in file_uploader.list_blobs(): file_uploader.delete_file(filename) else: Repository.update_history(history_id, {'file_name': None}) return {"description": "File not found", 'result': False} Repository.update_history(history_id, {'file_name': None}) return {"description": "File was successful deleted", 'result': True} return {"description": "File not found", 'result': False}
async def update_history(history_id: str, history: DiseaseHistoryScheme, Authorize: AuthJWT = Depends()): """ Update history with id - history_id :param history_id: id of history in the database :param history: dict of data to add to database :return: """ Authorize.jwt_required() current_role = Repository.get_user_role_by_email( Authorize.get_jwt_subject()) if current_role == 'doctor': result, status = Repository.update_history(history_id, history) return {'description': result, 'result': status} return {'description': 'You need to be a doctor', 'result': False}