diff --git a/organize/models.py b/organize/models.py index 0c2fb4913..60cb6fe68 100644 --- a/organize/models.py +++ b/organize/models.py @@ -50,16 +50,28 @@ def create(self, **data_dict): if previous_event: event_date = data_dict["date"] - if date(event_date.year, event_date.month, event_date.day) - date( - previous_event.date.year, previous_event.date.month, previous_event.date.day - ) < timedelta(days=180): - raise ValidationError( - { - "date": _( - "Your workshops should be at least 6 months apart. " "Please read our Organizer Manual." - ) - } - ) + try: + if date(event_date.year, event_date.month, event_date.day) - date( + previous_event.date.year, previous_event.date.month, previous_event.date.day + ) < timedelta(days=180): + raise ValidationError( + { + "date": _( + "Your workshops should be at least 6 months apart. " "Please read our Organizer Manual." + ) + } + ) + except ValueError: + if date(event_date.year, event_date.month, event_date.day) - date( + previous_event.date.year, previous_event.date.month, 1 + ) < timedelta(days=180): + raise ValidationError( + { + "date": _( + "Your workshops should be at least 6 months apart. " "Please read our Organizer Manual." + ) + } + ) return super().create(**data_dict) diff --git a/tests/organize/conftest.py b/tests/organize/conftest.py index 1cd7844da..316e1d767 100644 --- a/tests/organize/conftest.py +++ b/tests/organize/conftest.py @@ -454,3 +454,31 @@ def workshop_form_invalid_no_link(): "confirm_covid_19_protocols": True, } return data + + +@pytest.fixture +def previous_deployed_event(): + return EventApplication.objects.create( + city="Zanzibar", + country="Tanzania", + date="2070-10-10", + created_at=datetime.now() - timedelta(days=181), + main_organizer_email="test@example.com", + main_organizer_first_name="Anna", + main_organizer_last_name="Smith", + status="deployed", + ) + + +@pytest.fixture +def previous_application_approximate_date(): + return EventApplication.objects.create( + city="Zanzibar", + country="Tanzania", + date="2070-10-0", + created_at=datetime.now() - timedelta(days=181), + main_organizer_email="test@example.com", + main_organizer_first_name="Anna", + main_organizer_last_name="Smith", + status="deployed", + ) diff --git a/tests/organize/test_models.py b/tests/organize/test_models.py index f51227943..d3c17e9fa 100644 --- a/tests/organize/test_models.py +++ b/tests/organize/test_models.py @@ -43,7 +43,6 @@ def test_deploy_event_from_previous_event(get_or_create_gmail, base_application, @mock.patch("organize.models.gmail_accounts.get_or_create_gmail") def test_send_deployed_email(get_or_create_gmail, base_application, mailoutbox, stock_pictures): - get_or_create_gmail.return_value = (f"{base_application.city}@djangogirls.org", "asd123ASD") base_application.create_event() @@ -108,3 +107,15 @@ def test_application_fails_if_event_dates_not_six_months_apart(data_dict, previo def test_event_application_manager_create_method(data_dict): event = EventApplication.object.create(**data_dict) assert event.city == "Harare" + + +def test_previous_deployed_event_years_apart_is_successful(data_dict, previous_deployed_event): + assert EventApplication.object.count() == 1 + EventApplication.object.create(**data_dict) + assert EventApplication.object.count() == 2 + + +def test_previous_application_with_approximate_date(data_dict, previous_application_approximate_date): + assert EventApplication.object.count() == 1 + EventApplication.object.create(**data_dict) + assert EventApplication.object.count() == 2