Skip to content

Commit

Permalink
Update forward relation fields in the admin to be raw id fields (dj-s…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Causey authored and therefromhere committed Oct 14, 2019
1 parent fcf49ed commit bbfb005
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ History
- Renamed ``Invoice.billing`` to ``.collection_method`` (added deprecated property for the old name).
- Updated ``Invoice`` model to add missing fields.
- Change urls.py to use the new style urls.
- Update forward relation fields in the admin to be raw id fields.

Warning about safe uninstall of jsonfield on upgrade
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
37 changes: 28 additions & 9 deletions djstripe/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@
from . import models


def get_forward_relation_fields_for_model(model):
"""Return an iterable of the field names that are forward relations,
I.E ManyToManyField, OneToOneField, and ForeignKey.
Useful for perhaps ensuring the admin is always using raw ID fields for
newly added forward relation fields.
"""
return [
field.name
for field in model._meta.get_fields()
# Get only relation fields
if field.is_relation
# Exclude auto relation fields, like reverse one to one.
and not field.auto_created
# We only want forward relations.
and any((field.many_to_many, field.one_to_one, field.many_to_one))
]


class BaseHasSourceListFilter(admin.SimpleListFilter):
title = "source presence"
parameter_name = "has_source"
Expand Down Expand Up @@ -107,7 +126,7 @@ class WebhookEventTriggerAdmin(admin.ModelAdmin):
"djstripe_version",
)
list_filter = ("created", "valid", "processed")
raw_id_fields = ("event",)
raw_id_fields = get_forward_relation_fields_for_model(models.WebhookEventTrigger)

def reprocess(self, request, queryset):
for trigger in queryset:
Expand All @@ -126,6 +145,11 @@ class StripeModelAdmin(admin.ModelAdmin):

change_form_template = "djstripe/admin/change_form.html"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.raw_id_fields = get_forward_relation_fields_for_model(self.model)

def get_list_display(self, request):
return ("id",) + self.list_display + ("created", "livemode")

Expand Down Expand Up @@ -155,6 +179,7 @@ class SubscriptionInline(admin.StackedInline):
model = models.Subscription
extra = 0
readonly_fields = ("id", "created")
raw_id_fields = get_forward_relation_fields_for_model(model)
show_change_link = True


Expand All @@ -164,6 +189,7 @@ class SubscriptionItemInline(admin.StackedInline):
model = models.SubscriptionItem
extra = 0
readonly_fields = ("id", "created")
raw_id_fields = get_forward_relation_fields_for_model(model)
show_change_link = True


Expand All @@ -173,7 +199,7 @@ class InvoiceItemInline(admin.StackedInline):
model = models.InvoiceItem
extra = 0
readonly_fields = ("id", "created")
raw_id_fields = ("customer", "subscription", "plan")
raw_id_fields = get_forward_relation_fields_for_model(model)
show_change_link = True


Expand All @@ -182,7 +208,6 @@ class AccountAdmin(StripeModelAdmin):
list_display = ("business_url", "country", "default_currency")
list_filter = ("details_submitted",)
search_fields = ("settings", "business_profile")
raw_id_fields = ("branding_icon",)


@admin.register(models.Charge)
Expand All @@ -198,7 +223,6 @@ class ChargeAdmin(StripeModelAdmin):
)
search_fields = ("customer__id", "invoice__id")
list_filter = ("status", "paid", "refunded", "captured")
raw_id_fields = ("customer", "dispute", "invoice", "source", "transfer")


@admin.register(models.Coupon)
Expand All @@ -218,7 +242,6 @@ class CouponAdmin(StripeModelAdmin):

@admin.register(models.Customer)
class CustomerAdmin(StripeModelAdmin):
raw_id_fields = ("subscriber", "default_source", "coupon")
list_display = (
"subscriber",
"email",
Expand Down Expand Up @@ -315,7 +338,6 @@ class InvoiceAdmin(StripeModelAdmin):
"period_start",
"period_end",
)
raw_id_fields = ("customer", "charge", "subscription")
search_fields = ("customer__id", "number", "receipt_number")
inlines = (InvoiceItemInline,)

Expand Down Expand Up @@ -370,21 +392,18 @@ class RefundAdmin(StripeModelAdmin):

@admin.register(models.Source)
class SourceAdmin(StripeModelAdmin):
raw_id_fields = ("customer",)
list_display = ("customer", "type", "status", "amount", "currency", "usage", "flow")
list_filter = ("type", "status", "usage", "flow")


@admin.register(models.PaymentMethod)
class PaymentMethodAdmin(StripeModelAdmin):
raw_id_fields = ("customer",)
list_display = ("customer", "billing_details")
list_filter = ("customer",)


@admin.register(models.Subscription)
class SubscriptionAdmin(StripeModelAdmin):
raw_id_fields = ("customer",)
list_display = ("customer", "status")
list_filter = ("status", "cancel_at_period_end")

Expand Down

0 comments on commit bbfb005

Please sign in to comment.