forked from GamesDoneQuick/donation-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewutil.py
262 lines (210 loc) · 8.71 KB
/
viewutil.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
import re
import operator
from decimal import Decimal
from django.db.models import Count, Sum, Max, Avg, Q, Func
from django.core.urlresolvers import reverse
from django.db.models.functions import Coalesce
from django.http import Http404
from django.contrib.auth import get_user_model
from django.db import transaction
from django.conf import settings
from .models import *
from . import filters
def get_default_email_host_user():
return getattr(settings, 'EMAIL_HOST_USER', '')
def get_default_email_from_user():
return getattr(settings, 'EMAIL_FROM_USER', get_default_email_host_user())
def admin_url(obj):
return reverse("admin:%s_%s_change" % (obj._meta.app_label, obj._meta.object_name.lower()), args=(obj.pk,), current_app=obj._meta.app_label)
# Adapted from http://djangosnippets.org/snippets/1474/
# TODO: use request.build_absolute_uri instead
def get_request_server_url(request):
if request:
return request.build_absolute_uri('/')
else:
raise Exception("Request was null.")
def get_referer_site(request):
origin = request.META.get('HTTP_ORIGIN', None)
if origin != None:
return re.sub(r'^https?:\/\/', '', origin)
else:
return None
def get_event(event):
if event:
if isinstance(event, Event):
return event
try:
if re.match(r'^\d+$', event):
return Event.objects.get(id=event)
else:
return Event.objects.get(short=event)
except Event.DoesNotExist:
raise Http404
e = Event()
e.id = None
e.name = 'All Events'
return e
def request_params(request):
if request.method == 'GET':
return request.GET
elif request.method == 'POST':
return request.POST
else:
raise Exception(
"No request parameters associated with this request method.")
_1ToManyBidsAggregateFilter = Q(bids__donation__transactionstate='COMPLETED')
_1ToManyDonationAggregateFilter = Q(donation__transactionstate='COMPLETED')
DonationBidAggregateFilter = _1ToManyDonationAggregateFilter
DonorAggregateFilter = _1ToManyDonationAggregateFilter
EventAggregateFilter = _1ToManyDonationAggregateFilter
PrizeWinnersFilter = Q(prizewinner__acceptcount_gt=0) | Q(
prizewinner__pendingcount__gt=0)
# http://stackoverflow.com/questions/5722767/django-mptt-get-descendants-for-a-list-of-nodes
def get_tree_queryset_descendants(model, nodes, include_self=False):
if not nodes:
return nodes
filters = []
for n in nodes:
lft, rght = n.lft, n.rght
if include_self:
lft -= 1
rght += 1
filters.append(Q(tree_id=n.tree_id, lft__gt=lft, rght__lt=rght))
q = reduce(operator.or_, filters)
return model.objects.filter(q).order_by(*model._meta.ordering)
# http://stackoverflow.com/questions/6471354/efficient-function-to-retrieve-a-queryset-of-ancestors-of-an-mptt-queryset
def get_tree_queryset_ancestors(model, nodes):
tree_list = {}
query = Q()
for node in nodes:
if node.tree_id not in tree_list:
tree_list[node.tree_id] = []
parent = node.parent.pk if node.parent is not None else None,
if parent not in tree_list[node.tree_id]:
tree_list[node.tree_id].append(parent)
query |= Q(lft__lt=node.lft, rght__gt=node.rght,
tree_id=node.tree_id)
return model.objects.filter(query).order_by(*model._meta.ordering)
def get_tree_queryset_all(model, nodes):
filters = []
for node in nodes:
filters.append(Q(tree_id=node.tree_id))
q = reduce(operator.or_, filters)
return model.objects.filter(q).order_by(*model._meta.ordering)
ModelAnnotations = {
'event': {
'amount': Coalesce(Sum('donation__amount', only=EventAggregateFilter), Decimal('0.00')),
'count': Count('donation', only=EventAggregateFilter),
'max': Coalesce(Max('donation__amount', only=EventAggregateFilter), Decimal('0.00')),
'avg': Coalesce(Avg('donation__amount', only=EventAggregateFilter), Decimal('0.00')),
},
'prize': {'numwinners': Count('prizewinner', only=PrizeWinnersFilter), },
}
def find_people(people_list):
result = []
for person in people_list:
try:
d = Donor.objects.get(alias__iequals=person)
result.append(d)
except:
pass
return result
def prizecmp(a, b):
# if both prizes are run-linked, sort them that way
if a.startrun and b.startrun:
return cmp(a.startrun.starttime, b.startrun.starttime) or cmp(a.endrun.endtime, b.endrun.endtime) or cmp(a.name, b.name)
# else if they're both time-linked, sort them that way
if a.starttime and b.starttime:
return cmp(a.starttime, b.starttime) or cmp(a.endtime, b.endtime) or cmp(a.name, b.name)
# run-linked prizes are listed after time-linked and non-linked
if a.startrun and not b.startrun:
return 1
if b.startrun and not a.startrun:
return -1
# time-linked prizes are listed after non-linked
if a.starttime and not b.starttime:
return 1
if b.starttime and not a.starttime:
return -1
# sort by category or name as a fallback
return cmp(a.category, b.category) or cmp(a.name, b.name)
EVENT_SELECT = 'admin-event'
def get_selected_event(request):
evId = request.session.get(EVENT_SELECT, None)
if evId:
return Event.objects.get(pk=evId)
else:
return None
def set_selected_event(request, event):
if event:
request.session[EVENT_SELECT] = event.id
else:
request.session[EVENT_SELECT] = None
def get_donation_prize_contribution(prize, donation, secondaryAmount=None):
if prize.contains_draw_time(donation.timereceived):
amount = secondaryAmount if secondaryAmount != None else donation.amount
if prize.sumdonations or amount >= prize.minimumbid:
return amount
return None
def get_donation_prize_info(donation):
""" Attempts to find a list of all prizes this donation gives the donor eligibility for.
Does _not_ attempt to relate this information to any _past_ eligibility.
Returns the set as a list of {'prize','amount'} dictionaries. """
prizeList = []
for ticket in donation.tickets.all():
contribAmount = get_donation_prize_contribution(
ticket.prize, donation, ticket.amount)
if contribAmount != None:
prizeList.append({'prize': ticket.prize, 'amount': contribAmount})
for timeprize in filters.run_model_query('prize', params={'feed': 'current', 'ticketdraw': False, 'offset': donation.timereceived, 'noslice': True}):
contribAmount = get_donation_prize_contribution(timeprize, donation)
if contribAmount != None:
prizeList.append({'prize': timeprize, 'amount': contribAmount})
return prizeList
def tracker_log(category, message='', event=None, user=None):
Log.objects.create(category=category, message=message,
event=event, user=user)
def merge_bids(rootBid, bids):
for bid in bids:
if bid != rootBid:
for donationBid in bid.bids.all():
donationBid.bid = rootBid
donationBid.save()
for suggestion in bid.suggestions.all():
suggestion.bid = rootBid
suggestion.save()
bid.delete()
rootBid.save()
return rootBid
def merge_donors(rootDonor, donors):
for other in donors:
if other != rootDonor:
for donation in other.donation_set.all():
donation.donor = rootDonor
donation.save()
for prizewin in other.prizewinner_set.all():
prizewin.winner = rootDonor
prizewin.save()
other.delete()
rootDonor.save()
return rootDonor
def autocreate_donor_user(donor):
AuthUser = get_user_model()
if not donor.user:
with transaction.atomic():
try:
linkUser = AuthUser.objects.get(email=donor.email)
except AuthUser.MultipleObjectsReturned:
message = 'Multiple users found for email {0}, when trying to mail donor {1} for prizes'.format(
donor.email, donor.id)
tracker_log('prize', message)
raise Exception(message)
except AuthUser.DoesNotExist:
targetUsername = donor.email
if donor.alias and not AuthUser.objects.filter(username=donor.alias):
targetUsername = donor.alias
linkUser = AuthUser.objects.create(
username=targetUsername, email=donor.email, first_name=donor.firstname, last_name=donor.lastname, is_active=False)
donor.user = linkUser
donor.save()
return donor.user