107 lines
3.1 KiB
Python
107 lines
3.1 KiB
Python
from django.contrib import admin
|
|
from .models import (
|
|
Plan,
|
|
PrincipalSubscription,
|
|
Subscription,
|
|
WebhookEvent,
|
|
) # Update this with the correct import path for your models
|
|
|
|
|
|
# Plan ModelAdmin
|
|
class PlanAdmin(admin.ModelAdmin):
|
|
list_display = ("id", "title", "days") # Include 'id' field here
|
|
search_fields = ("title",) # Add search functionality by title
|
|
|
|
|
|
# Register Plan with the admin site
|
|
admin.site.register(Plan, PlanAdmin)
|
|
|
|
|
|
# Subscription ModelAdmin
|
|
class SubscriptionAdmin(admin.ModelAdmin):
|
|
list_display = ("id", "title", "plan", "amount") # Include 'id' field here
|
|
list_select_related = ("plan",) # Optimizes queries for the plan field
|
|
search_fields = (
|
|
"title",
|
|
"plan__title",
|
|
) # Add search functionality by title and plan's title
|
|
raw_id_fields = ("plan",) # Use a raw ID widget for the plan ForeignKey field
|
|
|
|
|
|
# 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)
|