Verify necessary fields before running model validation tests.

This commit is contained in:
Christopher Charbonneau Wells 2017-11-07 07:07:51 -05:00
parent 490e07c381
commit 1c5278cebf
2 changed files with 7 additions and 6 deletions

View File

@ -36,10 +36,11 @@ def validate_unique_period(queryset, model):
""" """
if model.id: if model.id:
queryset = queryset.exclude(id=model.id) queryset = queryset.exclude(id=model.id)
if queryset.filter(start__lte=model.end, end__gte=model.start): if model.start and model.end:
raise ValidationError( if queryset.filter(start__lte=model.end, end__gte=model.start):
'Another entry already exists within the specified time period.', raise ValidationError(
code='period_intersection') 'Another entry intersects the specified time period.',
code='period_intersection')
def validate_time(time, field_name): 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. :param field_name: the name of the field being checked.
:return: :return:
""" """
if time > timezone.localtime(): if time and time > timezone.localtime():
raise ValidationError( raise ValidationError(
{field_name: 'Date/time can not be in the future.'}, {field_name: 'Date/time can not be in the future.'},
code='time_invalid') code='time_invalid')

View File

@ -238,4 +238,4 @@ class FormsTestCase(TestCase):
page, page,
'form', 'form',
None, None,
'Another entry already exists within the specified time period.') 'Another entry intersects the specified time period.')