-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
255 lines (239 loc) · 14.3 KB
/
admin.py
File metadata and controls
255 lines (239 loc) · 14.3 KB
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
# Copyright (c) 2014-2015, Doug Kelly
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from django.conf.urls import patterns, url
from django.contrib import admin
from django.contrib import messages
from django.contrib.admin.helpers import ActionForm
from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction
from django.shortcuts import render
from django import forms
import stripe
from register.models import *
class RegistrationAdminForm(ActionForm):
amount = forms.FloatField(widget=forms.NumberInput(attrs={'style': 'width:auto'}), required=False)
method = forms.ModelChoiceField(widget=forms.Select(attrs={'style': 'width:auto'}), empty_label=None, queryset=PaymentMethod.objects.order_by('seq'), required=False)
reprint = forms.BooleanField(required=False)
class RegistrationAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'badge_name', 'registration_level', 'shirt_size', 'checked_in', 'paid', 'badge_number')
list_filter = ('registration_level', 'shirt_size', 'checked_in', 'volunteer', 'registration_level__convention', 'payment__payment_received')
search_fields = ['first_name', 'last_name', 'badge_name', 'email', 'badgeassignment__id']
actions = ['mark_checked_in', 'apply_payment', 'refund_payment', 'print_badge', 'download_registration_detail']
action_form = RegistrationAdminForm
ordering = ('id',)
def get_urls(self):
urls = super(RegistrationAdmin, self).get_urls()
my_urls = patterns('',
url(r'^print/$', self.print_badge_list, name='print')
)
return my_urls + urls
def mark_checked_in(self, request, queryset):
queryset.update(checked_in=True)
for id in queryset:
self.message_user(request, '%s successfully checked in!' % id)
mark_checked_in.short_description = 'Check in attendee'
def apply_payment(self, request, queryset):
try:
amount = request.POST['amount']
method = PaymentMethod.objects.get(id=request.POST['method'])
except ObjectDoesNotExist:
method = None
if (method and amount):
for id in queryset:
payment = Payment(registration=id,
payment_method=method,
payment_amount=float(amount),
created_by=request.user)
payment.save()
self.message_user(request, 'Applied %.02f payment by %s to %s' % (float(amount), method, id))
else:
self.message_user(request, 'Must specify an amount and payment method!', messages.ERROR)
def refund_payment(self, request, queryset):
for id in queryset:
payments = Payment.objects.filter(registration=id)
for payment in payments:
if (payment.payment_method.is_credit and payment.payment_extra):
try:
stripe.api_key = payment.registration.registration_level.convention.stripe_secret_key
charge = stripe.Charge.retrieve(payment.payment_extra)
refund = charge.refunds.create()
payment.refunded_by = request.user
payment.save()
self.message_user(request, 'Refunded %.02f payment by Stripe to %s' % (refund.amount / 100, id))
except stripe.error.StripeError as e:
self.message_user(request, 'Failed to refund %.02f payment by Stripe to %s (%s)' % (payment.payment_amount, id, e.json_body['error']['message']), messages.ERROR)
else:
payment.refunded_by = request.user
payment.save()
self.message_user(request, 'Refunded %.02f payment by %s to %s' % (payment.payment_amount, payment.payment_method, id))
refund_payment.short_description = 'Refund all payments from attendee'
def print_badge(self, request, queryset):
printable = True
ac = transaction.get_autocommit()
transaction.set_autocommit(False)
for user in queryset:
if not user.paid():
self.message_user(request, 'Cannot print unpaid badge for %s' % (user), messages.ERROR)
printable = False
badge_number = None
reprint = request.POST.get('reprint')
if reprint:
badge_number = user.badge_number()
if not reprint or not badge_number:
badge = BadgeAssignment(registration=user, printed_by=request.user)
badge.save()
badge_number = badge.id
user.badge_number = '%05d' % int(badge_number)
if printable:
transaction.commit()
transaction.set_autocommit(ac)
return render(request, 'register/badge.html', {'badges': queryset})
else:
transaction.rollback()
transaction.set_autocommit(ac)
def print_badge_list(self, request):
badges = Registration.objects.filter(checked_in=False).order_by('last_name')
split_badges = []
temp_list = []
for badge in badges:
if badge.badge_number():
temp_list.append({'first_name': badge.first_name, 'last_name': badge.last_name, 'badge_name': badge.badge_name, 'badge_number': badge.badge_number(), 'registration_level': badge.registration_level.title})
if len(temp_list) == 25:
split_badges.append({'list': temp_list, 'last': False})
temp_list = []
if len(temp_list) > 0:
split_badges.append({'list': temp_list, 'last': True})
return render(request, 'register/badgelist.html', {'lists': split_badges})
def download_registration_detail(self, request, queryset):
registration_list = []
for badge in queryset:
payments = Payment.objects.filter(registration=badge)
for payment in payments:
discount_amount = ''
try:
coupon = CouponUse.objects.get(registration=badge)
if coupon.coupon.percent:
discount_amount = '%.02f' % ((coupon.coupon.discount / 100) * badge.registration_level.price)
else:
discount_amount = '%.02f' % (coupon.coupon.discount)
except ObjectDoesNotExist:
pass
registration_list.append({'first_name': badge.first_name,
'last_name': badge.last_name,
'email': badge.email,
'address': badge.address,
'city': badge.city,
'state': badge.state,
'postal_code': badge.postal_code,
'country': badge.country,
'badge_name': badge.badge_name.replace('"', '""'),
'badge_number': badge.badge_number(),
'registration_level': badge.registration_level.title.replace('"', '""'),
'dealer_registration_level': badge.dealer_registration_level.number_tables if badge.dealer_registration_level else '',
'payment_amount': '%.02f' % payment.payment_amount,
'payment_created': payment.payment_received,
'received_by': payment.created_by.username.replace('"', '""') if payment.created_by else '',
'refunded_by': payment.refunded_by.username.replace('"', '""') if payment.refunded_by else '',
'discount_amount': discount_amount,
'payment_method': payment.payment_method})
if not payments:
discount_amount = ''
try:
coupon = CouponUse.objects.get(registration=badge)
if coupon.coupon.percent:
discount_amount = '%.02f' % ((coupon.coupon.discount / 100) * badge.registration_level.price)
else:
discount_amount = '%.02f' % (coupon.coupon.discount)
except ObjectDoesNotExist:
pass
registration_list.append({'first_name': badge.first_name,
'last_name': badge.last_email,
'email': badge.email,
'address': badge.address,
'city': badge.city,
'state': badge.state,
'postal_code': badge.postal_code,
'country': badge.country,
'badge_name': badge.badge_name.replace('"', '""'),
'badge_number': badge.badge_number(),
'registration_level': badge.registration_level.title.replace('"', '""'),
'dealer_registration_level': badge.dealer_registration_level.number_tables if badge.dealer_registration_level else '',
'payment_amount': '0.00',
'payment_created': '',
'received_by': '',
'refunded_by': '',
'discount_amount': discount_amount,
'payment_method': ''})
response = render(request, 'register/regdetail.csv', {'badges': registration_list}, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="regdetail.csv"'
return response
admin.site.register(Registration, RegistrationAdmin)
class PaymentAdmin(admin.ModelAdmin):
actions = ['download_payment_detail']
list_display = ('registration', 'payment_method', 'payment_amount', 'payment_received', 'created_by', 'refunded_by')
list_filter = ('payment_method',)
search_fields = ['registration__first_name', 'registration__last_name', 'registration__badge_name', 'registration__email']
def download_payment_detail(self, request, queryset):
registration_list = []
for payment in queryset:
badge = payment.registration
discount_amount = ''
try:
coupon = CouponUse.objects.get(registration=badge)
if coupon.coupon.percent:
discount_amount = '%.02f' % ((coupon.coupon.discount / 100) * badge.registration_level.price)
else:
discount_amount = '%.02f' % (coupon.coupon.discount)
except ObjectDoesNotExist:
pass
registration_list.append({'first_name': badge.first_name,
'last_name': badge.last_name,
'address': badge.address,
'city': badge.city,
'state': badge.state,
'postal_code': badge.postal_code,
'country': badge.country,
'badge_name': badge.badge_name.replace('"', '""'),
'badge_number': badge.badge_number(),
'registration_level': badge.registration_level.title.replace('"', '""'),
'dealer_registration_level': badge.dealer_registration_level.number_tables if badge.dealer_registration_level else '',
'payment_amount': '%.02f' % payment.payment_amount,
'payment_created': payment.payment_received,
'received_by': payment.created_by.username.replace('"', '""') if payment.created_by else '',
'refunded_by': payment.refunded_by.username.replace('"', '""') if payment.refunded_by else '',
'discount_amount': discount_amount,
'payment_method': payment.payment_method})
response = render(request, 'register/regdetail.csv', {'badges': registration_list}, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="regdetail.csv"'
return response
admin.site.register(Payment, PaymentAdmin)
class BadgeAssignmentAdmin(admin.ModelAdmin):
search_fields = ['id', 'registration__first_name', 'registration__last_name', 'registration__badge_name', 'registration__email']
admin.site.register(BadgeAssignment, BadgeAssignmentAdmin)
admin.site.register(RegistrationLevel)
admin.site.register(DealerRegistrationLevel)
admin.site.register(PaymentMethod)
admin.site.register(Convention)
admin.site.register(ShirtSize)
admin.site.register(CouponCode)
admin.site.register(CouponUse)