forked from enviPath/enviPy
Provide proper Error Pages
This commit is contained in:
@ -2,7 +2,7 @@ import base64
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
from django.conf import settings as s
|
from django.conf import settings as s
|
||||||
from django.http import HttpResponse, HttpResponseBadRequest
|
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotAllowed
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
|
|
||||||
from bayer.models import PESCompound
|
from bayer.models import PESCompound
|
||||||
@ -36,27 +36,49 @@ def create_pes(request, package_uuid):
|
|||||||
try:
|
try:
|
||||||
pes_data = fetch_pes(request, pes_link)
|
pes_data = fetch_pes(request, pes_link)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
return HttpResponseBadRequest(f"Could not fetch PES data for {pes_link}")
|
return error(
|
||||||
|
request,
|
||||||
|
"Could not fetch PES",
|
||||||
|
f"Could not fetch PES data for {pes_link}"
|
||||||
|
)
|
||||||
|
|
||||||
classification = pes_data.get("classificationLevel", "")
|
classification = pes_data.get("classificationLevel", "")
|
||||||
if "secret" == classification.lower():
|
if "secret" == classification.lower():
|
||||||
|
|
||||||
if current_package.classification_level != Package.Classification.SECRET:
|
if current_package.classification_level != Package.Classification.SECRET:
|
||||||
return HttpResponseBadRequest("Cannot create PESs for non-secret packages.")
|
return error(
|
||||||
|
request,
|
||||||
|
"Classification Mismatch!",
|
||||||
|
"Cannot create secret PESs in non-secret packages."
|
||||||
|
)
|
||||||
|
|
||||||
data_pools = pes_data.get("dataPools")
|
data_pools = pes_data.get("dataPools")
|
||||||
if data_pools:
|
if data_pools:
|
||||||
if s.DATA_POOL_MAPPING[current_package.data_pool.name] not in data_pools:
|
if (current_package.data_pool.name not in s.DATA_POOL_MAPPING
|
||||||
return HttpResponseBadRequest(
|
or s.DATA_POOL_MAPPING[current_package.data_pool.name] not in data_pools):
|
||||||
f"PES data pool {s.DATA_POOL_MAPPING[current_package.data_pool.name]} not found in PES data")
|
|
||||||
|
if current_package.data_pool.name not in s.DATA_POOL_MAPPING:
|
||||||
|
detail = f"Data pool {current_package.data_pool.name} not found in Mapping."
|
||||||
|
else:
|
||||||
|
detail = f"PES data pool {s.DATA_POOL_MAPPING[current_package.data_pool.name]} not found in PES data"
|
||||||
|
|
||||||
|
return error(
|
||||||
|
request,
|
||||||
|
"Invalid PES data",
|
||||||
|
detail
|
||||||
|
)
|
||||||
|
|
||||||
pes = PESCompound.create(current_package, pes_data, compound_name, compound_description)
|
pes = PESCompound.create(current_package, pes_data, compound_name, compound_description)
|
||||||
|
|
||||||
return redirect(pes.url)
|
return redirect(pes.url)
|
||||||
else:
|
else:
|
||||||
return HttpResponseBadRequest("Please provide a PES link.")
|
return error(
|
||||||
|
request,
|
||||||
|
"No PES link received",
|
||||||
|
"Please provide a PES link."
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
pass
|
return HttpResponseNotAllowed(["POST"])
|
||||||
|
|
||||||
|
|
||||||
@package_permission_required()
|
@package_permission_required()
|
||||||
@ -82,23 +104,45 @@ def create_pes_node(request, package_uuid, pathway_uuid):
|
|||||||
try:
|
try:
|
||||||
pes_data = fetch_pes(request, pes_link)
|
pes_data = fetch_pes(request, pes_link)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
return HttpResponseBadRequest(f"Could not fetch PES data for {pes_link}")
|
return error(
|
||||||
|
request,
|
||||||
|
"Could not fetch PES",
|
||||||
|
f"Could not fetch PES data for {pes_link}"
|
||||||
|
)
|
||||||
|
|
||||||
classification = pes_data.get("classificationLevel", "")
|
classification = pes_data.get("classificationLevel", "")
|
||||||
if "secret" == classification.lower():
|
if "secret" == classification.lower():
|
||||||
|
|
||||||
if current_package.classification_level != Package.Classification.SECRET:
|
if current_package.classification_level != Package.Classification.SECRET:
|
||||||
return HttpResponseBadRequest("Cannot create PESs for non-secret packages.")
|
return error(
|
||||||
|
request,
|
||||||
|
"Classification Mismatch!",
|
||||||
|
"Cannot create secret PESs in non-secret packages."
|
||||||
|
)
|
||||||
|
|
||||||
data_pools = pes_data.get("dataPools")
|
data_pools = pes_data.get("dataPools")
|
||||||
if data_pools:
|
if data_pools:
|
||||||
if s.DATA_POOL_MAPPING[current_package.data_pool.name] not in data_pools:
|
if (current_package.data_pool.name not in s.DATA_POOL_MAPPING
|
||||||
return HttpResponseBadRequest(
|
or s.DATA_POOL_MAPPING[current_package.data_pool.name] not in data_pools):
|
||||||
f"PES data pool {s.DATA_POOL_MAPPING[current_package.data_pool.name]} not found in PES data")
|
|
||||||
|
if current_package.data_pool.name not in s.DATA_POOL_MAPPING:
|
||||||
|
detail = f"Data pool {current_package.data_pool.name} not found in Mapping."
|
||||||
|
else:
|
||||||
|
detail = f"PES data pool {s.DATA_POOL_MAPPING[current_package.data_pool.name]} not found in PES data"
|
||||||
|
|
||||||
|
return error(
|
||||||
|
request,
|
||||||
|
"Invalid PES data",
|
||||||
|
detail
|
||||||
|
)
|
||||||
|
|
||||||
pes = PESCompound.create(current_package, pes_data, compound_name, compound_description)
|
pes = PESCompound.create(current_package, pes_data, compound_name, compound_description)
|
||||||
|
|
||||||
node_qs = Node.objects.filter(pathway=current_pathway, default_node_label=pes.default_structure)
|
node_qs = Node.objects.filter(
|
||||||
|
pathway=current_pathway,
|
||||||
|
default_node_label=pes.default_structure
|
||||||
|
)
|
||||||
|
|
||||||
if node_qs.exists():
|
if node_qs.exists():
|
||||||
return redirect(current_pathway.url)
|
return redirect(current_pathway.url)
|
||||||
|
|
||||||
@ -116,9 +160,13 @@ def create_pes_node(request, package_uuid, pathway_uuid):
|
|||||||
return redirect(current_pathway.url)
|
return redirect(current_pathway.url)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return HttpResponseBadRequest("Please provide a PES link.")
|
return error(
|
||||||
|
request,
|
||||||
|
"No PES link received",
|
||||||
|
"Please provide a PES link."
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
pass
|
return HttpResponseNotAllowed(["POST"])
|
||||||
|
|
||||||
|
|
||||||
def fetch_pes(request, pes_url) -> dict:
|
def fetch_pes(request, pes_url) -> dict:
|
||||||
|
|||||||
Reference in New Issue
Block a user