Order mnt_by without duplicates

This commit is contained in:
Sebastian Lohff 2017-03-31 00:50:00 +02:00
parent 1c95b05e8b
commit f25d07ccf7
2 changed files with 16 additions and 2 deletions

View File

@ -1,9 +1,10 @@
from django import forms 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 .models import Maintainer, Contact, InetNum, ASBlock, ASNumber
from .validators import HandleValidatorWithSuffix, IP46CIDRValidator from .validators import HandleValidatorWithSuffix, IP46CIDRValidator
from .formfields import MultiTextInput from .formfields import MultiTextInput
from .helpers import orderQueryset
import ipaddress import ipaddress

View File

@ -1,7 +1,7 @@
import whoisdb.models import whoisdb.models
import domains.models import domains.models
from django.db.models import F from django.db.models import F, When, Max, Case, IntegerField
import ipaddress import ipaddress
import re import re
@ -150,3 +150,16 @@ def findHandleFromStr(rawValue):
pass pass
return None 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