Skip to content

Commit

Permalink
Improved date parsing of batch query files
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Sep 29, 2023
1 parent a35f935 commit 4cf21d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
10 changes: 0 additions & 10 deletions adit/batch_query/parsers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import re

from adit.core.parsers import BatchFileParser
from adit.core.utils.dicom_utils import person_name_to_dicom

Expand All @@ -20,8 +18,6 @@
"pseudonym": "Pseudonym",
}

dt_regex = re.compile(r"^(\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2}$")


class BatchQueryFileParser(BatchFileParser[BatchQueryTask]):
serializer_class = BatchQueryTaskSerializer
Expand All @@ -34,12 +30,6 @@ def transform_value(self, field: str, value: str) -> str | list[str] | None:
if not value:
return None

m = dt_regex.match(value)
if m:
# Only extract the date as the DateField of the date field
# will only parse a valid date without time.
return m.group(1)

if field in ["modalities", "series_numbers"]:
return ", ".join(filter(len, map(str.strip, value.split(","))))

Expand Down
11 changes: 11 additions & 0 deletions adit/core/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Type

from django.utils import formats
from rest_framework import serializers

from adit.core.models import DicomTask
Expand Down Expand Up @@ -29,6 +30,16 @@ def many_init(cls, *args, **kwargs):

def adapt_date_field(self, field_name):
field = self.fields[field_name]

# When loading the data from the Excel sheet the fields with date content may return
# a date/time string (when date type was explicitly set in the Excel column)
# or the string that was directly provided by the user (text type column).
# We make sure that all different formats could be parsed.
date_input_formats = formats.get_format("DATE_INPUT_FORMATS")
datetime_input_formats = formats.get_format("DATETIME_INPUT_FORMATS")
input_formats = date_input_formats + datetime_input_formats
field.input_formats = input_formats

field.error_messages["invalid"] = "Date has wrong format."

def validate(self, attrs):
Expand Down

0 comments on commit 4cf21d0

Please sign in to comment.