This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathpayment-instructions.json.spt
99 lines (84 loc) · 3.02 KB
/
payment-instructions.json.spt
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
""" Get or change authenticated user's payment instructions.
"""
from aspen import Response
from gratipay.models.team import Team
from gratipay.utils import get_participant
[-----------------------------------------------------------------------------]
def format_payment_instruction(p):
return {
"team_name": p.team_name,
"team_slug": p.team_slug,
"amount": str(p.amount),
"due": str(p.due),
"ctime": p.ctime,
"mtime": p.mtime
}
def format_error(team_slug, error):
return {
"team_slug": team_slug,
"error": error
}
participant = get_participant(state, restrict=True)
if request.method == 'GET':
slug = request.qs.get("team_slug", "")
if slug:
# Make sure queried team exists.
team = Team.from_slug(slug)
if not team:
raise Response(400, _("Invalid team slug."))
pi, totals = participant.get_giving_for_profile()
out = {}
for p in pi:
if p.team_slug == slug:
out = format_payment_instruction(p)
break
if not out:
# Payment instruction for this team not found.
# Return default.
out = {
"team_name": team.name,
"team_slug": team.slug,
"amount": "0.00",
"due": "0.00",
"ctime": None,
"mtime": None
}
else:
pi, totals = participant.get_giving_for_profile()
out = [format_payment_instruction(p) for p in pi]
elif request.method == 'POST':
out = []
new_payment_instructions = request.body
for pi in new_payment_instructions:
if 'team_slug' not in pi:
one = format_error(None, "No team slug.")
elif 'amount' not in pi:
one = format_error(pi['team_slug'], "No amount.")
else:
team = Team.from_slug(pi['team_slug'])
if team and team.is_approved and not team.is_closed:
try:
created_pi = participant.set_payment_instruction(
team, parse_decimal(pi['amount'])
)
except Exception, exc:
one = format_error(team.slug, exc.__class__.__name__)
else:
# Payment instruction successfully created.
# Create response.
one = {
"team_name": team.name,
"team_slug": team.slug,
"ctime": created_pi['ctime'],
"mtime": created_pi['mtime'],
"amount": str(created_pi['amount']),
"due": str(created_pi['due'])
}
else:
one = format_error(pi['team_slug'],"Invalid or inactive team.")
out.append(one)
else:
# Only allow GET, POST.
raise Response(405, "", {"Allow": "GET, POST"})
[---] application/json
out