Files

94 lines
2.8 KiB
Python
Raw Permalink Normal View History

2024-02-29 13:25:50 +05:30
from django.contrib import admin
2024-04-19 15:17:29 +05:30
from .models import (
PrincipalSubscription,
Subscription,
WebhookEvent,
) # Update this with the correct import path for your models
2024-02-29 13:25:50 +05:30
# 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
2024-04-19 15:17:29 +05:30
search_fields = (
"title",
"interval",
) # Add search functionality by title and interval's title
2024-04-19 15:17:29 +05:30
2024-02-29 13:25:50 +05:30
# Register Subscription with the admin site
admin.site.register(Subscription, SubscriptionAdmin)
2024-04-19 15:17:29 +05:30
2024-02-29 13:25:50 +05:30
class PrincipalSubscriptionAdmin(admin.ModelAdmin):
2024-04-19 15:17:29 +05:30
list_display = (
"id",
"subscription",
"principal",
"is_paid",
"auto_renew",
"status",
"start_date",
"end_date",
)
list_filter = (
"is_paid",
"auto_renew",
"status",
# "cancelled",
2024-04-19 15:17:29 +05:30
) # 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
2024-02-29 13:25:50 +05:30
admin.site.register(PrincipalSubscription, PrincipalSubscriptionAdmin)
2024-04-19 15:17:29 +05:30
@admin.register(WebhookEvent)
class WebhookEventAdmin(admin.ModelAdmin):
2024-04-19 16:49:52 +05:30
list_display = ("event_id", "received_at", "event_type", "processed_at", "status")
2024-04-19 15:17:29 +05:30
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",
2024-04-19 16:49:52 +05:30
"event_type",
2024-04-19 15:17:29 +05:30
"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)