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 hmac import compare_digest
|
||||||
from operator import itemgetter
|
|
||||||
|
|
||||||
from django.contrib import messages
|
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.http import JsonResponse
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
@ -125,20 +122,25 @@ class SubmissionMixin(SingleObjectMixin):
|
||||||
def get_url_kwargs(self, **kwargs):
|
def get_url_kwargs(self, **kwargs):
|
||||||
return self.request.resolver_match.kwargs | 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
|
@context
|
||||||
@cached_property
|
@cached_property
|
||||||
def prev(self):
|
def prev(self):
|
||||||
prev = (
|
if self.index == 1:
|
||||||
self.get_queryset()
|
|
||||||
.annotate(prev=Max("created"))
|
|
||||||
.filter(created__lt=self.submission.created, created=F("prev"))
|
|
||||||
.first()
|
|
||||||
)
|
|
||||||
if prev is None:
|
|
||||||
return None
|
return None
|
||||||
return reverse(
|
return reverse(
|
||||||
self.request.resolver_match.view_name,
|
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
|
@context
|
||||||
|
@ -146,12 +148,10 @@ class SubmissionMixin(SingleObjectMixin):
|
||||||
def index(self):
|
def index(self):
|
||||||
return next(
|
return next(
|
||||||
map(
|
map(
|
||||||
itemgetter(1),
|
lambda s: s[0] + 1,
|
||||||
filter(
|
filter(
|
||||||
lambda s: s[0] == self.submission.pk,
|
lambda s: s[1].pk == self.submission.pk,
|
||||||
self.get_queryset()
|
enumerate(self.index_queryset),
|
||||||
.annotate(index=Window(expression=RowNumber(), order_by="created"))
|
|
||||||
.values_list("pk", "index"),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -159,22 +159,18 @@ class SubmissionMixin(SingleObjectMixin):
|
||||||
@context
|
@context
|
||||||
@cached_property
|
@cached_property
|
||||||
def count(self):
|
def count(self):
|
||||||
return self.get_queryset().count()
|
return self.index_queryset.count()
|
||||||
|
|
||||||
@context
|
@context
|
||||||
@cached_property
|
@cached_property
|
||||||
def next(self):
|
def next(self):
|
||||||
next = (
|
if self.index == self.count:
|
||||||
self.get_queryset()
|
|
||||||
.annotate(next=Min("created"))
|
|
||||||
.filter(created__gt=self.submission.created, created=F("next"))
|
|
||||||
.first()
|
|
||||||
)
|
|
||||||
if next is None:
|
|
||||||
return None
|
return None
|
||||||
return reverse(
|
return reverse(
|
||||||
self.request.resolver_match.view_name,
|
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