membershipworks: Add "refresh data" changelist actions to admin

This commit is contained in:
Adam Goldsmith 2023-12-21 14:56:38 -05:00
parent 5ddd0e68ac
commit 7afcc1f9e0

View File

@ -1,6 +1,12 @@
from django.contrib import admin
from django.contrib.humanize.templatetags.humanize import naturaltime
from django_object_actions import DjangoObjectActions, action
from django_q.tasks import async_task
from django_q.models import Task
from .models import Member, Flag, Transaction
from .tasks.scrape import scrape_membershipworks
class ReadOnlyAdmin(admin.ModelAdmin):
@ -14,18 +20,42 @@ class ReadOnlyAdmin(admin.ModelAdmin):
return False
class BaseMembershipWorksAdmin(DjangoObjectActions, ReadOnlyAdmin):
changelist_actions = ("refresh_membershipworks_data",)
# internal method from DjangoObjectActions
def _get_tool_dict(self, tool_name):
tool = super(DjangoObjectActions, self)._get_tool_dict(tool_name)
if tool_name == "refresh_membershipworks_data":
last_run = (
Task.objects.filter(group="Scrape Data from MembershipWorks")
.order_by("started")
.last()
)
tool["label"] = f"Refresh Data [Last Run {naturaltime(last_run.started)}]"
return tool
@action
def refresh_membershipworks_data(self, request, obj):
async_task(scrape_membershipworks, group="Scrape Data from MembershipWorks")
self.message_user(
request,
"Queued refresh, please wait a few seconds/minutes then refresh the page",
)
class MemberFlagInline(admin.TabularInline):
model = Member.flags.through
@admin.register(Member)
class MemberAdmin(ReadOnlyAdmin):
class MemberAdmin(BaseMembershipWorksAdmin):
search_fields = ["^first_name", "^last_name", "^account_name"]
inlines = [MemberFlagInline]
@admin.register(Flag)
class FlagAdmin(ReadOnlyAdmin):
class FlagAdmin(BaseMembershipWorksAdmin):
inlines = [MemberFlagInline]
list_display = ["name", "type"]
list_filter = ["type"]
@ -34,7 +64,7 @@ class FlagAdmin(ReadOnlyAdmin):
@admin.register(Transaction)
class TransactionAdmin(ReadOnlyAdmin):
class TransactionAdmin(BaseMembershipWorksAdmin):
list_display = ["timestamp", "member", "name", "type", "sum", "note"]
list_filter = ["type"]
show_facets = admin.ShowFacets.ALWAYS