2022-02-03 13:45:58 -05:00
|
|
|
from django.contrib import admin
|
2023-12-21 14:56:38 -05:00
|
|
|
from django.contrib.humanize.templatetags.humanize import naturaltime
|
2024-01-02 10:54:29 -05:00
|
|
|
from django.utils.html import format_html
|
2023-12-21 14:56:38 -05:00
|
|
|
|
2024-01-29 21:48:19 -05:00
|
|
|
from django_object_actions import (
|
|
|
|
DjangoObjectActions,
|
|
|
|
action,
|
|
|
|
takes_instance_or_queryset,
|
|
|
|
)
|
2023-12-21 14:56:38 -05:00
|
|
|
from django_q.models import Task
|
2024-01-17 21:17:24 -05:00
|
|
|
from django_q.tasks import async_task
|
2022-02-03 13:45:58 -05:00
|
|
|
|
2023-12-30 14:34:55 -05:00
|
|
|
from .models import (
|
|
|
|
Event,
|
|
|
|
EventExt,
|
|
|
|
EventInstructor,
|
2024-04-05 14:10:08 -04:00
|
|
|
EventInvoice,
|
2024-01-17 21:17:24 -05:00
|
|
|
EventMeetingTime,
|
|
|
|
Flag,
|
|
|
|
Member,
|
|
|
|
Transaction,
|
2023-12-30 14:34:55 -05:00
|
|
|
)
|
2024-01-29 21:48:19 -05:00
|
|
|
from .tasks.scrape import (
|
|
|
|
scrape_event_details,
|
|
|
|
scrape_membershipworks,
|
|
|
|
)
|
2024-02-08 15:45:02 -05:00
|
|
|
from .tasks.ucsAccounts import sync_accounts
|
2022-02-03 13:45:58 -05:00
|
|
|
|
|
|
|
|
2022-02-10 16:51:32 -05:00
|
|
|
class ReadOnlyAdmin(admin.ModelAdmin):
|
2022-02-03 13:45:58 -05:00
|
|
|
def has_add_permission(self, request, obj=None):
|
|
|
|
return False
|
|
|
|
|
2022-02-10 16:51:32 -05:00
|
|
|
def has_change_permission(self, request, obj=None):
|
|
|
|
return False
|
|
|
|
|
2022-02-03 13:45:58 -05:00
|
|
|
def has_delete_permission(self, request, obj=None):
|
|
|
|
return False
|
2022-02-10 16:51:32 -05:00
|
|
|
|
|
|
|
|
2023-12-21 14:56:38 -05:00
|
|
|
class BaseMembershipWorksAdmin(DjangoObjectActions, ReadOnlyAdmin):
|
2024-02-08 15:45:02 -05:00
|
|
|
changelist_actions = ("refresh_membershipworks_data", "sync_ucs_accounts")
|
2023-12-21 14:56:38 -05:00
|
|
|
|
|
|
|
# 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":
|
2024-01-17 11:19:32 -05:00
|
|
|
try:
|
|
|
|
last_run_time = naturaltime(
|
2024-02-01 11:10:22 -05:00
|
|
|
Task.objects.filter(group=scrape_membershipworks.q_task_group)
|
2024-01-17 11:19:32 -05:00
|
|
|
.values_list("started", flat=True)
|
|
|
|
.latest("started")
|
|
|
|
)
|
|
|
|
except Task.DoesNotExist:
|
|
|
|
last_run_time = "Never"
|
2023-12-22 13:37:19 -05:00
|
|
|
tool["label"] = f"Refresh Data [Last Run {last_run_time}]"
|
2023-12-21 14:56:38 -05:00
|
|
|
return tool
|
|
|
|
|
|
|
|
@action
|
|
|
|
def refresh_membershipworks_data(self, request, obj):
|
2024-02-01 11:10:22 -05:00
|
|
|
async_task(scrape_membershipworks, group=scrape_membershipworks.q_task_group)
|
2023-12-21 14:56:38 -05:00
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
"Queued refresh, please wait a few seconds/minutes then refresh the page",
|
|
|
|
)
|
|
|
|
|
2024-02-08 15:45:02 -05:00
|
|
|
@action
|
|
|
|
def sync_ucs_accounts(self, request, obj):
|
|
|
|
async_task(sync_accounts, group=sync_accounts.q_task_group)
|
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
"Queued refresh, please wait a few seconds/minutes then refresh the page",
|
|
|
|
)
|
|
|
|
|
2023-12-21 14:56:38 -05:00
|
|
|
|
2022-02-10 16:51:32 -05:00
|
|
|
class MemberFlagInline(admin.TabularInline):
|
|
|
|
model = Member.flags.through
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Member)
|
2023-12-21 14:56:38 -05:00
|
|
|
class MemberAdmin(BaseMembershipWorksAdmin):
|
2023-03-31 23:54:16 -04:00
|
|
|
search_fields = ["^first_name", "^last_name", "^account_name"]
|
2022-02-10 16:51:32 -05:00
|
|
|
inlines = [MemberFlagInline]
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Flag)
|
2023-12-21 14:56:38 -05:00
|
|
|
class FlagAdmin(BaseMembershipWorksAdmin):
|
2022-02-10 16:51:32 -05:00
|
|
|
inlines = [MemberFlagInline]
|
2023-01-19 19:02:03 -05:00
|
|
|
list_display = ["name", "type"]
|
|
|
|
list_filter = ["type"]
|
2023-12-04 13:08:36 -05:00
|
|
|
show_facets = admin.ShowFacets.ALWAYS
|
2023-01-19 19:02:03 -05:00
|
|
|
search_fields = ["name"]
|
2023-12-19 23:42:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Transaction)
|
2023-12-21 14:56:38 -05:00
|
|
|
class TransactionAdmin(BaseMembershipWorksAdmin):
|
2023-12-19 23:42:46 -05:00
|
|
|
list_display = ["timestamp", "member", "name", "type", "sum", "note"]
|
|
|
|
list_filter = ["type"]
|
|
|
|
show_facets = admin.ShowFacets.ALWAYS
|
2023-12-29 18:19:58 -05:00
|
|
|
search_fields = ["member", "name", "type", "note"]
|
|
|
|
date_hierarchy = "timestamp"
|
2023-12-30 14:34:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
class EventMeetingTimeInline(admin.TabularInline):
|
|
|
|
model = EventMeetingTime
|
|
|
|
extra = 0
|
|
|
|
min_num = 1
|
|
|
|
|
|
|
|
readonly_fields = ["duration"]
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(EventInstructor)
|
|
|
|
class EventInstructorAdmin(admin.ModelAdmin):
|
|
|
|
autocomplete_fields = ["member"]
|
2024-01-29 21:49:45 -05:00
|
|
|
search_fields = ["name", "member__account_name"]
|
2023-12-30 14:34:55 -05:00
|
|
|
|
|
|
|
|
2024-04-05 14:10:08 -04:00
|
|
|
class EventInvoiceInline(admin.StackedInline):
|
|
|
|
model = EventInvoice
|
|
|
|
|
|
|
|
|
2023-12-30 14:34:55 -05:00
|
|
|
@admin.register(EventExt)
|
2024-01-29 21:48:19 -05:00
|
|
|
class EventAdmin(DjangoObjectActions, admin.ModelAdmin):
|
2024-04-05 14:10:08 -04:00
|
|
|
inlines = [EventInvoiceInline, EventMeetingTimeInline]
|
2023-12-30 14:34:55 -05:00
|
|
|
list_display = [
|
2024-04-04 00:23:06 -04:00
|
|
|
"unescaped_title",
|
2023-12-30 14:34:55 -05:00
|
|
|
"start",
|
|
|
|
"duration",
|
|
|
|
"count",
|
|
|
|
"cap",
|
|
|
|
"category",
|
2024-01-02 10:54:29 -05:00
|
|
|
]
|
|
|
|
list_filter = [
|
|
|
|
"category",
|
|
|
|
"calendar",
|
2023-12-30 14:34:55 -05:00
|
|
|
"venue",
|
2024-01-02 10:54:29 -05:00
|
|
|
("materials_fee", admin.EmptyFieldListFilter),
|
2023-12-30 14:34:55 -05:00
|
|
|
]
|
|
|
|
show_facets = admin.ShowFacets.ALWAYS
|
|
|
|
search_fields = ["eid", "title", "url"]
|
|
|
|
date_hierarchy = "start"
|
2024-01-29 21:48:19 -05:00
|
|
|
exclude = ["url", "details"]
|
2024-01-02 19:26:06 -05:00
|
|
|
autocomplete_fields = ["instructor"]
|
2024-01-29 21:48:19 -05:00
|
|
|
change_actions = ["fetch_details"]
|
|
|
|
actions = ["fetch_details"]
|
2023-12-30 14:34:55 -05:00
|
|
|
|
2024-01-02 10:54:29 -05:00
|
|
|
@property
|
|
|
|
def readonly_fields(self):
|
|
|
|
fields = []
|
|
|
|
for field in Event._meta.get_fields():
|
|
|
|
if field.auto_created or field.many_to_many or not field.concrete:
|
|
|
|
continue
|
|
|
|
elif field.name == "url":
|
|
|
|
fields.append("_url")
|
|
|
|
else:
|
|
|
|
fields.append(field.name)
|
|
|
|
fields.insert(fields.index("end") + 1, "duration")
|
2024-05-08 12:45:34 -04:00
|
|
|
fields.append("details_timestamp")
|
2024-01-02 10:54:29 -05:00
|
|
|
return fields
|
|
|
|
|
2024-04-04 00:23:06 -04:00
|
|
|
@admin.display(ordering="title")
|
|
|
|
def unescaped_title(self, obj):
|
|
|
|
return obj.unescaped_title
|
|
|
|
|
2024-01-02 10:54:29 -05:00
|
|
|
@admin.display(ordering="duration")
|
2023-12-30 14:34:55 -05:00
|
|
|
def duration(self, obj):
|
|
|
|
return obj.duration
|
|
|
|
|
2024-01-02 10:54:29 -05:00
|
|
|
@admin.display(description="URL")
|
|
|
|
def _url(self, obj):
|
|
|
|
return format_html(
|
|
|
|
'<a href="https://claremontmakerspace.org/events/#!event/{0}">{0}</a>',
|
|
|
|
obj.url,
|
|
|
|
)
|
|
|
|
|
2024-01-29 21:48:19 -05:00
|
|
|
@takes_instance_or_queryset
|
|
|
|
def fetch_details(self, request, queryset):
|
|
|
|
scrape_event_details(queryset)
|
|
|
|
|
2023-12-30 14:34:55 -05:00
|
|
|
def has_add_permission(self, request, obj=None):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def has_delete_permission(self, request, obj=None):
|
|
|
|
return False
|