From 1c5278cebfc0e870e263a4f2ee42a7bd548524ce Mon Sep 17 00:00:00 2001 From: Christopher Charbonneau Wells Date: Tue, 7 Nov 2017 07:07:51 -0500 Subject: [PATCH] Verify necessary fields before running model validation tests. --- core/models.py | 11 ++++++----- core/tests/tests_forms.py | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/core/models.py b/core/models.py index 367045bd..2bfae0b0 100644 --- a/core/models.py +++ b/core/models.py @@ -36,10 +36,11 @@ def validate_unique_period(queryset, model): """ if model.id: queryset = queryset.exclude(id=model.id) - if queryset.filter(start__lte=model.end, end__gte=model.start): - raise ValidationError( - 'Another entry already exists within the specified time period.', - code='period_intersection') + if model.start and model.end: + if queryset.filter(start__lte=model.end, end__gte=model.start): + raise ValidationError( + 'Another entry intersects the specified time period.', + code='period_intersection') def validate_time(time, field_name): @@ -49,7 +50,7 @@ def validate_time(time, field_name): :param field_name: the name of the field being checked. :return: """ - if time > timezone.localtime(): + if time and time > timezone.localtime(): raise ValidationError( {field_name: 'Date/time can not be in the future.'}, code='time_invalid') diff --git a/core/tests/tests_forms.py b/core/tests/tests_forms.py index f60486f8..06341d8b 100644 --- a/core/tests/tests_forms.py +++ b/core/tests/tests_forms.py @@ -238,4 +238,4 @@ class FormsTestCase(TestCase): page, 'form', None, - 'Another entry already exists within the specified time period.') + 'Another entry intersects the specified time period.')