Skip to content

Commit

Permalink
Merge pull request #4577 from freelawproject/pghistory_admin_page
Browse files Browse the repository at this point in the history
feat(pghistory): enable Events admin for single object's history
  • Loading branch information
quevon24 authored Oct 17, 2024
2 parents 6ae2865 + ad9ce7c commit 1201181
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cl/settings/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@

INSTALLED_APPS = [
"daphne",
"pghistory.admin",
"django.contrib.admin",
"django.contrib.admindocs",
"django.contrib.contenttypes",
Expand Down Expand Up @@ -189,7 +190,6 @@

ASGI_APPLICATION = "cl.asgi.application"


################
# Misc. Django #
################
Expand Down
36 changes: 36 additions & 0 deletions cl/settings/third_party/pghistory.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
# Default pghistory trackers for @pghistory.track()
import pghistory
from pghistory.admin.core import (
BackFilter,
EventModelFilter,
LabelFilter,
MethodFilter,
ObjFilter,
)


class EventsAdminNoFilters(pghistory.admin.EventsAdmin):
"""Disable filters, except when looking at the events of a single object.
In combination with `PGHISTORY_ADMIN_ALL_EVENTS = False`
it causes the Events admin page to make no queries, except
when looking at a single object's history, preventing
useless DB hits
"""

def get_list_filter(self, request):
filters = []
if "obj" in request.GET:
filters = [
LabelFilter,
EventModelFilter,
MethodFilter,
ObjFilter,
BackFilter,
]

return filters


PGHISTORY_DEFAULT_TRACKERS = (
pghistory.UpdateEvent(
condition=pghistory.AnyChange(exclude_auto=True), row=pghistory.Old
),
pghistory.DeleteEvent(),
)

# Disable the page with all Pghistory events, this page
# will only be viewable with filters
PGHISTORY_ADMIN_ALL_EVENTS = False
PGHISTORY_ADMIN_CLASS = EventsAdminNoFilters

0 comments on commit 1201181

Please sign in to comment.