forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
348 lines (258 loc) · 11.2 KB
/
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
.. module:: djstripe.admin.
:synopsis: dj-stripe - Django Administration interface definitions
.. moduleauthor:: Daniel Greenfeld (@pydanny)
.. moduleauthor:: Alex Kavanaugh (@kavdev)
.. moduleauthor:: Lee Skillen (@lskillen)
"""
from django.contrib import admin
from .models import (
Charge, Coupon, Customer, Event, EventProcessingException, IdempotencyKey,
Invoice, InvoiceItem, Plan, Subscription, Transfer
)
class CustomerHasSourceListFilter(admin.SimpleListFilter):
"""A SimpleListFilter used with Customer admin."""
title = "source presence"
parameter_name = "has_source"
def lookups(self, request, model_admin):
"""
Return a list of tuples.
The first element in each tuple is the coded value for the option that will
appear in the URL query. The second element is the
human-readable name for the option that will appear
in the right sidebar.
source: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
"""
return [
["yes", "Has Source"],
["no", "Does Not Have Source"]
]
def queryset(self, request, queryset):
"""
Return the filtered queryset based on the value provided in the query string.
source: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
"""
if self.value() == "yes":
return queryset.exclude(default_source=None)
if self.value() == "no":
return queryset.filter(default_source=None)
class InvoiceCustomerHasSourceListFilter(admin.SimpleListFilter):
"""A SimpleListFilter used with Invoice admin."""
title = "source presence"
parameter_name = "has_source"
def lookups(self, request, model_admin):
"""
Return a list of tuples.
The first element in each tuple is the coded value for the option that will
appear in the URL query. The second element is the
human-readable name for the option that will appear
in the right sidebar.
source: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
"""
return [
["yes", "Has Source"],
["no", "Does Not Have Source"]
]
def queryset(self, request, queryset):
"""
Return the filtered queryset based on the value provided in the query string.
source: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
"""
if self.value() == "yes":
return queryset.exclude(customer__default_source=None)
if self.value() == "no":
return queryset.filter(customer__default_source=None)
class CustomerSubscriptionStatusListFilter(admin.SimpleListFilter):
"""A SimpleListFilter used with Customer admin."""
title = "subscription status"
parameter_name = "sub_status"
def lookups(self, request, model_admin):
"""
Return a list of tuples.
The first element in each tuple is the coded value for the option that will
appear in the URL query. The second element is the
human-readable name for the option that will appear
in the right sidebar.
source: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
"""
statuses = [
[x, x.replace("_", " ").title()]
for x in Subscription.objects.values_list(
"status",
flat=True
).distinct()
]
statuses.append(["none", "No Subscription"])
return statuses
def queryset(self, request, queryset):
"""
Return the filtered queryset based on the value provided in the query string.
source: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
"""
if self.value() is None:
return queryset.all()
else:
return queryset.filter(subscriptions__status=self.value()).distinct()
@admin.register(IdempotencyKey)
class IdempotencyKeyAdmin(admin.ModelAdmin):
list_display = ("uuid", "action", "created", "is_expired", "livemode")
list_filter = ("livemode", )
search_fields = ("uuid", "action")
@admin.register(EventProcessingException)
class EventProcessingExceptionAdmin(admin.ModelAdmin):
list_display = ("message", "event", "created")
raw_id_fields = ("event", )
search_fields = ("message", "traceback", "data")
def has_add_permission(self, request):
return False
class StripeObjectAdmin(admin.ModelAdmin):
"""Base class for all StripeObject-based model admins"""
change_form_template = "djstripe/admin/change_form.html"
def get_list_display(self, request):
return ("stripe_id", ) + self.list_display + ("stripe_timestamp", "livemode")
def get_list_filter(self, request):
return self.list_filter + ("stripe_timestamp", "livemode")
def get_readonly_fields(self, request, obj=None):
return self.readonly_fields + ("stripe_id", "stripe_timestamp")
def get_search_fields(self, request):
return self.search_fields + ("stripe_id", )
def get_fieldsets(self, request, obj=None):
common_fields = ("livemode", "stripe_id", "stripe_timestamp")
# Have to remove the fields from the common set, otherwise they'll show up twice.
fields = [f for f in self.get_fields(request, obj) if f not in common_fields]
return (
(None, {"fields": common_fields}),
(self.model.__name__, {"fields": fields}),
)
def reprocess_events(modeladmin, request, queryset):
"""Re-process the selected webhook events.
Note that this isn't idempotent, so any side-effects that are produced from
the event being handled will be multiplied (for example, an event handler
that sends emails will send duplicates; an event handler that adds 1 to a
total count will be a count higher than it was, etc.)
There aren't any event handlers with adverse side-effects built within
dj-stripe, but there might be within your own event handlers, third-party
plugins, contrib code, etc.
"""
processed = 0
for event in queryset:
if event.process(force=True):
processed += 1
message = "{processed}/{total} event(s) successfully re-processed."
total = queryset.count()
modeladmin.message_user(request, message.format(processed=processed, total=total))
reprocess_events.short_description = "Re-process selected webhook events"
class SubscriptionInline(admin.StackedInline):
"""A TabularInline for use models.Subscription."""
model = Subscription
extra = 0
readonly_fields = ("stripe_id", "stripe_timestamp")
show_change_link = True
def subscription_status(customer):
"""
Return a string representation of the customer's subscription status.
If the customer does not have a subscription, an empty string is returned.
"""
if customer.subscriptions.count() > 1:
return "Multiple"
elif customer.subscription:
return customer.subscription.status
else:
return ""
subscription_status.short_description = "Subscription Status"
def cancel_subscription(modeladmin, request, queryset):
"""Cancel a subscription."""
for subscription in queryset:
subscription.cancel()
cancel_subscription.short_description = "Cancel selected subscriptions"
class InvoiceItemInline(admin.StackedInline):
"""A TabularInline for use InvoiceItem."""
model = InvoiceItem
extra = 0
readonly_fileds = ("stripe_id", "stripe_timestamp")
raw_id_fields = ("customer", "subscription")
show_change_link = True
def customer_has_source(obj):
"""Return True if the customer has a source attached to its account."""
return obj.customer.default_source is not None
customer_has_source.short_description = "Customer Has Source"
def customer_email(obj):
"""Return a string representation of the customer's email."""
if obj.customer.subscriber:
return str(obj.customer.subscriber.email)
else:
return ""
customer_email.short_description = "Customer"
@admin.register(Charge)
class ChargeAdmin(StripeObjectAdmin):
list_display = (
"customer", "amount", "description", "paid", "disputed", "refunded",
"fee", "receipt_sent"
)
search_fields = ("stripe_id", "customer__stripe_id", "invoice__stripe_id")
list_filter = (
"status", "paid", "disputed", "refunded", "fraudulent", "captured",
)
raw_id_fields = ("customer", "source", "transfer")
@admin.register(Coupon)
class CouponAdmin(StripeObjectAdmin):
list_display = (
"amount_off", "percent_off", "duration", "duration_in_months",
"redeem_by", "max_redemptions", "times_redeemed"
)
list_filter = ("duration", "redeem_by")
radio_fields = {"duration": admin.HORIZONTAL}
@admin.register(Customer)
class CustomerAdmin(StripeObjectAdmin):
raw_id_fields = ("subscriber", "default_source", "coupon")
list_display = ("subscriber", subscription_status)
list_filter = (CustomerHasSourceListFilter, CustomerSubscriptionStatusListFilter)
inlines = (SubscriptionInline, )
@admin.register(Event)
class EventAdmin(StripeObjectAdmin):
raw_id_fields = ("customer", )
list_display = ("type", "created", "valid", "processed")
list_filter = ("type", "created", "valid", "processed")
actions = (reprocess_events, )
# radio_fields = {"valid": admin.HORIZONTAL}
def has_add_permission(self, request):
return False
@admin.register(Invoice)
class InvoiceAdmin(StripeObjectAdmin):
list_display = (
"paid", "forgiven", "closed", customer_email, customer_has_source,
"period_start", "period_end", "subtotal", "total"
)
list_filter = (
InvoiceCustomerHasSourceListFilter, "paid", "forgiven", "closed", "attempted",
"date", "period_end",
)
raw_id_fields = ("customer", "charge", "subscription")
search_fields = ("customer__stripe_id", )
inlines = (InvoiceItemInline, )
@admin.register(Plan)
class PlanAdmin(StripeObjectAdmin):
radio_fields = {"interval": admin.HORIZONTAL}
def save_model(self, request, obj, form, change):
"""Update or create objects using our custom methods that sync with Stripe."""
if change:
obj.update_name()
else:
Plan.get_or_create(**form.cleaned_data)
def get_readonly_fields(self, request, obj=None):
"""Return extra readonly_fields."""
readonly_fields = super(PlanAdmin, self).get_readonly_fields(request, obj)
if obj:
readonly_fields += (
"amount", "currency", "interval", "interval_count", "trial_period_days"
)
return readonly_fields
@admin.register(Subscription)
class SubscriptionAdmin(StripeObjectAdmin):
raw_id_fields = ("customer", )
list_display = ("customer", "status")
list_filter = ("status", "cancel_at_period_end")
actions = (cancel_subscription, )
@admin.register(Transfer)
class TransferAdmin(StripeObjectAdmin):
list_display = ("amount", "status", "date", "description")