Example #1
0
    def clean(self):
        """
        Custom form validation
        """
        cleaned_data = super().clean()

        # Check if the edited or created booking is not in the past (or currently effective)
        if timezone.now() > cleaned_data.get("start_date"):
            raise ValidationError(
                _("Start date cannot be in the past"), code="start_date_past"
            )

        # Check if start_date is before end_date
        if cleaned_data.get("end_date") < cleaned_data.get("start_date"):
            raise ValidationError(
                _("Start date cannot be after end date"), code="end_before_start"
            )

        # Check if the dates don't overlap an existing booking
        if Booking.check_overlap(
            cleaned_data.get("resource"),
            cleaned_data["start_date"],
            cleaned_data["end_date"],
        ):
            raise ValidationError(
                _("The resource is not available during this time range"),
                code="not_available",
            )
Example #2
0
    def test_check_overlap(self):
        result = Booking.check_overlap(
            1,
            datetime(2020, 3, 1, 14, tzinfo=timezone("UTC")),
            datetime(2020, 3, 1, 18, tzinfo=timezone("UTC")),
        )
        self.assertFalse(result)

        result = Booking.check_overlap(
            1,
            datetime(2020, 3, 1, 7, tzinfo=timezone("UTC")),
            datetime(2020, 3, 1, 11, 30, tzinfo=timezone("UTC")),
        )
        self.assertTrue(result)

        result = Booking.check_overlap(
            1,
            datetime(2020, 3, 1, 10, tzinfo=timezone("UTC")),
            datetime(2020, 3, 1, 14, tzinfo=timezone("UTC")),
        )
        self.assertTrue(result)