forms.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. from django import forms
  2. from adhesions.models import User
  3. from .models import Task, TaskComment
  4. class TaskForm(forms.ModelForm):
  5. def __init__(self, *args, **kwargs):
  6. tasklist = kwargs.pop('tasklist')
  7. super().__init__(*args, **kwargs)
  8. if tasklist.groups.exists():
  9. members = User.objects.filter(groups__in=tasklist.groups.all())
  10. else:
  11. members = User.objects.all()
  12. members = members.order_by('adhesion__id')
  13. members = members.select_related('adhesion')
  14. self.fields['assigned_to'].queryset = members
  15. self.fields["assigned_to"].label_from_instance = lambda obj: "ADT%d %s" % (
  16. obj.adhesion.id,
  17. str(obj.profile),
  18. )
  19. class Meta:
  20. model = Task
  21. fields = ('title', 'note', 'due_date', 'assigned_to',)
  22. widgets = {
  23. 'due_date': forms.DateInput(attrs={"type": "date"}),
  24. }
  25. class CommentForm(forms.ModelForm):
  26. class Meta:
  27. model = TaskComment
  28. fields = ('body',)