Skip to content

Commit

Permalink
Fake paginator for Ark admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
avdempsey committed Feb 3, 2022
1 parent 18e2a0c commit 102c2ca
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ark/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
from django.contrib import admin
from django.core.paginator import Paginator
from django.db import OperationalError, connection, transaction
from django.utils.functional import cached_property

from ark.models import User, Naan, Shoulder, Ark, Key


class TimeLimitedPaginator(Paginator):
"""
Paginator that enforces a timeout on the count operation.
If the operations times out, a fake bogus value is
returned instead.
Lifted from: https://web.archive.org/web/20210422225156/https://hakibenita.com/optimizing-the-django-admin-paginator
"""

@cached_property
def count(self):
# We set the timeout in a db transaction to prevent it from
# affecting other transactions.
with transaction.atomic(), connection.cursor() as cursor:
cursor.execute("SET LOCAL statement_timeout TO 1000;")
try:
return super().count
except OperationalError:
return 9999999999


@admin.register(User)
class UserAdmin(admin.ModelAdmin):
pass
Expand All @@ -22,6 +46,7 @@ class ShoulderAdmin(admin.ModelAdmin):
class ArkAdmin(admin.ModelAdmin):
list_display = ["ark", "url"]
show_full_result_count = False
paginator = TimeLimitedPaginator


@admin.register(Key)
Expand Down

0 comments on commit 102c2ca

Please sign in to comment.