Skip to content

Commit

Permalink
begin adding tests for admin form
Browse files Browse the repository at this point in the history
  • Loading branch information
knightebsuku committed Jan 31, 2025
1 parent 9967590 commit 7593931
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,8 @@ def test_subtask_list(self):
}],
'title': 'First Task'}
])



class AdminFormTestCase(APITestCase):
pass
27 changes: 26 additions & 1 deletion umsebenzi/forms.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
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):
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:
Expand Down

0 comments on commit 7593931

Please sign in to comment.