Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions managers/commons/constants.py
Original file line number Diff line number Diff line change
@@ -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!")
7 changes: 7 additions & 0 deletions managers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__)
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 11 additions & 4 deletions managers/tests/test_unit_model.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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())