ticket.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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)
  9. if not user.is_superuser:
  10. group_ids = group_ids.filter(user=user)
  11. ticket_lists = TicketList.objects.select_related("group").filter(group__in=group_ids)
  12. self.fields["assigned_to"].empty_label = "Anyone"
  13. self.fields["ticket_list"].empty_label = None
  14. self.fields['assigned_to'].queryset = get_user_model().objects.all().order_by('username')
  15. self.fields["ticket_list"].queryset = ticket_lists
  16. self.fields["ticket_list"].label_from_instance = lambda obj: f"{obj.group} > {obj}"
  17. if self.instance.pk:
  18. self.initial["ticket_type"] = self.instance.ticket_type
  19. self.fields["ticket_type"].disabled = True
  20. class Meta:
  21. model = Ticket
  22. exclude = ["status", "json"]
  23. widgets = {
  24. 'title': forms.TextInput(attrs={
  25. 'class': 'form-control',
  26. 'autofocus': True,
  27. }),
  28. 'ticket_list': forms.Select(attrs={
  29. 'class': 'form-control'
  30. }),
  31. 'ticket_type': forms.Select(attrs={
  32. 'class': 'form-control'
  33. }),
  34. 'due_date': forms.DateInput(attrs={
  35. 'class': 'form-control',
  36. 'type': 'date'
  37. }),
  38. 'assigned_to': forms.Select(attrs={
  39. 'class': 'form-control'
  40. }),
  41. 'note': forms.Textarea(attrs={
  42. 'class': 'form-control',
  43. 'rows': 4
  44. }),
  45. 'priority': forms.NumberInput(attrs={
  46. 'class': 'form-control',
  47. }),
  48. }