Rewrite dependency tickets ordering, using Case annotation

This commit is contained in:
Georg Lehner 2024-06-10 10:08:07 +02:00
parent ec603347c7
commit 54864b2c1e
3 changed files with 7 additions and 15 deletions

View File

@ -11,7 +11,3 @@ 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)

View File

@ -1,7 +0,0 @@
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

View File

@ -20,7 +20,7 @@ from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied
from django.core.handlers.wsgi import WSGIRequest
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.db.models import Q
from django.db.models import Q, Case, Value, When
from django.forms import HiddenInput, inlineformset_factory, TextInput
from django.http import Http404, HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
@ -423,9 +423,12 @@ def view_ticket(request, ticket_id):
return redirect('helpdesk:edit_ticket_checklist', ticket.id, checklist.id)
# 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))
# List open tickets on top
dependencies = ticket.ticketdependency.annotate(
rank=Case(
When(depends_on__status__in=Ticket.OPEN_STATUSES, then=Value('1')),
default=Value('2')
)).order_by('rank')
return render(request, 'helpdesk/ticket.html', {
'ticket': ticket,