forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
322 lines (274 loc) · 7.52 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
# -*- coding: utf-8 -*-
"""
Note: Code to make this work with Django 1.5+ customer user models
was inspired by work by Andrew Brown (@almostabc).
"""
from django.contrib import admin
from django.db.models.fields import FieldDoesNotExist
from .models import Event, EventProcessingException, Transfer, Charge, Plan
from .models import Invoice, InvoiceItem, CurrentSubscription, Customer
from .settings import User
if hasattr(User, 'USERNAME_FIELD'):
# Using a Django 1.5 User model
user_search_fields = [
"customer__user__{0}".format(User.USERNAME_FIELD)
]
try:
# get_field_by_name throws FieldDoesNotExist if the field is not present on the model
User._meta.get_field_by_name('email')
user_search_fields + ["customer__user__email"]
except FieldDoesNotExist:
pass
else:
# Using a pre-Django 1.5 User model
user_search_fields = [
"customer__user__username",
"customer__user__email"
]
class CustomerHasCardListFilter(admin.SimpleListFilter):
title = "card presence"
parameter_name = "has_card"
def lookups(self, request, model_admin):
return [
["yes", "Has Card"],
["no", "Does Not Have a Card"]
]
def queryset(self, request, queryset):
if self.value() == "yes":
return queryset.exclude(card_fingerprint="")
if self.value() == "no":
return queryset.filter(card_fingerprint="")
class InvoiceCustomerHasCardListFilter(admin.SimpleListFilter):
title = "card presence"
parameter_name = "has_card"
def lookups(self, request, model_admin):
return [
["yes", "Has Card"],
["no", "Does Not Have a Card"]
]
def queryset(self, request, queryset):
if self.value() == "yes":
return queryset.exclude(customer__card_fingerprint="")
if self.value() == "no":
return queryset.filter(customer__card_fingerprint="")
class CustomerSubscriptionStatusListFilter(admin.SimpleListFilter):
title = "subscription status"
parameter_name = "sub_status"
def lookups(self, request, model_admin):
statuses = [
[x, x.replace("_", " ").title()]
for x in CurrentSubscription.objects.all().values_list(
"status",
flat=True
).distinct()
]
statuses.append(["none", "No Subscription"])
return statuses
def queryset(self, request, queryset):
if self.value() is None:
return queryset.all()
else:
return queryset.filter(current_subscription__status=self.value())
def send_charge_receipt(modeladmin, request, queryset):
"""
Function for sending receipts from the admin if a receipt is not sent for
a specific charge.
"""
for charge in queryset:
charge.send_receipt()
admin.site.register(
Charge,
readonly_fields=('created',),
list_display=[
"stripe_id",
"customer",
"amount",
"description",
"paid",
"disputed",
"refunded",
"fee",
"receipt_sent",
"created"
],
search_fields=[
"stripe_id",
"customer__stripe_id",
"customer__user__email",
"card_last_4",
"customer__user__username",
"invoice__stripe_id"
] + user_search_fields,
list_filter=[
"paid",
"disputed",
"refunded",
"card_kind",
"created"
],
raw_id_fields=[
"customer",
"invoice"
],
actions=(send_charge_receipt,),
)
admin.site.register(
EventProcessingException,
readonly_fields=('created',),
list_display=[
"message",
"event",
"created"
],
search_fields=[
"message",
"traceback",
"data"
],
)
admin.site.register(
Event,
raw_id_fields=["customer"],
readonly_fields=('created',),
list_display=[
"stripe_id",
"kind",
"livemode",
"valid",
"processed",
"created"
],
list_filter=[
"kind",
"created",
"valid",
"processed"
],
search_fields=[
"stripe_id",
"customer__stripe_id",
"customer__user__username",
"customer__user__email",
"validated_message"
] + user_search_fields,
)
class CurrentSubscriptionInline(admin.TabularInline):
model = CurrentSubscription
def subscription_status(obj):
return obj.current_subscription.status
subscription_status.short_description = "Subscription Status"
admin.site.register(
Customer,
raw_id_fields=["user"],
readonly_fields=('created',),
list_display=[
"stripe_id",
"user",
"card_kind",
"card_last_4",
subscription_status,
"created"
],
list_filter=[
"card_kind",
CustomerHasCardListFilter,
CustomerSubscriptionStatusListFilter
],
search_fields=[
"stripe_id",
"user__username",
"user__email"
] + user_search_fields,
inlines=[CurrentSubscriptionInline]
)
class InvoiceItemInline(admin.TabularInline):
model = InvoiceItem
def customer_has_card(obj):
return obj.customer.card_fingerprint != ""
customer_has_card.short_description = "Customer Has Card"
def customer_user(obj):
if hasattr(obj, 'USERNAME_FIELD'):
# Using a Django 1.5 User model
username = getattr(obj.customer.user, User.USERNAME_FIELD)
else:
# Using a pre-Django 1.5 User model
username = obj.customer.user.username
# In Django 1.5+ a User is not guaranteed to have an email field
email = getattr(obj.customer.user, 'email', '')
return "{0} <{1}>".format(
username,
email
)
customer_has_card.short_description = "Customer"
admin.site.register(
Invoice,
raw_id_fields=["customer"],
readonly_fields=('created',),
list_display=[
"stripe_id",
"paid",
"closed",
customer_user,
customer_has_card,
"period_start",
"period_end",
"subtotal",
"total",
"created"
],
search_fields=[
"stripe_id",
"customer__stripe_id",
"customer__user__username",
"customer__user__email"
] + user_search_fields,
list_filter=[
InvoiceCustomerHasCardListFilter,
"paid",
"closed",
"attempted",
"attempts",
"created",
"date",
"period_end",
"total"
],
inlines=[InvoiceItemInline]
)
admin.site.register(
Transfer,
raw_id_fields=["event"],
readonly_fields=('created',),
list_display=[
"stripe_id",
"amount",
"status",
"date",
"description",
"created"
],
search_fields=[
"stripe_id",
"event__stripe_id"
]
)
class PlanAdmin(admin.ModelAdmin):
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):
readonly_fields = list(self.readonly_fields)
if obj:
readonly_fields.extend([
'stripe_id',
'amount',
'currency',
'interval',
'interval_count',
'trial_period_days'])
return readonly_fields
admin.site.register(Plan, PlanAdmin)