From ff77aa0fe3d5e1d601b3383a09de7932edddc048 Mon Sep 17 00:00:00 2001 From: bbe Date: Fri, 12 Jun 2020 15:34:39 +0200 Subject: [PATCH] A ticket cannot depends on itself or on a ticket already depending on it --- helpdesk/views/staff.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index f0789044..ba7ed16a 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -1475,16 +1475,17 @@ def ticket_dependency_add(request, ticket_id): raise PermissionDenied() if not _is_my_ticket(request.user, ticket): raise PermissionDenied() - if request.method == 'POST': - form = TicketDependencyForm(request.POST) - if form.is_valid(): - ticketdependency = form.save(commit=False) - ticketdependency.ticket = ticket - if ticketdependency.ticket != ticketdependency.depends_on: - ticketdependency.save() - return HttpResponseRedirect(reverse('helpdesk:view', args=[ticket.id])) - else: - form = TicketDependencyForm() + + form = TicketDependencyForm(request.POST or None) + # A ticket cannot depends on itself or on a ticket already depending on it + form.fields['depends_on'].queryset = Ticket.objects.exclude( + Q(id=ticket.id) | Q(ticketdependency__depends_on=ticket) + ) + if form.is_valid(): + ticketdependency = form.save(commit=False) + ticketdependency.ticket = ticket + ticketdependency.save() + return HttpResponseRedirect(reverse('helpdesk:view', args=[ticket.id])) return render(request, 'helpdesk/ticket_dependency_add.html', { 'ticket': ticket, 'form': form,