1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- from django import forms
- from django.contrib.auth.models import Group
- from django.contrib.auth import get_user_model
- from tickets.models import Ticket, TicketList
- class TicketForm(forms.ModelForm):
- def __init__(self, user, *args, **kwargs):
- super().__init__(*args, **kwargs)
- group_ids = Group.objects.values_list("pk", flat=True)
- if not user.is_superuser:
- group_ids = group_ids.filter(user=user)
-
- ticket_lists = TicketList.objects.select_related("group").filter(group__in=group_ids)
- self.fields["assigned_to"].empty_label = "Anyone"
- self.fields["ticket_list"].empty_label = None
- self.fields['assigned_to'].queryset = get_user_model().objects.all().order_by('username')
- self.fields["ticket_list"].queryset = ticket_lists
-
- self.fields["ticket_list"].label_from_instance = lambda obj: f"{obj.group} > {obj}"
- if self.instance.pk:
- self.initial["ticket_type"] = self.instance.ticket_type
- self.fields["ticket_type"].disabled = True
- class Meta:
- model = Ticket
- exclude = ["status", "json"]
- widgets = {
- 'title': forms.TextInput(attrs={
- 'class': 'form-control',
- 'autofocus': True,
- }),
- 'ticket_list': forms.Select(attrs={
- 'class': 'form-control'
- }),
- 'ticket_type': forms.Select(attrs={
- 'class': 'form-control'
- }),
- 'due_date': forms.DateInput(attrs={
- 'class': 'form-control',
- 'type': 'date'
- }),
- 'assigned_to': forms.Select(attrs={
- 'class': 'form-control'
- }),
- 'note': forms.Textarea(attrs={
- 'class': 'form-control',
- 'rows': 4
- }),
- 'priority': forms.NumberInput(attrs={
- 'class': 'form-control',
- }),
- }
|