From f25d07ccf7c539dea9fae0175dfb5bc39b6c72d9 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Fri, 31 Mar 2017 00:50:00 +0200 Subject: [PATCH] Order mnt_by without duplicates --- whoisdb/forms.py | 3 ++- whoisdb/helpers.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/whoisdb/forms.py b/whoisdb/forms.py index 8339587..9dc7c8d 100644 --- a/whoisdb/forms.py +++ b/whoisdb/forms.py @@ -1,9 +1,10 @@ from django import forms -from django.db.models import Case, When, IntegerField +from django.db.models import Q from .models import Maintainer, Contact, InetNum, ASBlock, ASNumber from .validators import HandleValidatorWithSuffix, IP46CIDRValidator from .formfields import MultiTextInput +from .helpers import orderQueryset import ipaddress diff --git a/whoisdb/helpers.py b/whoisdb/helpers.py index a2ed3a0..ef5f9bd 100644 --- a/whoisdb/helpers.py +++ b/whoisdb/helpers.py @@ -1,7 +1,7 @@ import whoisdb.models import domains.models -from django.db.models import F +from django.db.models import F, When, Max, Case, IntegerField import ipaddress import re @@ -150,3 +150,16 @@ def findHandleFromStr(rawValue): pass return None + + +def orderQueryset(qs, userOwned, objOwned): + # when for + whens = [When(userOwned, then=2)] + if objOwned: + # add existing + whens.append(When(objOwned, then=1)) + + qs = qs.annotate(card=Max(Case(*whens, default=0, output_field=IntegerField()))).order_by("-card") + + return qs +