Skip to content

Commit

Permalink
Merge pull request #887 from amakarudze/fix-885
Browse files Browse the repository at this point in the history
Catch ValueError due to approximate date with no day
  • Loading branch information
amakarudze authored Jul 27, 2023
2 parents 97291f4 + cee216f commit a370f82
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
32 changes: 22 additions & 10 deletions organize/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
28 changes: 28 additions & 0 deletions tests/organize/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
13 changes: 12 additions & 1 deletion tests/organize/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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

0 comments on commit a370f82

Please sign in to comment.