Skip to content

Commit

Permalink
fix linting in BU-ISCIII#181
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-VM committed Jan 21, 2025
1 parent 0dc3f4f commit a582002
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
6 changes: 3 additions & 3 deletions core/api/utils/common_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def get_analysis_defined(s_obj):
bioinfo_analysis_fieldID__property_name="analysis_date", sample=s_obj
).values_list("value", flat=True)


def add_sample_state_history(sample_obj, state_id, error_name=None):
"""
Adds a new state history entry for a sample and marks previous states as not current.
Since all
"""
# Validate the state exists
state_obj = None
Expand All @@ -44,10 +44,10 @@ def add_sample_state_history(sample_obj, state_id, error_name=None):
if state_id:
state_obj = core.models.SampleState.objects.filter(pk=state_id).last()
else:
# If no state is defined, use the last record for that sample
# If no state is defined, use the last record for that sample
state_obj = (
core.models.SampleStateHistory.objects.filter(sample=sample_obj)
.order_by('-changed_at')
.order_by("-changed_at")
.first()
)

Expand Down
75 changes: 38 additions & 37 deletions core/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
)
from rest_framework import serializers
from django.http import QueryDict
from django.utils import timezone

# Local imports
import core.urls
Expand Down Expand Up @@ -119,7 +118,9 @@ def create_sample_data(request):
)
if sample_obj:
error = "Sample already defined"
error_name_value = core.models.ErrorName.objects.filter(error_name=error).first()
error_name_value = core.models.ErrorName.objects.filter(
error_name=error
).first()
if error_name_value:
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
Expand All @@ -146,9 +147,13 @@ def create_sample_data(request):
# Add initial state history (after creating the sample)
is_first_entry = not core.models.SampleStateHistory.objects.filter(
sample=sample_obj
).exists()
).exists()
if is_first_entry:
error_name_value = core.models.ErrorName.objects.filter(pk=1).values_list('error_name',flat=True).first()
error_name_value = (
core.models.ErrorName.objects.filter(pk=1)
.values_list("error_name", flat=True)
.first()
)
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=split_data["sample"]["state"],
Expand All @@ -166,11 +171,11 @@ def create_sample_data(request):
split_data["ena"], "ena", schema_obj, sample_id
)
if "ERROR" in result:
error = 'Failed to store data'
error = "Failed to store data"
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=state_id,
error_name=error,
error_name=error,
)
return Response(result, status=status.HTTP_206_PARTIAL_CONTENT)
# check that the ena_sample_accession is not empty or "Not Provided"
Expand All @@ -184,14 +189,14 @@ def create_sample_data(request):
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=state_id,
error_name='No error',
error_name="No error",
)
except ValueError:
error = 'Failed to update sample state history due to invalid state or missing data'
error = "Failed to update sample state history due to invalid state or missing data"
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=state_id,
error_name=error,
error_name=error,
)
return Response(
{"ERROR": error}, status=status.HTTP_400_BAD_REQUEST
Expand All @@ -201,11 +206,9 @@ def create_sample_data(request):
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=state_id,
error_name=error,
)
return Response(
{"ERROR": error}, status=status.HTTP_400_BAD_REQUEST
error_name=error,
)
return Response({"ERROR": error}, status=status.HTTP_400_BAD_REQUEST)

# Save GISAID info if included
if len(split_data["gisaid"]) > 0:
Expand All @@ -219,27 +222,27 @@ def create_sample_data(request):
split_data["gisaid"], "gisaid", schema_obj, sample_id
)
if "ERROR" in result:
error = 'Failed to store data'
error = "Failed to store data"
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=state_id,
error_name=error,
)
error_name=error,
)
return Response(result, status=status.HTTP_400_BAD_REQUEST)
# Save entry in update state table
sample_obj.update_state("Gisaid")
try:
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=state_id,
error_name='No error',
error_name="No error",
)
except ValueError as e:
error = 'Failed to update sample state history due to invalid state or missing data'
error = "Failed to update sample state history due to invalid state or missing data"
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=state_id,
error_name=error,
error_name=error,
)
return Response(
{"ERROR": str(e)}, status=status.HTTP_400_BAD_REQUEST
Expand Down Expand Up @@ -449,16 +452,14 @@ def create_metadata_value(request):
)
try:
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=state_id,
error_name='No error'
sample_obj, state_id=state_id, error_name="No error"
)
except ValueError as e:
error = 'Failed to store data'
error = "Failed to store data"
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=state_id,
error_name=error,
error_name=error,
)
return Response({"ERROR": str(e)}, status=status.HTTP_400_BAD_REQUEST)

Expand Down Expand Up @@ -561,7 +562,6 @@ def create_metadata_value(request):
},
),
)

# TODO: still need to add update_sample_history here
@authentication_classes([SessionAuthentication, BasicAuthentication])
@api_view(["POST"])
Expand Down Expand Up @@ -681,43 +681,44 @@ def update_state(request):
errors = []
if sample_obj is None:
return Response(
core.config.ERROR_SAMPLE_NOT_DEFINED,
status=status.HTTP_400_BAD_REQUEST
core.config.ERROR_SAMPLE_NOT_DEFINED, status=status.HTTP_400_BAD_REQUEST
)

# Validate the new state exists
new_state_obj = core.models.SampleState.objects.filter(
state=data["state"]
).last()

# Catch errors during the execution
if not new_state_obj:
errors.append({
'error_name': "The specified state does not exist",
'http_status': status.HTTP_400_BAD_REQUEST }
errors.append(
{
"error_name": "The specified state does not exist",
"http_status": status.HTTP_400_BAD_REQUEST,
}
)

# Add new sample state in history records
try:
core.api.utils.common_functions.add_sample_state_history(
sample_obj=sample_obj,
state_id= new_state_obj.pk if new_state_obj else None,
error_name='No errors'
state_id=new_state_obj.pk if new_state_obj else None,
error_name="No errors",
)
except ValueError as e:
error = 'Failed to update sample state history due to invalid state or missing data'
error = "Failed to update sample state history due to invalid state or missing data"
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=new_state_obj.pk if new_state_obj else None,
error_name=error,
error_name=error,
)
return Response({"ERROR": str(e)}, status=status.HTTP_400_BAD_REQUEST)

# Return response of error encountered while updating sample state
if len(errors) > 0:
return Response(
errors[0]['error_name'],
status=errors[0]['http_status'],
errors[0]["error_name"],
status=errors[0]["http_status"],
)
else:
return Response(
Expand Down
1 change: 1 addition & 0 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ def get_state(self):
if self.state:
return "%s" % (self.state.get_state())
return None

def update_state(self):
if not SampleState.objects.filter(state__exact=self.state).exists():
return False
Expand Down

0 comments on commit a582002

Please sign in to comment.