diff --git a/helpdesk/forms.py b/helpdesk/forms.py index 87da3893..33501190 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -591,6 +591,12 @@ class TicketDependencyForm(forms.ModelForm): model = TicketDependency exclude = ('ticket',) + def __init__(self, ticket, *args, **kwargs): + super(TicketDependencyForm,self).__init__(*args, **kwargs) + + # Exclude duplicate tickets, myself, existing dependencies and parents + self.fields['depends_on'].queryset = Ticket.objects.exclude(status=Ticket.DUPLICATE_STATUS).exclude(id=ticket.id).exclude(depends_on__ticket=ticket).exclude(ticketdependency__depends_on=ticket) + class TicketResolvesForm(forms.ModelForm): ''' Adds this ticket as a dependency for a different ticket ''' @@ -599,6 +605,12 @@ class TicketResolvesForm(forms.ModelForm): #exclude = ('depends_on',) fields = ('ticket',) + def __init__(self, ticket, *args, **kwargs): + super(TicketResolvesForm,self).__init__(*args, **kwargs) + + # Exclude duplicate tickets, myself, existing dependencies and parents + self.fields['ticket'].queryset = Ticket.objects.exclude(status=Ticket.DUPLICATE_STATUS).exclude(id=ticket.id).exclude(depends_on__ticket=ticket).exclude(ticketdependency__depends_on=ticket) + class MultipleTicketSelectForm(forms.Form): tickets = forms.ModelMultipleChoiceField( diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index b95d6569..1d80864d 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -1672,7 +1672,7 @@ def ticket_dependency_add(request, ticket_id): ticket = get_object_or_404(Ticket, id=ticket_id) ticket_perm_check(request, ticket) if request.method == 'POST': - form = TicketDependencyForm(request.POST) + form = TicketDependencyForm(ticket, request.POST) if form.is_valid(): ticketdependency = form.save(commit=False) ticketdependency.ticket = ticket @@ -1680,7 +1680,7 @@ def ticket_dependency_add(request, ticket_id): ticketdependency.save() return HttpResponseRedirect(reverse('helpdesk:view', args=[ticket.id])) else: - form = TicketDependencyForm() + form = TicketDependencyForm(ticket) return render(request, 'helpdesk/ticket_dependency_add.html', { 'ticket': ticket, 'form': form, @@ -1708,7 +1708,7 @@ def ticket_resolves_add(request, ticket_id): depends_on = get_object_or_404(Ticket, id=ticket_id) ticket_perm_check(request, depends_on) if request.method == 'POST': - form = TicketResolvesForm(request.POST) + form = TicketResolvesForm(depends_on, request.POST) if form.is_valid(): ticketdependency = form.save(commit=False) ticketdependency.depends_on = depends_on @@ -1716,7 +1716,7 @@ def ticket_resolves_add(request, ticket_id): ticketdependency.save() return HttpResponseRedirect(reverse('helpdesk:view', args=[depends_on.id])) else: - form = TicketResolvesForm() + form = TicketResolvesForm(depends_on) return render(request, 'helpdesk/ticket_resolves_add.html', { 'depends_on': depends_on, 'form': form,