From 3b5d29912887f93d1aa6980b0c1e4dcf8ccd05fd Mon Sep 17 00:00:00 2001 From: Tim Lorsbach Date: Thu, 7 May 2026 11:19:30 +0200 Subject: [PATCH] API PES --- epauth/views.py | 5 ++++ epdb/legacy_api.py | 66 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/epauth/views.py b/epauth/views.py index e0a5d795..d0243f7d 100644 --- a/epauth/views.py +++ b/epauth/views.py @@ -116,6 +116,11 @@ def get_access_token_from_request(request, scopes=None): """ Get an access token from the request using MSAL token cache. """ + + # Check if auth via Access Token + if request.headers.get("Authorization"): + return {"access_token": request.headers.get("Authorization").split(" ")[1]} + if scopes is None: scopes = s.MS_ENTRA_SCOPES diff --git a/epdb/legacy_api.py b/epdb/legacy_api.py index 7c00a657..a8e62d7a 100644 --- a/epdb/legacy_api.py +++ b/epdb/legacy_api.py @@ -820,6 +820,7 @@ class CreateCompound(Schema): compoundName: str | None = None compoundDescription: str | None = None inchi: str | None = None + pesLink: str | None = None @router.post("/package/{uuid:package_uuid}/compound") @@ -831,9 +832,28 @@ def create_package_compound( try: p = get_package_for_write(request.user, package_uuid) # inchi is not used atm - c = Compound.create( - p, c.compoundSmiles, c.compoundName, c.compoundDescription, inchi=c.inchi - ) + + if c.pesLink is not None: + from bayer.views import fetch_pes + from bayer.models import PESCompound + + try: + pes_data = fetch_pes(request, c.pesLink) + except ValueError as e: + return 400, {"message": f"Could not fetch PES data for {c.pesLink}"} + + classification = pes_data.get("classificationLevel", "") + if "secret" == classification.lower(): + data_pools = pes_data.get("dataPools") + if data_pools: + if s.DATA_POOL_MAPPING[p.data_pool.name] not in data_pools: + return 400, { "messsage": f"PES data pool {s.DATA_POOL_MAPPING[p.data_pool.name]} not found in PES data"} + + c = PESCompound.create(p, pes_data, c.compoundName, c.compoundDescription) + else: + c = Compound.create( + p, c.compoundSmiles, c.compoundName, c.compoundDescription, inchi=c.inchi + ) return redirect(c.url) except ValueError as e: return 400, {"message": str(e)} @@ -1854,6 +1874,7 @@ class CreateNode(Schema): nodeName: str | None = None nodeReason: str | None = None nodeDepth: str | None = None + pesLink: str | None = None @router.post( @@ -1865,14 +1886,43 @@ def add_pathway_node(request, package_uuid, pathway_uuid, n: Form[CreateNode]): p = get_package_for_write(request.user, package_uuid) pw = Pathway.objects.get(package=p, uuid=pathway_uuid) - if n.nodeDepth is not None and n.nodeDepth.strip() != "": - node_depth = int(n.nodeDepth) + if n.pesLink: + from bayer.views import fetch_pes + from bayer.models import PESCompound + + try: + pes_data = fetch_pes(request, c.pesLink) + except ValueError as e: + return 400, {"message": f"Could not fetch PES data for {c.pesLink}"} + + classification = pes_data.get("classificationLevel", "") + if "secret" == classification.lower(): + data_pools = pes_data.get("dataPools") + if data_pools: + if s.DATA_POOL_MAPPING[p.data_pool.name] not in data_pools: + return 400, { "messsage": f"PES data pool {s.DATA_POOL_MAPPING[p.data_pool.name]} not found in PES data"} + + c = PESCompound.create(p, pes_data, c.compoundName, c.compoundDescription) + + node = Node() + node.stereo_removed = False + node.pathway = pw + node.depth = 0 + + node.default_node_label = c.default_structure + node.save() + + node.node_labels.add(c.default_structure) + node.save() else: - node_depth = -1 + if n.nodeDepth is not None and n.nodeDepth.strip() != "": + node_depth = int(n.nodeDepth) + else: + node_depth = -1 - n = Node.create(pw, n.nodeAsSmiles, node_depth, n.nodeName, n.nodeReason) + node = Node.create(pw, n.nodeAsSmiles, node_depth, n.nodeName, n.nodeReason) - return redirect(n.url) + return redirect(node.url) except ValueError: return 403, {"message": "Adding node failed!"}