Skip to content

Commit

Permalink
fix black linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-VM committed Dec 17, 2024
1 parent aa182f0 commit 2324895
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
8 changes: 7 additions & 1 deletion core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ class ConfigSettingAdmin(admin.ModelAdmin):


class SampleStateHistoryAdmin(admin.ModelAdmin):
list_display = ["is_current", custom_date_format, "sample_id", "state_id", "error_name_id"]
list_display = [
"is_current",
custom_date_format,
"sample_id",
"state_id",
"error_name_id",
]


class EffectAdmin(admin.ModelAdmin):
Expand Down
7 changes: 4 additions & 3 deletions core/api/utils/common_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ def add_sample_state_history(sample_obj, state_id, error_name=None):
# Assign the 'No Error' entry with pk=1 as default
error_obj = core.models.ErrorName.objects.filter(pk=1).first()
if not error_obj:
raise ValueError("Default error entry with pk=1 does not exist in the database.")
raise ValueError(
"Default error entry with pk=1 does not exist in the database."
)

# Mark previous states as not current
core.models.SampleStateHistory.objects.filter(
sample=sample_obj,
is_current=True
sample=sample_obj, is_current=True
).update(is_current=False)

# Add the new state history
Expand Down
15 changes: 6 additions & 9 deletions core/api/utils/metadata_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,17 @@ def store_metadata_values(s_data, schema_obj, analysis_date):
sequencing_sample_id__iexact=s_data["sequencing_sample_id"]
).last()
for field, value in s_data.items():
property_name = (
core.models.SchemaProperties.objects.filter(
schemaID=schema_obj, property__iexact=field
)
.last()
)
property_name = core.models.SchemaProperties.objects.filter(
schemaID=schema_obj, property__iexact=field
).last()
data = {
"value": value,
"sample": sample_obj.id,
"schema_property": property_name.id,
"analysis_date": analysis_date
"analysis_date": analysis_date,
}
meta_value_serializer = (
core.api.serializers.CreateMetadataValueSerializer(data=data)
meta_value_serializer = core.api.serializers.CreateMetadataValueSerializer(
data=data
)
if not meta_value_serializer.is_valid():
return {
Expand Down
32 changes: 21 additions & 11 deletions core/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ def create_sample_data(request):
sample_id = sample_obj.get_sample_id()

# Add initial state history (after creating the sample)
is_first_entry = not core.models.SampleStateHistory.objects.filter(sample=sample_obj).exists()
is_first_entry = not core.models.SampleStateHistory.objects.filter(
sample=sample_obj
).exists()
if is_first_entry:
error_name_obj = core.models.ErrorName.objects.filter(pk=1).first()
try:
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=split_data["sample"]["state"],
error_name=error_name_obj
error_name=error_name_obj,
)
except ValueError as e:
return Response({"ERROR": str(e)}, status=status.HTTP_400_BAD_REQUEST)
Expand Down Expand Up @@ -171,10 +173,12 @@ def create_sample_data(request):
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=state_id,
error_name=None # TODO: do not know what to do with this at this point
error_name=None, # TODO: do not know what to do with this at this point
)
except ValueError as e:
return Response({"ERROR": str(e)}, status=status.HTTP_400_BAD_REQUEST)
return Response(
{"ERROR": str(e)}, status=status.HTTP_400_BAD_REQUEST
)

# Save GISAID info if included
if len(split_data["gisaid"]) > 0:
Expand All @@ -195,10 +199,12 @@ def create_sample_data(request):
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=state_id,
error_name=None # TODO: do not know what to do with this at this point
error_name=None, # TODO: do not know what to do with this at this point
)
except ValueError as e:
return Response({"ERROR": str(e)}, status=status.HTTP_400_BAD_REQUEST)
return Response(
{"ERROR": str(e)}, status=status.HTTP_400_BAD_REQUEST
)

# Save AUTHOR info if included
if len(split_data["author"]) > 0:
Expand Down Expand Up @@ -406,7 +412,7 @@ def create_metadata_value(request):
core.api.utils.common_functions.add_sample_state_history(
sample_obj,
state_id=state_id,
error_name=None # TODO: do not know what to do with this at this point
error_name=None, # TODO: do not know what to do with this at this point
)
except ValueError as e:
return Response({"ERROR": str(e)}, status=status.HTTP_400_BAD_REQUEST)
Expand Down Expand Up @@ -657,13 +663,15 @@ def update_state(request):
# FIXME: it has been tested with error_code == None --> automatically_assigned value "other" (error_code_pk=21). Further testing is required.
e_data = {
"error_name": data.get("error_name", None),
"error_code": data.get("error_code", None)
"error_code": data.get("error_code", None),
}
error_name_obj = core.api.utils.common_functions.handle_sample_errors(dict_error=e_data)
error_name_obj = core.api.utils.common_functions.handle_sample_errors(
dict_error=e_data
)

# Create changed_at field if not provided
try:
current_time = data['changed_at']
current_time = data["changed_at"]
except KeyError:
current_time = timezone.now()

Expand All @@ -682,7 +690,9 @@ def update_state(request):
).update(is_current=False)

# Serialize and validate the state update
sample_state_serializer = core.api.serializers.SampleStateHistorySerializer(data=s_data)
sample_state_serializer = core.api.serializers.SampleStateHistorySerializer(
data=s_data
)
if not sample_state_serializer.is_valid():
return Response(
sample_state_serializer.errors, status=status.HTTP_400_BAD_REQUEST
Expand Down
10 changes: 4 additions & 6 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def create_new_value(self, data):
value=data["value"],
analysis_date=data["analysis_date"],
sample=data["sample_id"],
schema_property=data["schema_property_id"]
schema_property=data["schema_property_id"],
)
return new_value

Expand All @@ -320,14 +320,12 @@ class MetadataValues(models.Model):
generated_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)
analysis_date = models.DateField()
sample = models.ForeignKey(
'core.Sample',
on_delete=models.CASCADE,
related_name="metadata_values"
"core.Sample", on_delete=models.CASCADE, related_name="metadata_values"
)
schema_property = models.ForeignKey(
'core.SchemaProperties',
"core.SchemaProperties",
on_delete=models.CASCADE,
related_name="metadata_values"
related_name="metadata_values",
)

class Meta:
Expand Down

0 comments on commit 2324895

Please sign in to comment.