improve date validation

This commit is contained in:
Sina Atalay 2023-09-18 19:00:53 +02:00
parent c9cefcf748
commit 792cfb1f05
1 changed files with 38 additions and 11 deletions

View File

@ -452,25 +452,52 @@ class Event(BaseModel):
"""Make sure that either `#!python start_date` and `#!python end_date` or only
`#!python date`is provided.
"""
if (
model.start_date is not None
and model.end_date is not None
and model.date is not None
):
dateIsProvided = False
startDateIsProvided = False
endDateIsProvided = False
if model.date is not None:
dateIsProvided = True
if model.start_date is not None:
startDateIsProvided = True
if model.end_date is not None:
endDateIsProvided = True
if dateIsProvided and startDateIsProvided and endDateIsProvided:
logging.warning(
"start_date, end_date and date are all provided. Therefore, date will"
" be ignored."
'"start_date", "end_date" and "date" are all provided in of the'
" entries. Therefore, date will be ignored."
)
model.date = None
elif model.date is not None and (
model.start_date is not None or model.end_date is not None
):
elif dateIsProvided and startDateIsProvided and not endDateIsProvided:
logging.warning(
"date is provided. Therefore, start_date and end_date will be ignored."
'Both "date" and "start_date" is provided in of the entries.'
' "start_date" will be ignored.'
)
model.start_date = None
model.end_date = None
elif dateIsProvided and endDateIsProvided and not startDateIsProvided:
logging.warning(
'Both "date" and "end_date" is provided in of the entries. "end_date"'
" will be ignored."
)
model.start_date = None
model.end_date = None
elif startDateIsProvided and not endDateIsProvided:
logging.warning(
'"start_date" is provided in of the entries, but "end_date" is not.'
' "end_date" will be set to "present".'
)
model.end_date = "present"
elif not startDateIsProvided and not dateIsProvided:
raise ValueError(
'Either "date" or "start_date" and "end_date" should be provided in'
" each entry."
)
return model
@computed_field