forked from GamesDoneQuick/donation-tracker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheventutil.py
47 lines (39 loc) · 1.47 KB
/
eventutil.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
import json
import traceback
import urllib.request
from django.core import serializers
from django.db.models import Sum
import tracker.models as models
import tracker.search_filters as filters
import tracker.viewutil as viewutil
# TODO: this is 2018, we ought to be using requests
def post_donation_to_postbacks(donation):
event_donations = filters.run_model_query('donation', {'event': donation.event.id})
total = event_donations.aggregate(amount=Sum('amount'))['amount']
data = {
'id': donation.id,
'timereceived': str(donation.timereceived),
'comment': donation.comment,
'amount': donation.amount,
'donor__visibility': donation.donor.visibility,
'donor__visiblename': donation.donor.visible_name(),
'new_total': total,
'domain': donation.domain,
}
try:
data_json = json.dumps(
data, ensure_ascii=False, cls=serializers.json.DjangoJSONEncoder
).encode('utf-8')
postbacks = models.PostbackURL.objects.filter(event=donation.event)
for postback in postbacks:
opener = urllib.request.build_opener()
req = urllib.request.Request(
postback.url,
data_json,
headers={'Content-Type': 'application/json; charset=utf-8'},
)
opener.open(req, timeout=5)
except Exception:
viewutil.tracker_log(
'postback_url', traceback.format_exc(), event=donation.event
)