paperwork: Fix is_latest/is_current to use correct ordering

This commit is contained in:
Adam Goldsmith 2023-02-02 22:06:18 -05:00
parent d5a45a0589
commit f01e4c72ab

View File

@ -2,7 +2,7 @@ import re
from semver import VersionInfo from semver import VersionInfo
from django.db import models from django.db import models
from django.db.models import ExpressionWrapper, OuterRef, Q, Subquery from django.db.models import OuterRef, Q, Subquery
from django.core.validators import RegexValidator from django.core.validators import RegexValidator
from membershipworks.models import Member, Flag as MembershipWorksFlag from membershipworks.models import Member, Flag as MembershipWorksFlag
@ -108,17 +108,11 @@ class CertificationDefinition(models.Model):
class CertificationVersionManager(models.Manager): class CertificationVersionManager(models.Manager):
def get_queryset(self): def get_queryset(self):
qs = super().get_queryset() qs = super().get_queryset()
latest = qs.filter(definition__pk=OuterRef("definition__pk")) latest = qs.filter(definition__pk=OuterRef("definition__pk")).reverse()
return qs.annotate( return qs.annotate(
is_latest=ExpressionWrapper( is_latest=Q(pk=Subquery(latest.values("pk")[:1])),
Q(pk=Subquery(latest.values("pk")[:1])),
output_field=models.BooleanField(),
),
# TODO: should do a more correct comparison than just major version # TODO: should do a more correct comparison than just major version
is_current=ExpressionWrapper( is_current=Q(major=Subquery(latest.values("major")[:1])),
Q(major=Subquery(latest.values("major")[:1])),
output_field=models.BooleanField(),
),
) )