From 753f6798e4bf59f139c666db31c48b44e8a634aa Mon Sep 17 00:00:00 2001 From: Szymi1994 Date: Tue, 4 Jun 2019 17:22:21 +0200 Subject: [PATCH] Add validation to stop_date field in project --- managers/commons/constants.py | 3 +++ managers/models.py | 7 +++++++ managers/tests/test_unit_model.py | 15 +++++++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/managers/commons/constants.py b/managers/commons/constants.py index 4c49e8ab6..a691029fd 100644 --- a/managers/commons/constants.py +++ b/managers/commons/constants.py @@ -1,4 +1,7 @@ +from django.utils.translation import ugettext_lazy as _ + from common.constants import CORRECT_DATE_FORMAT MAX_NAME_LENGTH = 64 MESSAGE_FOR_CORRECT_DATE_FORMAT = f"Please enter date in this format: {CORRECT_DATE_FORMAT}" +STOP_DATE_VALIDATION_ERROR_MESSAGE = _("A project can not be created after expired date!") diff --git a/managers/models.py b/managers/models.py index 37ffb9a37..5bf57b39b 100644 --- a/managers/models.py +++ b/managers/models.py @@ -2,6 +2,7 @@ from typing import Any from typing import Set +from django.core.exceptions import ValidationError from django.db import models from django.db.models import Q from django.db.models.query import QuerySet @@ -11,6 +12,7 @@ from managers.commons.constants import MAX_NAME_LENGTH from managers.commons.constants import MESSAGE_FOR_CORRECT_DATE_FORMAT +from managers.commons.constants import STOP_DATE_VALIDATION_ERROR_MESSAGE from users.models import CustomUser logger = logging.getLogger(__name__) @@ -45,6 +47,11 @@ def __str__(self) -> str: def get_report_ordered(self) -> QuerySet: return self.report_set.select_related("task_activities").order_by("author__email", "-date", "-creation_date") + def clean(self) -> None: + super().clean() + if self.stop_date is not None and self.start_date > self.stop_date: + raise ValidationError(message=STOP_DATE_VALIDATION_ERROR_MESSAGE) + @receiver(m2m_changed, sender=Project.managers.through) def update_user_type(sender: Project, action: str, pk_set: Set, **kwargs: Any) -> None: diff --git a/managers/tests/test_unit_model.py b/managers/tests/test_unit_model.py index d1cb61ef1..826a45947 100644 --- a/managers/tests/test_unit_model.py +++ b/managers/tests/test_unit_model.py @@ -1,9 +1,11 @@ from datetime import datetime +from datetime import timedelta from django.core.exceptions import ValidationError from django.test import TestCase from managers.commons.constants import MAX_NAME_LENGTH +from managers.commons.constants import STOP_DATE_VALIDATION_ERROR_MESSAGE from managers.models import Project from users.common.model_helpers import create_user_using_full_clean_and_save from users.common.utils import generate_random_phone_number @@ -76,10 +78,6 @@ def test_project_model_stop_date_field_should_accept_correct_input(self): def test_project_model_stop_date_field_may_be_empty(self): self.field_should_accept_null("stop_date") - def test_project_model_stop_date_field_should_not_accept_non_date_or_datetime_value(self): - self.field_should_not_accept_input("stop_date", generate_random_phone_number(MAX_NAME_LENGTH)) - self.field_should_not_accept_input("stop_date", generate_random_string_from_letters_and_digits(MAX_NAME_LENGTH)) - def test_project_model_terminated_field_should_accept_correct_input(self): self.field_should_accept_input("terminated", True) @@ -99,3 +97,12 @@ def test_project_model_members_field_should_accept_correct_input(self): project.save() project.members.add(self.member) self.assertTrue(project.members.all().filter(email=self.member.email).exists()) + + def test_project_model_end_date_should_not_be_before_start_day(self): + project = self.default_model() + project.stop_date = project.start_date - timedelta(1) + with self.assertRaises(ValidationError) as exception: + project.full_clean() + project.save() + self.assertEqual(STOP_DATE_VALIDATION_ERROR_MESSAGE, exception.exception.messages.pop()) + self.assertFalse(Project.objects.all().exists())