diff --git a/helpdesk/apps.py b/helpdesk/apps.py index d316d314..de9d8537 100644 --- a/helpdesk/apps.py +++ b/helpdesk/apps.py @@ -11,3 +11,7 @@ class HelpdeskConfig(AppConfig): def ready(self): from . import webhooks # noqa: F401 + + from django.db.models.fields import Field + from helpdesk.orm_operators import NotIn + Field.register_lookup(NotIn) diff --git a/helpdesk/orm_operators.py b/helpdesk/orm_operators.py new file mode 100644 index 00000000..46c26ff3 --- /dev/null +++ b/helpdesk/orm_operators.py @@ -0,0 +1,7 @@ +from django.db.models.lookups import In + +class NotIn(In): + lookup_name = "not_in" + + def get_rhs_op(self, connection, rhs): + return "NOT IN %s" % rhs diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 0f230583..b95d6569 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -424,11 +424,9 @@ def view_ticket(request, ticket_id): return redirect('helpdesk:edit_ticket_checklist', ticket.id, checklist.id) - open_dependencies = ticket.ticketdependency.filter(depends_on__status__in=Ticket.OPEN_STATUSES) - dependencies = [d for d in itertools.chain( - open_dependencies, - ticket.ticketdependency.all().difference(open_dependencies) - )] + # Open tickets on top + dependencies = list(ticket.ticketdependency.filter(depends_on__status__in=Ticket.OPEN_STATUSES)) \ + + list(ticket.ticketdependency.filter(depends_on__status__not_in=Ticket.OPEN_STATUSES)) return render(request, 'helpdesk/ticket.html', { 'ticket': ticket,