forked from enviPath/enviPy
Return proper http response instead of error
This commit is contained in:
@ -2,8 +2,7 @@ import base64
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
from django.conf import settings as s
|
from django.conf import settings as s
|
||||||
from django.core.exceptions import BadRequest
|
from django.http import HttpResponse, HttpResponseBadRequest
|
||||||
from django.http import HttpResponse
|
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
|
|
||||||
from bayer.models import PESCompound
|
from bayer.models import PESCompound
|
||||||
@ -23,7 +22,7 @@ def create_pes(request, package_uuid):
|
|||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
|
|
||||||
if current_package.classification_level == Package.Classification.INTERNAL:
|
if current_package.classification_level == Package.Classification.INTERNAL:
|
||||||
raise BadRequest("Cannot create PESs for internal packages.")
|
raise HttpResponseBadRequest("Cannot create PESs for internal packages.")
|
||||||
|
|
||||||
compound_name = request.POST.get('compound-name')
|
compound_name = request.POST.get('compound-name')
|
||||||
compound_description = request.POST.get('compound-description')
|
compound_description = request.POST.get('compound-description')
|
||||||
@ -33,25 +32,25 @@ 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 BadRequest(f"Could not fetch PES data for {pes_link}")
|
return HttpResponseBadRequest(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 BadRequest("Cannot create PESs for non-secret packages.")
|
return HttpResponseBadRequest("Cannot create PESs for 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 s.DATA_POOL_MAPPING[current_package.data_pool.name] not in data_pools:
|
||||||
return BadRequest(
|
return HttpResponseBadRequest(
|
||||||
f"PES data pool {s.DATA_POOL_MAPPING[current_package.data_pool.name]} not found in PES data")
|
f"PES data pool {s.DATA_POOL_MAPPING[current_package.data_pool.name]} not found in PES data")
|
||||||
|
|
||||||
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 BadRequest("Please provide a PES link.")
|
return HttpResponseBadRequest("Please provide a PES link.")
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -65,7 +64,7 @@ def create_pes_node(request, package_uuid, pathway_uuid):
|
|||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
|
|
||||||
if current_package.classification_level == Package.Classification.INTERNAL:
|
if current_package.classification_level == Package.Classification.INTERNAL:
|
||||||
raise BadRequest("Cannot create PESs for internal packages.")
|
raise HttpResponseBadRequest("Cannot create PESs for internal packages.")
|
||||||
|
|
||||||
compound_name = request.POST.get('compound-name')
|
compound_name = request.POST.get('compound-name')
|
||||||
compound_description = request.POST.get('compound-description')
|
compound_description = request.POST.get('compound-description')
|
||||||
@ -75,18 +74,18 @@ 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 BadRequest(f"Could not fetch PES data for {pes_link}")
|
return HttpResponseBadRequest(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 BadRequest("Cannot create PESs for non-secret packages.")
|
return HttpResponseBadRequest("Cannot create PESs for 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 s.DATA_POOL_MAPPING[current_package.data_pool.name] not in data_pools:
|
||||||
return BadRequest(
|
return HttpResponseBadRequest(
|
||||||
f"PES data pool {s.DATA_POOL_MAPPING[current_package.data_pool.name]} not found in PES data")
|
f"PES data pool {s.DATA_POOL_MAPPING[current_package.data_pool.name]} not found in PES data")
|
||||||
|
|
||||||
pes = PESCompound.create(current_package, pes_data, compound_name, compound_description)
|
pes = PESCompound.create(current_package, pes_data, compound_name, compound_description)
|
||||||
@ -109,7 +108,7 @@ def create_pes_node(request, package_uuid, pathway_uuid):
|
|||||||
return redirect(current_pathway.url)
|
return redirect(current_pathway.url)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return BadRequest("Please provide a PES link.")
|
return HttpResponseBadRequest("Please provide a PES link.")
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user