Files
goodtimes/manage_subscriptions/admin.py
2024-08-23 12:26:09 +05:30

94 lines
2.8 KiB
Python

from django.contrib import admin
from .models import (
PrincipalSubscription,
Subscription,
WebhookEvent,
) # Update this with the correct import path for your models
# Subscription ModelAdmin
class SubscriptionAdmin(admin.ModelAdmin):
list_display = ("id", "title", "interval", "amount") # Include 'id' field here
list_select_related = ("interval",) # Optimizes queries for the interval field
search_fields = (
"title",
"interval",
) # Add search functionality by title and interval's title
# Register Subscription with the admin site
admin.site.register(Subscription, SubscriptionAdmin)
class PrincipalSubscriptionAdmin(admin.ModelAdmin):
list_display = (
"id",
"subscription",
"principal",
"is_paid",
"auto_renew",
"status",
"start_date",
"end_date",
)
list_filter = (
"is_paid",
"auto_renew",
"status",
# "cancelled",
) # Enable filtering by these fields
search_fields = (
"subscription__title",
"principal__name",
"order_id",
) # Adjust 'principal__name' as necessary
raw_id_fields = (
"subscription",
"principal",
) # Use raw ID widget for these ForeignKey fields
date_hierarchy = "start_date" # Enables a date drill down by start_date
admin.site.register(PrincipalSubscription, PrincipalSubscriptionAdmin)
@admin.register(WebhookEvent)
class WebhookEventAdmin(admin.ModelAdmin):
list_display = ("event_id", "received_at", "event_type", "processed_at", "status")
list_filter = ("status", "received_at", "processed_at")
search_fields = ("event_id", "status")
readonly_fields = ("received_at", "event_id")
def get_readonly_fields(self, request, obj=None):
if (
obj
): # This makes sure that fields are readonly after the object has been created
return ["received_at", "event_id", "event_payload"] + list(
super().get_readonly_fields(request, obj)
)
return super().get_readonly_fields(request, obj)
fieldsets = (
(
None,
{
"fields": (
"event_id",
"received_at",
"processed_at",
"event_type",
"status",
"error_message",
"event_payload",
)
},
),
)
def has_add_permission(self, request, obj=None):
# Disable add permission if you do not want admins to manually create events
return False
def has_delete_permission(self, request, obj=None):
# Optionally, restrict delete permission
return super().has_delete_permission(request, obj)