forked from GamesDoneQuick/donation-tracker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.py
executable file
·1341 lines (1190 loc) · 57.7 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from functools import reduce
import django.contrib.admin.models
import django.forms as djforms
import requests
from django.contrib import admin
from django.contrib import messages
from django.contrib.admin import SimpleListFilter
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import permission_required, REDIRECT_FIELD_NAME
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied, ValidationError
from django.db import transaction
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render, redirect
from django.urls import reverse, path
from django.utils.html import format_html
from django.utils.safestring import mark_safe
import tracker.filters as filters
from django.conf import settings
import tracker.forms as forms
import tracker.horaro as horaro
import tracker.logutil as logutil
import tracker.models
import tracker.prizemail as prizemail
import tracker.prizeutil as prizeutil
import tracker.tiltify as tiltify
import tracker.views as views
import tracker.viewutil as viewutil
def admin_auth(perm=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url='admin:login'):
def impl_dec(viewFunc):
wrapFunc = viewFunc
if perm:
wrapFunc = permission_required(perm, raise_exception=True)(viewFunc)
return staff_member_required(wrapFunc, redirect_field_name=redirect_field_name, login_url=login_url)
return impl_dec
from datetime import *
import time
from ajax_select import make_ajax_field
from ajax_select.admin import AjaxSelectAdmin
def reverse_lazy(url):
return lambda: reverse(url)
def latest_event_id():
try:
return tracker.models.Event.objects.latest().id
except tracker.models.Event.DoesNotExist:
return 0
class CustomModelAdmin(AjaxSelectAdmin):
pass
class CustomStackedInline(admin.StackedInline):
# Adds an link that lets you edit an in-line linked object
def edit_link(self, instance):
if instance.id != None:
url = reverse('admin:{l}_{m}_change'.format(l=instance._meta.app_label,m=instance._meta.model_name), args=[instance.id])
return mark_safe('<a href="{u}">Edit</a>'.format(u=url))
else:
return mark_safe('Not Saved Yet')
def ReadOffsetTokenPair(value):
toks = value.split('-')
feed = toks[0]
params = {}
if len(toks) > 1:
params['delta'] = toks[1]
return feed, params
class DonationListFilter(SimpleListFilter):
title = 'feed'
parameter_name = 'feed'
def lookups(self, request, model_admin):
return (('toprocess', 'To Process'), ('toread', 'To Read'), ('recent-5', 'Last 5 Minutes'), ('recent-10','Last 10 Minutes'), ('recent-30','Last 30 Minutes'), ('recent-60','Last Hour'), ('recent-180','Last 3 Hours'),)
def queryset(self, request, queryset):
if self.value() is not None:
feed, params = ReadOffsetTokenPair(self.value())
return filters.apply_feed_filter(queryset, 'donation', feed, params, user=request.user, noslice=True)
else:
return queryset
class BidListFilter(SimpleListFilter):
title = 'feed'
parameter_name = 'feed'
def lookups(self, request, model_admin):
return (('current', 'Current'), ('future', 'Future'), ('open','Open'), ('closed', 'Closed'))
def queryset(self, request, queryset):
if self.value() is not None:
feed, params = ReadOffsetTokenPair(self.value())
return filters.apply_feed_filter(queryset, 'bid', feed, params, request.user, noslice=True)
else:
return queryset
class BidParentFilter(SimpleListFilter):
title = 'top level'
parameter_name = 'toplevel'
def lookups(self, request, model_admin):
return ((1, 'Yes'), (0, 'No'))
def queryset(self, request, queryset):
try:
queryset = queryset.filter(parent__isnull=True if int(self.value()) == 1 else False)
except TypeError as ValueError: # self.value cannot be converted to int for whatever reason
pass
return queryset
class BidSuggestionListFilter(SimpleListFilter):
title = 'feed'
parameter_name = 'feed'
def lookups(self, request, model_admin):
return (('expired', 'Expired'),)
def queryset(self, request, queryset):
if self.value() is not None:
feed, params = ReadOffsetTokenPair(self.value())
return filters.apply_feed_filter(queryset, 'bidsuggestion', feed, params, request.user, noslice=True)
else:
return queryset
class RunListFilter(SimpleListFilter):
title = 'feed'
parameter_name = 'feed'
def lookups(self, request, model_admin):
return (('current','Current'), ('future', 'Future'), ('recent-60', 'Last Hour'), ('recent-180', 'Last 3 Hours'), ('recent-300', 'Last 5 Hours'), ('future-60', 'Next Hour'), ('future-180', 'Next 3 Hours'), ('future-300', 'Next 5 Hours'))
def queryset(self, request, queryset):
if self.value() is not None:
feed, params = ReadOffsetTokenPair(self.value())
return filters.apply_feed_filter(queryset, 'run', feed, params, user=request.user, noslice=True)
else:
return queryset
class PrizeListFilter(SimpleListFilter):
title = 'feed'
parameter_name = 'feed'
def lookups(self, request, model_admin):
return (('unwon', 'Not Drawn'), ('won', 'Drawn'), ('current', 'Current'), ('future', 'Future'), ('todraw', 'Ready To Draw'))
def queryset(self, request, queryset):
if self.value() is not None:
feed, params = ReadOffsetTokenPair(self.value())
return filters.apply_feed_filter(queryset, 'prize', feed, params, request.user, noslice=True)
else:
return queryset
class PrizeWinnerListFilter(SimpleListFilter):
title = "Accept Status"
parameter_name = 'accept_status'
def lookups(self, request, model_admin):
return (
('pending', 'Pending'),
('accepted', 'Accepted'),
('declined', 'Declined'),
)
def queryset(self, request, queryset):
if self.value() == 'pending':
return queryset.filter(pendingcount__gt=0)
elif self.value() == 'accepted':
return queryset.filter(acceptcount__gt=0)
elif self.value() == 'declined':
return queryset.filter(declinecount__gt=0)
def bid_open_action(modeladmin, request, queryset):
bid_set_state_action(modeladmin, request, queryset, 'OPENED')
bid_open_action.short_description = "Set Bids as OPENED"
def bid_close_action(modeladmin, request, queryset):
bid_set_state_action(modeladmin, request, queryset, 'CLOSED')
bid_close_action.short_description = "Set Bids as CLOSED"
def bid_hidden_action(modeladmin, request, queryset):
bid_set_state_action(modeladmin, request, queryset, 'HIDDEN')
bid_hidden_action.short_description = "Set Bids as HIDDEN"
def bid_set_state_action(modeladmin, request, queryset, value, recursive=False):
if not request.user.has_perm('tracker.can_edit_locked_event'):
unchanged = queryset.filter(event__locked=True)
if unchanged.exists():
messages.warning(request, '%d bid(s) unchanged due to the event being locked.' % unchanged.count())
queryset = queryset.filter(event__locked=False)
if not recursive:
unchanged = queryset.filter(parent__isnull=False)
if unchanged.exists():
messages.warning(request, '%d bid(s) possibly unchanged because you can only use the dropdown on top level bids.' % unchanged.count())
queryset = queryset.filter(parent__isnull=True)
total = queryset.count()
for b in queryset:
b.state = value
b.clean()
b.save() # can't use queryset.update because that doesn't send the post_save signals
logutil.change(request, b, ['state'])
if total and not recursive:
messages.success(request, '%d bid(s) changed to %s.' % (total,value))
return total
class CountryRegionForm(djforms.ModelForm):
country = make_ajax_field(tracker.models.CountryRegion, 'country', 'country')
class Meta:
model = tracker.models.CountryRegion
exclude = ('', '')
class CountryRegionAdmin(CustomModelAdmin):
form = CountryRegionForm
list_display = ('name', 'country',)
list_display_links = ('country', )
search_fields = ('name', 'country__name', )
list_filter = ('country', )
fieldsets = [
(None, { 'fields': ['name', 'country'], }),
]
class BidForm(djforms.ModelForm):
speedrun = make_ajax_field(tracker.models.Bid, 'speedrun', 'run')
event = make_ajax_field(tracker.models.Bid, 'event', 'event', initial=latest_event_id)
biddependency = make_ajax_field(tracker.models.Bid, 'biddependency', 'allbids')
class BidInline(CustomStackedInline):
model = tracker.models.Bid
fieldsets = [(None, {
'fields': ['name', 'description', 'shortdescription', 'istarget', 'goal', 'state', 'total', 'edit_link'],
},)]
extra = 0
readonly_fields = ('total','edit_link',)
ordering = ('-total', 'name')
class BidOptionInline(BidInline):
verbose_name_plural = 'Options'
verbose_name = 'Option'
fk_name = 'parent'
class BidDependentsInline(BidInline):
verbose_name_plural = 'Dependent Bids'
verbose_name = 'Dependent Bid'
fk_name = 'biddependency'
class BidAdmin(CustomModelAdmin):
form = BidForm
list_display = ('name', 'parentlong', 'istarget', 'goal', 'total', 'description', 'state', 'biddependency')
list_editable = ('state',)
list_display_links = ('parentlong', 'biddependency')
list_select_related = ('event', 'speedrun', 'parent')
search_fields = ('name', 'speedrun__name', 'description', 'shortdescription', 'parent__name')
list_filter = ('speedrun__event', 'state', 'istarget', BidParentFilter, BidListFilter)
readonly_fields = ('parent', 'parent_', 'total')
fieldsets = [
(None, { 'fields': ['name', 'state', 'description', 'shortdescription', 'goal', 'istarget', 'allowuseroptions', 'revealedtime', 'total'] }),
('Link Info', { 'fields': ['event', 'speedrun', 'parent_', 'biddependency'] }),
]
inlines = [BidOptionInline, BidDependentsInline]
def parentlong(self, obj):
return str(obj.parent or obj.speedrun or obj.event)
def parent_(self, obj):
targetObject = None
if obj.parent:
targetObject = obj.parent
elif obj.speedrun:
targetObject = obj.speedrun
elif obj.event:
targetObject = obj.event
if targetObject:
return mark_safe('<a href={0}>{1}</a>'.format(str(viewutil.admin_url(targetObject)), targetObject))
else:
return '<None>'
parentlong.short_description = 'Parent'
def get_queryset(self, request):
event = viewutil.get_selected_event(request)
params = {}
if event:
params['event'] = event.id
if not request.user.has_perm('tracker.can_edit_locked_events'):
params['locked'] = False
return filters.run_model_query('allbids', params, user=request.user, mode='admin')
def has_add_permission(self, request):
return request.user.has_perm('tracker.top_level_bid')
def has_change_permission(self, request, obj=None):
return super(BidAdmin, self).has_change_permission(request, obj) and \
(obj == None or request.user.has_perm('tracker.can_edit_locked_events') or not obj.event.locked)
def has_delete_permission(self, request, obj=None):
return super(BidAdmin, self).has_delete_permission(request, obj) and \
(obj == None or ((request.user.has_perm('tracker.can_edit_locked_events') or not obj.event.locked) and
(request.user.has_perm('tracker.delete_all_bids') or not obj.total)))
def merge_bids(self, request, queryset):
bids = queryset
for bid in bids:
if not bid.istarget:
self.message_user(request, "All merged bids must be target bids.", level=messages.ERROR)
return HttpResponseRedirect(reverse('admin:tracker_bid_changelist'))
bidIds = [str(o.id) for o in bids]
return HttpResponseRedirect(settings.SITE_PREFIX + 'admin/merge_bids?objects=' + ','.join(bidIds))
merge_bids.short_description = "Merge selected bids"
actions = [bid_open_action, bid_close_action, bid_hidden_action,merge_bids]
def get_actions(self, request):
actions = super(BidAdmin, self).get_actions(request)
if not request.user.has_perm('tracker.delete_all_bids') and 'delete_selected' in actions:
del actions['delete_selected']
return actions
@admin_auth('tracker.change_bid')
def merge_bids_view(request, *args, **kwargs):
if request.method == 'POST':
objects = [int(x) for x in request.POST['objects'].split(',')]
form = forms.MergeObjectsForm(model=tracker.models.Bid,objects=objects, data=request.POST)
if form.is_valid():
viewutil.merge_bids(form.cleaned_data['root'], form.cleaned_data['objects'])
logutil.change(request, form.cleaned_data['root'], 'Merged bid {0} with {1}'.format(form.cleaned_data['root'], ','.join([str(d) for d in form.cleaned_data['objects']])))
return HttpResponseRedirect(reverse('admin:tracker_bid_changelist'))
else:
objects = [int(x) for x in request.GET['objects'].split(',')]
form = forms.MergeObjectsForm(model=tracker.models.Bid,objects=objects)
return render(request, 'admin/merge_bids.html', context={'form': form})
class BidSuggestionForm(djforms.ModelForm):
bid = make_ajax_field(tracker.models.BidSuggestion, 'bid', 'bidtarget')
class BidSuggestionAdmin(CustomModelAdmin):
form = BidSuggestionForm
list_display = ('name', 'bid')
list_select_related = ('bid',)
search_fields = ('name', 'bid__name', 'bid__description')
list_filter = ('bid__state', 'bid__speedrun__event', 'bid__event', BidSuggestionListFilter)
def get_queryset(self, request):
event = viewutil.get_selected_event(request)
params = {}
if not request.user.has_perm('tracker.can_edit_locked_events'):
params['locked'] = False
if event:
params['event'] = event.id
return filters.run_model_query('bidsuggestion', params, user=request.user, mode='admin')
class DonationBidForm(djforms.ModelForm):
bid = make_ajax_field(tracker.models.DonationBid, 'bid', 'bidtarget')
donation = make_ajax_field(tracker.models.DonationBid, 'donation', 'donation')
class DonationBidInline(CustomStackedInline):
form = DonationBidForm
model = tracker.models.DonationBid
extra = 0
max_num=100
readonly_fields = ('edit_link',)
class DonationBidAdmin(CustomModelAdmin):
form = DonationBidForm
list_display = ('bid', 'donation', 'amount')
list_select_related = ('bid', 'donation')
def get_queryset(self, request):
event = viewutil.get_selected_event(request)
params = {}
if not request.user.has_perm('tracker.can_edit_locked_events'):
params['locked'] = False
if event:
params['event'] = event.id
return filters.run_model_query('donationbid', params, user=request.user, mode='admin')
class DonationForm(djforms.ModelForm):
donor = make_ajax_field(tracker.models.Donation, 'donor', 'donor')
event = make_ajax_field(tracker.models.Donation, 'event', 'event', initial=latest_event_id)
class Meta:
model = tracker.models.Donation
exclude = ('', '')
class DonationInline(CustomStackedInline):
form = DonationForm
model = tracker.models.Donation
extra = 0
readonly_fields = ('edit_link',)
def mass_assign_action(self, request, queryset, field, value):
queryset.update(**{ field: value })
self.message_user(request, "Updated %s to %s" % (field, value))
class PrizeTicketInline(CustomStackedInline):
model = tracker.models.PrizeTicket
fk_name = 'donation'
extra = 0
readonly_fields = ('edit_link',)
class DonationAdmin(CustomModelAdmin):
form = DonationForm
list_display = ('id', 'visible_donor_name', 'amount', 'comment', 'commentlanguage', 'timereceived', 'event', 'domain', 'transactionstate', 'bidstate', 'readstate', 'commentstate',)
list_editable = ('transactionstate', 'bidstate', 'readstate', 'commentstate')
search_fields_base = ('donor__alias', 'amount', 'comment', 'modcomment')
list_filter = ('event', 'transactionstate', 'readstate', 'commentstate', 'bidstate', 'commentlanguage', DonationListFilter)
readonly_fields = ['domainId']
inlines = (DonationBidInline,PrizeTicketInline)
fieldsets = [
(None, {'fields': ('donor', 'event', 'timereceived')}),
('Comment State', {'fields': ('comment', 'modcomment')}),
('Donation State', {'fields': (('transactionstate', 'bidstate', 'readstate', 'commentstate'),)}),
('Financial', {'fields': (('amount', 'fee', 'currency', 'testdonation'),)}),
('Extra Donor Info', {'fields': (('requestedvisibility', 'requestedalias', 'requestedemail','requestedsolicitemail'),)}),
('Other', {'fields': (('domain', 'domainId'),)}),
]
def visible_donor_name(self, obj):
if obj.donor:
return obj.donor.visible_name()
else:
return None
def set_readstate_ready(self, request, queryset):
mass_assign_action(self, request, queryset, 'readstate', 'READY')
set_readstate_ready.short_description = 'Set Read state to ready to read.'
def set_readstate_ignored(self, request, queryset):
mass_assign_action(self, request, queryset, 'readstate', 'IGNORED')
set_readstate_ignored.short_description = 'Set Read state to ignored.'
def set_readstate_read(self, request, queryset):
mass_assign_action(self, request, queryset, 'readstate', 'READ')
set_readstate_read.short_description = 'Set Read state to read.'
def set_commentstate_approved(self, request, queryset):
mass_assign_action(self, request, queryset, 'commentstate', 'APPROVED')
set_commentstate_approved.short_description = 'Set Comment state to approved.'
def set_commentstate_denied(self, request, queryset):
mass_assign_action(self, request, queryset, 'commentstate', 'DENIED')
set_commentstate_denied.short_description = 'Set Comment state to denied.'
def cleanup_orphaned_donations(self, request, queryset):
count = 0
for donation in queryset.filter(donor=None, domain='PAYPAL', transactionstate='PENDING', timereceived__lte=datetime.utcnow() - timedelta(hours=8)):
for bid in donation.bids.all():
bid.delete()
for ticket in donation.tickets.all():
ticket.delete()
donation.delete()
count += 1
self.message_user(request, "Deleted %d donations." % count)
viewutil.tracker_log('donation', 'Deleted {0} orphaned donations'.format(count), user=request.user)
cleanup_orphaned_donations.short_description = 'Clear out incomplete donations.'
def get_list_display(self, request):
ret = list(self.list_display)
if not request.user.has_perm('tracker.delete_all_donations'):
ret.remove('transactionstate')
return ret
def get_readonly_fields(self, request, obj=None):
perm = request.user.has_perm('tracker.delete_all_donations')
ret = list(self.readonly_fields)
if not perm:
ret.append('domain')
ret.append('fee')
ret.append('transactionstate')
ret.append('testdonation')
if obj and obj.domain != 'LOCAL':
ret.append('donor')
ret.append('event')
ret.append('timereceived')
ret.append('amount')
ret.append('currency')
return ret
def has_change_permission(self, request, obj=None):
return super(DonationAdmin, self).has_change_permission(request, obj) and \
(obj == None or request.user.has_perm('tracker.can_edit_locked_events') or not obj.event.locked)
def has_delete_permission(self, request, obj=None):
return super(DonationAdmin, self).has_delete_permission(request, obj) and \
(obj == None or obj.domain == 'LOCAL' or request.user.has_perm('tracker.delete_all_donations'))
def get_search_fields(self, request):
search_fields = list(self.search_fields_base)
if request.user.has_perm('tracker.view_emails'):
search_fields += ['donor__email', 'donor__paypalemail']
if request.user.has_perm('tracker.view_usernames'):
search_fields += ['donor__firstname', 'donor__lastname']
return search_fields
def get_queryset(self, request):
event = viewutil.get_selected_event(request)
params = {}
if not request.user.has_perm('tracker.can_edit_locked_events'):
params['locked'] = False
if event:
params['event'] = event.id
return filters.run_model_query('donation', params, user=request.user, mode='admin')
actions = [set_readstate_ready, set_readstate_ignored, set_readstate_read, set_commentstate_approved, set_commentstate_denied, cleanup_orphaned_donations]
def get_actions(self, request):
actions = super(DonationAdmin, self).get_actions(request)
if not request.user.has_perm('tracker.delete_all_donations') and 'delete_selected' in actions:
del actions['delete_selected']
return actions
class PrizeWinnerForm(djforms.ModelForm):
winner = make_ajax_field(tracker.models.PrizeWinner, 'winner', 'donor')
prize = make_ajax_field(tracker.models.PrizeWinner, 'prize', 'prize')
class Meta:
model = tracker.models.PrizeWinner
exclude = ('','')
class PrizeWinnerInline(CustomStackedInline):
form = PrizeWinnerForm
model = tracker.models.PrizeWinner
readonly_fields = ['winner_email', 'edit_link']
def winner_email(self, obj):
return obj.winner.email
extra = 0
class PrizeWinnerAdmin(CustomModelAdmin):
form = PrizeWinnerForm
search_fields = ['prize__name', 'winner__email']
list_display = ['__str__', 'prize', 'prize_event', 'winner', 'winner_email', 'pendingcount', 'acceptcount', 'declinecount',
'shippingstate', 'prize_requiresshipping', 'accept_url']
list_editable = ('pendingcount', 'acceptcount', 'declinecount', 'shippingstate')
list_filter = ['prize__event', 'shippingstate', 'prize__requiresshipping', PrizeWinnerListFilter]
list_select_related = ('prize', 'prize__event', 'prize__startrun', 'winner')
readonly_fields = ['winner_email', 'accept_url', 'winner_address']
fieldsets = [
(None, { 'fields': ['prize', 'winner', 'winner_email', 'emailsent', 'pendingcount', 'acceptcount', 'declinecount', 'acceptdeadline', 'accept_url'], }),
('Shipping Info', { 'fields': ['acceptemailsentcount', 'shippingstate', 'winner_address', 'winnernotes', 'shippingemailsent',
'trackingnumber', 'shippingcost', 'shipping_receipt_url'] })
]
def winner_email(self, obj):
return obj.winner.email
def prize_event(self, obj):
return obj.prize.event
prize_event.short_description = "Event"
def prize_requiresshipping(self, obj):
return obj.prize.requiresshipping
prize_requiresshipping.short_description = 'Requires Postal Shipping'
prize_requiresshipping.boolean = True
def accept_url(self, obj):
"""
:type obj: tracker.models.PrizeWinner
"""
# Only show link if the prize winner is existing and pending acceptance.
if obj.pk and obj.pendingcount > 0:
return format_html("""<a href="{}">Link</a>""".format(obj.make_winner_url()))
else:
return format_html("")
accept_url.short_description = "Prize Acceptance Form"
def winner_address(self, obj):
"""
:type obj: tracker.models.PrizeWinner
"""
if obj.prize.requiresshipping:
addr_str = ""
for field in (
'addressstreet',
'addresscity',
'addressstate',
'addresscountry',
'addresszip',
):
val = getattr(obj.winner, field)
if val:
addr_str += "{}<br>".format(val)
return format_html("""{}<br><a href="{}">Edit Address</a>""".format(
addr_str, reverse('admin:{}_{}_change'.format(
obj.winner._meta.app_label, obj.winner._meta.model_name), args=[obj.winner.id])))
else:
return format_html("N/A")
winner_address.short_description = "Winner Shipping Address"
def get_queryset(self, request):
event = viewutil.get_selected_event(request)
params = {}
if not request.user.has_perm('tracker.can_edit_locked_events'):
params['locked'] = False
if event:
params['event'] = event.id
return filters.run_model_query('prizewinner', params, user=request.user, mode='admin')
def announce_winners(self, request, queryset):
for prize_winner in queryset:
try:
prize_winner.announce_to_chat()
except ValidationError as e:
self.message_user(request, "Couldn't announce {}, winner {} - {}".format(
prize_winner.prize.name, prize_winner.winner.alias, e.message), level=messages.ERROR)
else:
self.message_user(request, "Announced prize {}, winner {}".format(
prize_winner.prize.name, prize_winner.winner.alias), level=messages.SUCCESS)
announce_winners.short_description = "Announce selected winners to chat"
actions = [announce_winners]
class DonorPrizeEntryForm(djforms.ModelForm):
donor = make_ajax_field(tracker.models.DonorPrizeEntry, 'donor', 'donor')
prize = make_ajax_field(tracker.models.DonorPrizeEntry, 'prize', 'prize')
class Meta:
model = tracker.models.DonorPrizeEntry
exclude = ('', '')
class DonorPrizeEntryAdmin(CustomModelAdmin):
form = DonorPrizeEntryForm
model = tracker.models.DonorPrizeEntry
search_fields = ['prize__name', 'donor__email', 'donor__alias', 'donor__firstname', 'donor__lastname']
list_display = ['prize', 'donor', 'weight']
list_filter = ['prize__event', 'prize', 'donor']
list_select_related = ('prize', 'prize__event', 'prize__startrun', 'donor')
fieldsets = [
(None, {'fields': ['donor', 'prize', 'weight']}),
]
def get_queryset(self, request):
event = viewutil.get_selected_event(request)
params = {}
if not request.user.has_perm('tracker.can_edit_locked_events'):
params['locked'] = False
if event:
params['event'] = event.id
return filters.run_model_query('prizeentry', params, user=request.user, mode='admin')
class DonorPrizeEntryInline(CustomStackedInline):
form = DonorPrizeEntryForm
model = tracker.models.DonorPrizeEntry
readonly_fields = ['edit_link']
extra = 0
class DonorForm(djforms.ModelForm):
addresscountry = make_ajax_field(tracker.models.Donor, 'addresscountry', 'country')
user = make_ajax_field(tracker.models.Donor, 'user', 'user')
class Meta:
model = tracker.models.Donor
exclude = ('', '')
class DonorAdmin(CustomModelAdmin):
form = DonorForm
search_fields = ('email', 'paypalemail', 'alias', 'firstname', 'lastname')
list_filter = ('donation__event', 'visibility')
readonly_fields = ('visible_name',)
list_display = ('__str__', 'visible_name', 'alias', 'visibility')
fieldsets = [
(None, { 'fields': ['email', 'alias', 'firstname', 'lastname', 'visibility', 'visible_name', 'user', 'solicitemail'] }),
('Donor Info', {
'classes': ['collapse'],
'fields': ['paypalemail']
}),
('Address Info', {
'classes': ['collapse'],
'fields': ['addressstreet', 'addresscity', 'addressstate', 'addresscountry','addresszip']
}),
]
inlines = [DonationInline, PrizeWinnerInline, DonorPrizeEntryInline,]
def visible_name(self, obj):
return obj.visible_name()
def merge_donors(self, request, queryset):
donors = queryset
donorIds = [str(o.id) for o in donors]
return HttpResponseRedirect(settings.SITE_PREFIX + 'admin/merge_donors?objects=' + ','.join(donorIds))
merge_donors.short_description = "Merge selected donors"
actions = [merge_donors]
@admin_auth('tracker.change_donor')
def merge_donors_view(request, *args, **kwargs):
if request.method == 'POST':
objects = [int(x) for x in request.POST['objects'].split(',')]
form = forms.MergeObjectsForm(model=tracker.models.Donor,objects=objects, data=request.POST)
if form.is_valid():
viewutil.merge_donors(form.cleaned_data['root'], form.cleaned_data['objects'])
logutil.change(request, form.cleaned_data['root'], 'Merged donor {0} with {1}'.format(form.cleaned_data['root'], ','.join([str(d) for d in form.cleaned_data['objects']])))
return HttpResponseRedirect(reverse('admin:tracker_donor_changelist'))
else:
donors = [int(x) for x in request.GET['objects'].split(',')]
form = forms.MergeObjectsForm(model=tracker.models.Donor,donors=donors)
return render(request, 'admin/merge_donors.html', context={'form': form})
def google_flow(request):
try:
flow = tracker.models.FlowModel.objects.get(id=request.user.id).flow
except tracker.models.FlowModel.DoesNotExist:
raise Http404
if 'error' in request.GET:
return HttpResponse('Either you or Google denied access', status=403)
credentials = tracker.models.CredentialsModel.objects.get_or_create(id=request.user)[0]
credentials.credentials = flow.step2_exchange(request.GET['code'])
credentials.clean()
credentials.save()
return HttpResponse('Credentials saved successfully, try your previous action again')
class EventForm(djforms.ModelForm):
allowed_prize_countries = make_ajax_field(tracker.models.Event, 'allowed_prize_countries', 'country')
disallowed_prize_regions = make_ajax_field(tracker.models.Event, 'disallowed_prize_regions', 'countryregion')
prizecoordinator = make_ajax_field(tracker.models.Event, 'prizecoordinator', 'user')
class Meta:
model = tracker.models.Event
exclude = ('', '')
class EventBidInline(BidInline):
def get_queryset(self, request):
qs = super(EventBidInline, self).get_queryset(request)
return qs.filter(speedrun=None)
class EventAdmin(CustomModelAdmin):
form = EventForm
search_fields = ('short', 'name')
inlines = [EventBidInline]
list_display = ['name', 'locked']
list_editable = ['locked']
readonly_fields = ['scheduleid', 'admin_horaro_check_cols']
fieldsets = [
(None, { 'fields': ['short', 'name', 'receivername', 'targetamount', 'minimumdonation', 'datetime', 'timezone', 'locked'] }),
('Paypal', {
'classes': ['collapse'],
'fields': ['paypalemail', 'usepaypalsandbox', 'paypalcurrency', ]
}),
('Donation Autoreply', {
'classes': ['collapse',],
'fields': ['donationemailsender', 'donationemailtemplate', 'pendingdonationemailtemplate',],
}),
('Prize Management', {
'classes': ['collapse',],
'fields': ['prize_accept_deadline_delta', 'prizecoordinator', 'allowed_prize_countries', 'disallowed_prize_regions', 'prizecontributoremailtemplate', 'prizewinneremailtemplate', 'prizewinneracceptemailtemplate', 'prizeshippedemailtemplate',],
}),
('Google Document', {
'classes': ['collapse'],
'fields': ['scheduleid']
}),
('Horaro Schedule', {
'classes': ('collapse',),
'fields': ('horaro_id', 'admin_horaro_check_cols', 'horaro_game_col', 'horaro_category_col',
'horaro_runners_col'),
}),
('Tiltify Donations', {
'classes': ('collapse',),
'fields': ('tiltify_enable_sync', 'tiltify_api_key'),
}),
('Twitch Chat Announcements', {
'classes': ('collapse',),
'fields': ('twitch_channel', 'twitch_login', 'twitch_oauth'),
}),
]
class Media:
js = (
'event_admin.js',
)
def merge_horaro_schedule(self, request, queryset):
"""Merge run schedule from Horaro API."""
if len(queryset) != 1:
self.message_user(request, "Please select only a single event for Horaro merge", level=messages.ERROR)
return
# Get content type for log entries.
ct = ContentType.objects.get_for_model(tracker.models.Event)
for event in queryset:
try:
with transaction.atomic():
num_runs = horaro.merge_event_schedule(event)
msg = 'Merged Horaro schedule for event {} - {} runs'.format(event, num_runs)
admin.models.LogEntry.objects.log_action(user_id=request.user.id, content_type_id=ct.pk, object_id=event.pk,
object_repr=str(event), action_flag=admin.models.CHANGE,
change_message=msg)
except horaro.HoraroError as e:
self.message_user(request, "Can't merge Horaro schedule - {}".format(e), level=messages.ERROR)
else:
self.message_user(request, "%d runs merged for %s." % (num_runs, event.name), level=messages.SUCCESS)
merge_horaro_schedule.short_description = "Merge Horaro schedule for a single event (do this once every 24 hours)"
def sync_tiltify_donations(self, request, queryset):
"""Sync donations from Tiltify API."""
if len(queryset) != 1:
self.message_user(request, "Please select only a single event for Tiltify sync", level=messages.ERROR)
return
# Get content type for log entries.
ct = ContentType.objects.get_for_model(tracker.models.Event)
# Sync donations from Tiltify API.
for event in queryset:
try:
with transaction.atomic():
num_donations = tiltify.sync_event_donations(event)
msg = 'Synced Tiltify donations for event {} - {} donations'.format(event, num_donations)
admin.models.LogEntry.objects.log_action(user_id=request.user.id, content_type_id=ct.pk, object_id=event.pk,
object_repr=str(event), action_flag=admin.models.CHANGE,
change_message=msg)
except (ValidationError, requests.exceptions.RequestException) as e:
self.message_user(request, "Can't sync Tiltify donations - {}".format(e), level=messages.ERROR)
else:
self.message_user(request, "%d donations synced for %s." % (num_donations, event.name), level=messages.SUCCESS)
sync_tiltify_donations.short_description = "Sync Tiltify donations for a single event"
actions = [merge_horaro_schedule, sync_tiltify_donations]
class PostbackURLForm(djforms.ModelForm):
event = make_ajax_field(tracker.models.PostbackURL, 'event', 'event', initial=latest_event_id)
class Meta:
model = tracker.models.PostbackURL
exclude = ('', '')
class PostbackURLAdmin(CustomModelAdmin):
form = PostbackURLForm
search_fields = ('url',)
list_filter = ('event',)
list_display = ('url', 'event')
fieldsets = [
(None, { 'fields': ['event', 'url'] })
]
def get_queryset(self, request):
event = viewutil.get_selected_event(request)
if event:
return tracker.models.PostbackURL.objects.filter(event=event)
else:
return tracker.models.PostbackURL.objects.all()
class PrizeForm(djforms.ModelForm):
event = make_ajax_field(tracker.models.Prize, 'event', 'event', initial=latest_event_id)
startrun = make_ajax_field(tracker.models.Prize, 'startrun', 'run')
endrun = make_ajax_field(tracker.models.Prize, 'endrun', 'run')
handler = make_ajax_field(tracker.models.Prize, 'handler', 'user')
allowed_prize_countries = make_ajax_field(tracker.models.Prize, 'allowed_prize_countries', 'country')
disallowed_prize_regions = make_ajax_field(tracker.models.Prize, 'disallowed_prize_regions', 'countryregion')
class Meta:
model = tracker.models.Prize
exclude = ('', '')
class PrizeInline(CustomStackedInline):
model = tracker.models.Prize
form = PrizeForm
fk_name = 'endrun'
extra = 0
fields = ['name', 'description', 'shortdescription', 'handler', 'image', 'altimage', 'event', 'state', 'allowed_prize_countries', 'disallowed_prize_regions', 'edit_link']
readonly_fields = ('edit_link',)
class PrizeAdmin(CustomModelAdmin):
form = PrizeForm
list_display = ('name', 'category', 'bidrange', 'games', 'start_draw_time', 'end_draw_time', 'sumdonations',
'randomdraw', 'event', 'state', 'winners_', 'provider', 'handler' )
list_editable = ('state',)
list_filter = ('event', 'category', 'state', 'requiresshipping', PrizeListFilter)
list_select_related = ('event', 'startrun', 'endrun')
fieldsets = [
(None, { 'fields': ['name', 'description', 'shortdescription', 'image', 'altimage', 'event', 'category', 'requiresshipping', 'handler' ] }),
('Contributor Information', {
'fields': ['provider', 'creator', 'creatoremail', 'creatorwebsite', 'extrainfo', 'estimatedvalue', 'acceptemailsent', 'state', 'reviewnotes',] }),
('Drawing Parameters', {
'classes': ['collapse'],
'fields': ['maxwinners', 'maxmultiwin', 'minimumbid', 'maximumbid', 'sumdonations', 'randomdraw', 'ticketdraw',
'auto_tickets', 'startrun', 'endrun', 'starttime', 'endtime', 'custom_country_filter',
'allowed_prize_countries', 'disallowed_prize_regions']
}),
]
search_fields = ('name', 'description', 'shortdescription', 'provider', 'handler__username', 'handler__email', 'handler__last_name', 'handler__first_name', 'prizewinner__winner__firstname', 'prizewinner__winner__lastname', 'prizewinner__winner__alias', 'prizewinner__winner__email')
inlines = [PrizeWinnerInline]
def winners_(self, obj):
winners = obj.get_winners()
if len(winners) > 0:
return reduce(lambda x,y: x + " ; " + y, [str(x) for x in winners])
else:
return 'None'
def bidrange(self, obj):
s = str(obj.minimumbid)
if obj.minimumbid != obj.maximumbid:
if obj.maximumbid == None:
max = 'Infinite'
else:
max = str(obj.maximumbid)
s += ' <--> ' + max
return s
bidrange.short_description = 'Bid Range'
def games(self, obj):
if obj.startrun == None:
return ''
else:
s = str(obj.startrun.name_with_category())
if obj.startrun != obj.endrun:
s += ' <--> ' + str(obj.endrun.name_with_category())
def draw_prize_internal(self, request, queryset, limit):
numDrawn = 0
for prize in queryset:
if limit is None:
limit = prize.maxwinners
numToDraw = min(limit, prize.maxwinners - prize.current_win_count())
drawingError = False
thisDrawn = 0
while not drawingError and thisDrawn < numToDraw:
drawn, msg = prizeutil.draw_prize(prize)
time.sleep(1)
if not drawn:
self.message_user(request, msg, level=messages.ERROR)
drawingError = True
else:
numDrawn += 1
thisDrawn += 1
if numDrawn > 0:
self.message_user(request, "%d prizes drawn." % numDrawn)
def draw_prize_once_action(self, request, queryset):
self.draw_prize_internal(request, queryset, 1)
draw_prize_once_action.short_description = "Draw a SINGLE winner for the selected prizes"
def draw_prize_action(self, request, queryset):
self.draw_prize_internal(request, queryset, None)
draw_prize_action.short_description = "Draw (all) winner(s) for the selected prizes"
def set_state_accepted(self, request, queryset):
mass_assign_action(self, request, queryset, 'state', 'ACCEPTED')
set_state_accepted.short_description = "Set state to Accepted"
def set_state_pending(self, request, queryset):
mass_assign_action(self, request, queryset, 'state', 'PENDING')
set_state_pending.short_description = "Set state to Pending"
def set_state_denied(self, request, queryset):
mass_assign_action(self, request, queryset, 'state', 'DENIED')
set_state_denied.short_description = "Set state to Denied"
actions = [draw_prize_action, draw_prize_once_action, set_state_accepted, set_state_pending, set_state_denied]
def get_queryset(self, request):
event = viewutil.get_selected_event(request)
params = {}
if not request.user.has_perm('tracker.can_edit_locked_events'):
params['locked'] = False
if event:
params['event'] = event.id
return filters.run_model_query('prize', params, user=request.user, mode='admin')
class PrizeTicketForm(djforms.ModelForm):
prize = make_ajax_field(tracker.models.PrizeTicket, 'prize', 'prize')
donation = make_ajax_field(tracker.models.PrizeTicket, 'donation', 'donation')
class Meta:
model = tracker.models.PrizeTicket
exclude = ('', '')
class PrizeTicketAdmin(CustomModelAdmin):
form = PrizeTicketForm
list_display = ('prize', 'donation', 'amount')
list_select_related = ('prize', 'donation')
def get_queryset(self, request):
event = viewutil.get_selected_event(request)
params = {}
if not request.user.has_perm('tracker.can_edit_locked_events'):
params['locked'] = False
if event:
params['event'] = event.id
return filters.run_model_query('prizeticket', params, user=request.user, mode='admin')
class RunnerAdminForm(djforms.ModelForm):
donor = make_ajax_field(tracker.models.Runner, 'donor', 'donor')
class Meta:
model = tracker.models.Runner
exclude = ('', '')
class RunnerAdmin(CustomModelAdmin):
form = RunnerAdminForm
search_fields = ['name', 'stream', 'twitter', 'youtube', 'donor__alias', 'donor__firstname', 'donor__lastname', 'donor__email',]
list_display = ('name', 'stream', 'twitter', 'youtube', 'donor',)
list_select_related = ('donor',)
fieldsets = [(None, { 'fields': ('name', 'stream', 'twitter', 'youtube', 'donor',) }),]
class SpeedRunAdminForm(djforms.ModelForm):
event = make_ajax_field(tracker.models.SpeedRun, 'event', 'event', initial=latest_event_id)
runners = make_ajax_field(tracker.models.SpeedRun, 'runners', 'runner')
class Meta:
model = tracker.models.SpeedRun
exclude = ('', '')
class StartRunForm(djforms.Form):
run_time = djforms.CharField(help_text='Run time of previous run')
start_time = djforms.DateTimeField(help_text='Start time of current run')
def start_run_view(request, run):
if not request.user.has_perm('tracker.change_speedrun'):
raise PermissionDenied
run = tracker.models.SpeedRun.objects.get(id=run)
prev = tracker.models.SpeedRun.objects.filter(event=run.event, order__lt=run.order).last()
form = StartRunForm(data=request.POST if request.method == 'POST' else None,initial={'run_time': prev.run_time, 'start_time': run.starttime})
if form.is_valid():
rt = tracker.models.event.TimestampField.time_string_to_int(form.cleaned_data['run_time'])
endtime = prev.starttime + timedelta(milliseconds=rt)
if form.cleaned_data['start_time'] < endtime:
return HttpResponse('Nope', status=400)
prev.run_time = form.cleaned_data['run_time']
prev.setup_time = str(form.cleaned_data['start_time'] - endtime)