1
0

ticket.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from django import forms
  2. from django.contrib.auth.models import Group
  3. from django.contrib.auth import get_user_model
  4. from tickets.models import Ticket, TicketList
  5. class TicketForm(forms.ModelForm):
  6. def __init__(self, user, *args, **kwargs):
  7. super().__init__(*args, **kwargs)
  8. group_ids = Group.objects.values_list("pk", flat=True).filter(user=user)
  9. ticket_lists = TicketList.objects.select_related("group").filter(group__in=group_ids)
  10. self.fields["assigned_to"].empty_label = "Anyone"
  11. self.fields["ticket_list"].empty_label = None
  12. self.fields['assigned_to'].queryset = get_user_model().objects.all().order_by('username')
  13. self.fields["ticket_list"].queryset = ticket_lists
  14. self.fields["ticket_list"].label_from_instance = lambda obj: f"{obj.group} > {obj}"
  15. if self.instance.pk:
  16. self.initial["ticket_type"] = self.instance.ticket_type
  17. self.fields["ticket_type"].disabled = True
  18. class Meta:
  19. model = Ticket
  20. exclude = ["status", "json"]
  21. widgets = {
  22. 'title': forms.TextInput(attrs={
  23. 'class': 'form-control',
  24. 'autofocus': True,
  25. }),
  26. 'ticket_list': forms.Select(attrs={
  27. 'class': 'form-control'
  28. }),
  29. 'ticket_type': forms.Select(attrs={
  30. 'class': 'form-control'
  31. }),
  32. 'due_date': forms.DateInput(attrs={
  33. 'class': 'form-control',
  34. 'type': 'date'
  35. }),
  36. 'assigned_to': forms.Select(attrs={
  37. 'class': 'form-control'
  38. }),
  39. 'note': forms.Textarea(attrs={
  40. 'class': 'form-control',
  41. 'rows': 4
  42. }),
  43. 'priority': forms.NumberInput(attrs={
  44. 'class': 'form-control',
  45. }),
  46. }