Skip to content

Commit

Permalink
chore(utils): drop dateparser methods that are not used anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Dec 17, 2024
1 parent 516b07c commit f01e7be
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 88 deletions.
76 changes: 0 additions & 76 deletions apis_core/utils/DateParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,79 +273,3 @@ def parse_iso_date(date_string):
print("Could not parse date: '", date_string, "' due to error: ", e)

return date_single, date_ab, date_bis


def get_date_help_text_from_dates(
single_date, single_start_date, single_end_date, single_date_written
):
"""
function for creating string help text from parsed dates, to provide feedback to the user
about the parsing status of a given date field.
:param single_date: datetime :
the individual date point
:param single_start_date: datetime :
the start range of a date
:param single_end_date: datetime :
the endrange of a date
:param single_date_written: str :
the textual user entry of a date field (needed to check if empty or not)
:return help_text: str :
The text to be displayed underneath a date field, informing the user about the parsing result
"""

# check which of the dates could be parsed to construct the relevant feedback text
help_text = ""
if single_date:
# single date could be parsed
help_text = "Date interpreted as "
if single_start_date or single_end_date:
# date has also start or end ranges, then ignore single date
if single_start_date:
# date has start range
help_text += (
str(single_start_date.year)
+ "-"
+ str(single_start_date.month)
+ "-"
+ str(single_start_date.day)
+ " until "
)
else:
# date has no start range, then write "undefined"
help_text += "undefined start until "
if single_end_date:
# date has end range
help_text += (
str(single_end_date.year)
+ "-"
+ str(single_end_date.month)
+ "-"
+ str(single_end_date.day)
)
else:
# date has no start range, then write "undefined"
help_text += "undefined end"
else:
# date has no start nor end range. Use single date then.
help_text += (
str(single_date.year)
+ "-"
+ str(single_date.month)
+ "-"
+ str(single_date.day)
)
elif single_date_written is not None:
# date input field is not empty but it could not be parsed either. Show parsing info and help text
help_text = (
"<b>Date could not be interpreted</b><br>" + get_date_help_text_default()
)
else:
# date field is completely empty. Show help text only
help_text = get_date_help_text_default()

return help_text


def get_date_help_text_default():
return "Dates are interpreted by defined rules. If this fails, an iso-date can be explicitly set with '&lt;YYYY-MM-DD&gt;'."
16 changes: 4 additions & 12 deletions apis_core/utils/test_DateParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from django.test import TestCase

from .DateParser import get_date_help_text_from_dates, parse_date
from .DateParser import parse_date

help_text_default = "Dates are interpreted by defined rules. If this fails, an iso-date can be explicitly set with '&lt;YYYY-MM-DD&gt;'."

Expand All @@ -16,39 +16,31 @@ def fi(datestring):


# a dict of dates with the written date as key
# followed by a tuple of (single, start, end, help_text)
# followed by a tuple of (single, start, end)
dates = {
"vor dem 23.10.1449 <1449-10-23>": (
fi("1449-10-23"),
None,
None,
"Date interpreted as 1449-10-23",
),
"1459-12": (
fi("1459-12-16"),
fi("1459-12-01"),
fi("1459-12-31"),
"Date interpreted as 1459-12-1 until 1459-12-31",
),
"1460-03-30": (fi("1460-03-30"), None, None, "Date interpreted as 1460-3-30"),
"1460-03-30": (fi("1460-03-30"), None, None),
"": (
None,
None,
None,
"<b>Date could not be interpreted</b><br>" + help_text_default,
),
}


class DateParserTest(TestCase):
def test_dates(self):
for datestring, (expsingle, expstart, expend, help_text) in dates.items():
for datestring, (expsingle, expstart, expend) in dates.items():
single, start, end = parse_date(datestring)
self.assertEqual(expsingle, single)
self.assertEqual(expstart, start)
self.assertEqual(expend, end)

def test_help_text(self):
for datestring, (single, start, end, exp_help_text) in dates.items():
help_text = get_date_help_text_from_dates(single, start, end, datestring)
self.assertEqual(exp_help_text, help_text)

0 comments on commit f01e7be

Please sign in to comment.