Skip to content

Commit

Permalink
AREG-68 - improve services and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
D-GopalKrishna committed May 15, 2024
1 parent 3efb340 commit b58e62d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -95,28 +95,25 @@ 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():
"""
Export all fields of all antibodies to a CSV file - Only for admin users
"""
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)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import time
from datetime import datetime

from portal.settings import MAX_TRIES

Expand All @@ -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
2 changes: 1 addition & 1 deletion applications/portal/backend/api/services/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit b58e62d

Please sign in to comment.