Skip to content

Commit

Permalink
use ScheduledVisitWindowError exception message in form validation error
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Feb 21, 2024
1 parent 6ae8c0d commit 24b66fd
Showing 1 changed file with 5 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
from typing import TYPE_CHECKING

from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.utils.translation import gettext as _
from edc_utils import convert_php_dateformat, formatted_date, to_utc
from edc_utils.date import ceil_datetime, floor_secs, to_local
from edc_utils import formatted_date, to_utc
from edc_utils.date import floor_secs
from edc_visit_schedule.exceptions import (
ScheduledVisitWindowError,
UnScheduledVisitWindowError,
)
from edc_visit_schedule.utils import get_lower_datetime, get_upper_datetime
from edc_visit_schedule.utils import get_lower_datetime

from ..constants import COMPLETE_APPT, INCOMPLETE_APPT

Expand Down Expand Up @@ -50,7 +49,6 @@ def datetime_in_window_or_raise(
form_field: str,
):
if proposed_appt_datetime:
datetimestring = convert_php_dateformat(settings.SHORT_DATETIME_FORMAT)
try:
appointment.schedule.datetime_in_window(
timepoint_datetime=appointment.timepoint_datetime,
Expand Down Expand Up @@ -88,26 +86,8 @@ def datetime_in_window_or_raise(
},
UNSCHEDULED_WINDOW_ERROR,
)
except ScheduledVisitWindowError:
lower = floor_secs(to_local(get_lower_datetime(appointment))).strftime(
datetimestring
)
upper = floor_secs(
to_local(ceil_datetime(get_upper_datetime(appointment)))
).strftime(datetimestring)
proposed = floor_secs(to_local(proposed_appt_datetime)).strftime(
datetimestring
)
# TODO: check this is correctly limiting (e.g. requistions)
self.raise_validation_error(
{
form_field: (
f"Invalid. Expected a date/time between {lower} and {upper} (S). "
f"Got {proposed}."
)
},
SCHEDULED_WINDOW_ERROR,
)
except ScheduledVisitWindowError as e:
self.raise_validation_error({form_field: (str(e))}, SCHEDULED_WINDOW_ERROR)

@staticmethod
def baseline_timepoint_datetime(appointment: Appointment) -> datetime:
Expand Down

0 comments on commit 24b66fd

Please sign in to comment.