paperwork: Use window functions for CertificationVersion is_current/is_latest
This should be a lot more efficient, and is much cleaner code too
This commit is contained in:
parent
caf8c2cf45
commit
a50112c534
@ -4,7 +4,8 @@ from typing import TypedDict, TYPE_CHECKING, Optional
|
|||||||
|
|
||||||
from semver import VersionInfo
|
from semver import VersionInfo
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import OuterRef, Q, ExpressionWrapper, Subquery
|
from django.db.models import Q, Window
|
||||||
|
from django.db.models.functions import FirstValue
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.validators import RegexValidator
|
from django.core.validators import RegexValidator
|
||||||
from django_stubs_ext import WithAnnotations
|
from django_stubs_ext import WithAnnotations
|
||||||
@ -139,18 +140,23 @@ 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"]:
|
||||||
qs = super().get_queryset()
|
window = {
|
||||||
latest = qs.filter(definition__pk=OuterRef("definition__pk")).reverse()
|
"partition_by": "definition",
|
||||||
return qs.annotate(
|
"order_by": [
|
||||||
is_latest=ExpressionWrapper(
|
"-major",
|
||||||
Q(pk=Subquery(latest.values("pk")[:1])),
|
"-minor",
|
||||||
output_field=models.BooleanField(),
|
"-patch",
|
||||||
),
|
"-prerelease",
|
||||||
# TODO: should do a more correct comparison than just major version
|
],
|
||||||
is_current=ExpressionWrapper(
|
}
|
||||||
Q(major=Subquery(latest.values("major")[:1])),
|
return (
|
||||||
output_field=models.BooleanField(),
|
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)),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user