From b58e62df2f6455358dba7694c6a1ce608be943ad Mon Sep 17 00:00:00 2001 From: "D. Gopal Krishna" Date: Wed, 15 May 2024 21:40:35 +0530 Subject: [PATCH] AREG-68 - improve services and fixes --- .../backend/api/controllers/antibody_controller.py | 11 ++++------- .../portal/backend/api/services/filesystem_service.py | 4 ++++ .../portal/backend/api/services/user_service.py | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/applications/portal/backend/api/controllers/antibody_controller.py b/applications/portal/backend/api/controllers/antibody_controller.py index af8e6a7a..e168cf48 100644 --- a/applications/portal/backend/api/controllers/antibody_controller.py +++ b/applications/portal/backend/api/controllers/antibody_controller.py @@ -10,7 +10,7 @@ from fastapi.encoders import jsonable_encoder from fastapi.responses import JSONResponse, RedirectResponse -from api.services import antibody_service +from api.services import antibody_service, filesystem_service from api.utilities.exceptions import AntibodyDataException from openapi.models import AddAntibody as AddAntibodyDTO, PaginatedAntibodies @@ -95,15 +95,12 @@ def get_antibodies_export(): # check if file exists and it is created within 24 hours # if not, generate a new file - if check_if_file_exists_and_recent(fname): + if filesystem_service.check_if_file_exists_and_recent(fname): generate_antibodies_csv_file(fname) return RedirectResponse("/" + fname) # return FileResponse(fname, filename="antibodies_export.csv") -def check_if_file_exists_and_recent(fname): - return not os.path.exists(fname) or (datetime.now() - datetime.fromtimestamp(os.path.getmtime(fname))).days > 1 - def get_antibodies_export_admin(): """ @@ -111,12 +108,12 @@ def get_antibodies_export_admin(): """ try: is_admin = check_if_user_is_admin() - except HTTPException as e: + except Exception as e: raise HTTPException(status_code=401, detail="Unauthorized: Only admin users can access this endpoint") from api.services.export_service import generate_all_antibodies_fields_to_csv fname = "static/www/antibodies_admin_export.csv" - if check_if_file_exists_and_recent(fname) and is_admin: + if filesystem_service.check_if_file_exists_and_recent(fname) and is_admin: generate_all_antibodies_fields_to_csv(fname) return RedirectResponse("/" + fname) diff --git a/applications/portal/backend/api/services/filesystem_service.py b/applications/portal/backend/api/services/filesystem_service.py index 5dff8e5a..4f6841ec 100644 --- a/applications/portal/backend/api/services/filesystem_service.py +++ b/applications/portal/backend/api/services/filesystem_service.py @@ -1,5 +1,6 @@ import os import time +from datetime import datetime from portal.settings import MAX_TRIES @@ -12,3 +13,6 @@ def replace_file(previous_path, new_path, max_tries=MAX_TRIES): time.sleep(1) tries += 1 os.rename(new_path, previous_path) + +def check_if_file_exists_and_recent(fname): + return not os.path.exists(fname) or (datetime.now() - datetime.fromtimestamp(os.path.getmtime(fname))).days > 1 diff --git a/applications/portal/backend/api/services/user_service.py b/applications/portal/backend/api/services/user_service.py index c2bfc775..07118f03 100644 --- a/applications/portal/backend/api/services/user_service.py +++ b/applications/portal/backend/api/services/user_service.py @@ -19,6 +19,6 @@ def check_if_user_is_admin(): """ auth = KeycloakService() if auth.current_user_has_realm_role("administrator"): - return {} + return True raise Exception("User is not an admin")