refactor(SubmissionMixin): compute indices in Python
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
f04e42cfc1
commit
1c5f068ea3
|
@ -1,9 +1,6 @@
|
|||
from hmac import compare_digest
|
||||
from operator import itemgetter
|
||||
|
||||
from django.contrib import messages
|
||||
from django.db.models import F, Max, Min, Window
|
||||
from django.db.models.functions import RowNumber
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import redirect
|
||||
from django.urls import reverse
|
||||
|
@ -125,20 +122,25 @@ class SubmissionMixin(SingleObjectMixin):
|
|||
def get_url_kwargs(self, **kwargs):
|
||||
return self.request.resolver_match.kwargs | kwargs
|
||||
|
||||
@cached_property
|
||||
def index_queryset(self):
|
||||
return (
|
||||
self.get_queryset()
|
||||
.prefetch_related(None)
|
||||
.only("pk", "code")
|
||||
.order_by("created")
|
||||
)
|
||||
|
||||
@context
|
||||
@cached_property
|
||||
def prev(self):
|
||||
prev = (
|
||||
self.get_queryset()
|
||||
.annotate(prev=Max("created"))
|
||||
.filter(created__lt=self.submission.created, created=F("prev"))
|
||||
.first()
|
||||
)
|
||||
if prev is None:
|
||||
if self.index == 1:
|
||||
return None
|
||||
return reverse(
|
||||
self.request.resolver_match.view_name,
|
||||
kwargs=self.get_url_kwargs(code=prev.code),
|
||||
kwargs=self.get_url_kwargs(
|
||||
code=self.index_queryset[self.index - 1 - 1].code
|
||||
),
|
||||
)
|
||||
|
||||
@context
|
||||
|
@ -146,12 +148,10 @@ class SubmissionMixin(SingleObjectMixin):
|
|||
def index(self):
|
||||
return next(
|
||||
map(
|
||||
itemgetter(1),
|
||||
lambda s: s[0] + 1,
|
||||
filter(
|
||||
lambda s: s[0] == self.submission.pk,
|
||||
self.get_queryset()
|
||||
.annotate(index=Window(expression=RowNumber(), order_by="created"))
|
||||
.values_list("pk", "index"),
|
||||
lambda s: s[1].pk == self.submission.pk,
|
||||
enumerate(self.index_queryset),
|
||||
),
|
||||
)
|
||||
)
|
||||
|
@ -159,22 +159,18 @@ class SubmissionMixin(SingleObjectMixin):
|
|||
@context
|
||||
@cached_property
|
||||
def count(self):
|
||||
return self.get_queryset().count()
|
||||
return self.index_queryset.count()
|
||||
|
||||
@context
|
||||
@cached_property
|
||||
def next(self):
|
||||
next = (
|
||||
self.get_queryset()
|
||||
.annotate(next=Min("created"))
|
||||
.filter(created__gt=self.submission.created, created=F("next"))
|
||||
.first()
|
||||
)
|
||||
if next is None:
|
||||
if self.index == self.count:
|
||||
return None
|
||||
return reverse(
|
||||
self.request.resolver_match.view_name,
|
||||
kwargs=self.get_url_kwargs(code=next.code),
|
||||
kwargs=self.get_url_kwargs(
|
||||
code=self.index_queryset[self.index - 1 + 1].code
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue