paperwork: Fix CertificationVersion is_latest/is_current when filtered
All checks were successful
Ruff / ruff (push) Successful in 24s

Unsurprisingly, filtering breaks window functions. Subqueries are less
elegant, but actually work :)
This commit is contained in:
Adam Goldsmith 2024-02-07 21:27:30 -05:00
parent c07e3ac07a
commit bbd4d8d700

View File

@ -5,8 +5,7 @@ from typing import TYPE_CHECKING, TypedDict
from django.conf import settings from django.conf import settings
from django.core.validators import RegexValidator from django.core.validators import RegexValidator
from django.db import models from django.db import models
from django.db.models import Q, Window from django.db.models import OuterRef, Q, Subquery
from django.db.models.functions import FirstValue
from django_stubs_ext import WithAnnotations from django_stubs_ext import WithAnnotations
from semver import VersionInfo from semver import VersionInfo
@ -142,23 +141,18 @@ class CertificationVersionAnnotations(TypedDict):
class CertificationVersionManager(models.Manager["CertificationVersion"]): class CertificationVersionManager(models.Manager["CertificationVersion"]):
def get_queryset(self) -> models.QuerySet["CertificationVersion"]: def get_queryset(self) -> models.QuerySet["CertificationVersion"]:
window = { qs = super().get_queryset()
"partition_by": "definition", department_versions = qs.filter(definition=OuterRef("definition")).order_by(
"order_by": [
"-major", "-major",
"-minor", "-minor",
"-patch", "-patch",
"-prerelease", "-prerelease",
],
}
return (
super()
.get_queryset()
.annotate(
is_latest=Q(pk=Window(FirstValue("pk"), **window)),
# TODO: should do a more correct comparison than just major version
is_current=Q(major=Window(FirstValue("major"), **window)),
) )
return qs.annotate(
is_latest=Q(pk=Subquery(department_versions.values("pk")[:1])),
# TODO: should do a more correct comparison than just major version
is_current=Q(major=Subquery(department_versions.values("major")[:1])),
) )