diff --git a/tests/tests.py b/tests/tests.py index d4d4044..f4cebae 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -408,3 +408,8 @@ def test_subtask_list(self): }], 'title': 'First Task'} ]) + + + +class AdminFormTestCase(APITestCase): + pass \ No newline at end of file diff --git a/umsebenzi/forms.py b/umsebenzi/forms.py index ea75344..93917c3 100644 --- a/umsebenzi/forms.py +++ b/umsebenzi/forms.py @@ -1,6 +1,7 @@ from django import forms -from umsebenzi.models import Task +from umsebenzi.models import Task, Project from umsebenzi.latest import get_task_code +from umsebenzi.enums import Issue class TaskForm(forms.ModelForm): @@ -8,6 +9,30 @@ class Meta: model = Task exclude = ('code',) + def clean(self): + """ + Rules + 1) If Epic cannot have parent + 2) Parent task must belong to same project + 3) If task is subtask parent cannot be subtask + """ + data = super().clean() + issue = data['issue'] + epic: Task = data['parent'] + + if issue is Issue.EPIC and epic: + raise forms.ValidationError("Epic task cannot have parent") + + if issue is Issue.SUBTASK and epic and epic.issue is Issue.SUBTASK: + raise forms.ValidationError("Subtask parent cannot be subtask") + + project: Project = data['project'] + if epic and project: + if epic.project != project: + raise forms.ValidationError("Subtask parent(epic) must belong to same project") + + return data + def save(self, commit=True): task = super().save(commit=False) if not self.instance.id: