Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option for a recipient to pause donations #2297

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions emails/donations_paused.spt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ _("Liberapay donation paused") }}

[---] text/html
<p>{{ _("{0} has temporarily paused your donations to them.", recipient) }}</p>
4 changes: 4 additions & 0 deletions emails/donations_resumed.spt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ _("Liberapay donation resumed") }}

[---] text/html
<p>{{ _("{0} has resumed your donations to them.", recipient) }}</p>
15 changes: 14 additions & 1 deletion liberapay/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -2235,16 +2235,19 @@ def update_goal(self, goal, cursor=None):
raise UnexpectedCurrency(goal, self.main_currency)
with self.db.get_cursor(cursor) as c:
json = None if goal is None else str(goal)
donations_paused = goal == -2 and self.goal != -1
donations_resumed = goal != -1 and self.goal == -2
self.add_event(c, 'set_goal', json)
c.run("UPDATE participants SET goal=%s WHERE id=%s", (goal, self.id))
self.set_attributes(goal=goal)
if not self.accepts_tips:
if not self.accepts_tips or donations_resumed:
tippers = c.all("""
SELECT p
FROM current_tips t
JOIN participants p ON p.id = t.tipper
WHERE t.tippee = %s
""", (self.id,))
if not self.accepts_tips:
for tipper in tippers:
tipper.update_giving(cursor=c)
r = c.one("""
Expand All @@ -2255,6 +2258,16 @@ def update_goal(self, goal, cursor=None):
RETURNING receiving, npatrons
""", (self.id,))
self.set_attributes(**r._asdict())
if donations_paused or donations_resumed:
for tipper in tippers:
event = 'donations_paused' if donations_paused else 'donations_resumed'
tipper.send_email(
event,
tipper.get_email(tipper.get_email_address()),
recipient=self.username,
donations_paused=donations_paused,
donations_url=tipper.url('giving/')
)

def update_status(self, status, cursor=None):
with self.db.get_cursor(cursor) as c:
Expand Down
34 changes: 33 additions & 1 deletion tests/py/test_goal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from liberapay.testing import EUR, Harness

from liberapay.testing.emails import EmailHarness

class Tests(Harness):

Expand Down Expand Up @@ -70,3 +70,35 @@ def test_team_member_can_change_team_goal(self):
)
assert r.code == 302
assert team.refetch().goal == EUR('99.99')

class TestPauseDonations(EmailHarness):
def setUp(self):
EmailHarness.setUp(self)
self.alice = self.make_participant('alice', email='[email protected]')

def change_goal(self, goal, goal_custom="", auth_as="alice", expect_success=False):
r = self.client.PxST(
"/alice/edit/goal",
{'goal': goal, 'goal_custom': goal_custom},
auth_as=self.alice if auth_as == 'alice' else auth_as
)
if expect_success and r.code >= 400:
raise r
return r

def test_pause(self):
bob = self.make_participant('bob', email='[email protected]')
bob.set_tip_to(self.alice, EUR('0.99'), renewal_mode=2)
self.change_goal("-2", expect_success=True)

assert self.mailer.call_count == 1
last_email = self.get_last_email()
assert last_email['to'][0] == 'bob <[email protected]>'
assert "paused" in last_email['text']

self.change_goal("null", "", expect_success=True)

assert self.mailer.call_count == 2
last_email = self.get_last_email()
assert last_email['to'][0] == 'bob <[email protected]>'
assert "resume" in last_email['text']
21 changes: 15 additions & 6 deletions www/%username/edit/goal.spt
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@ if request.method == "POST":
goal = request.body["goal_custom"]
goal_currency = request.body.get_currency('currency', participant.main_currency)
goal = locale.parse_money_amount(goal, goal_currency)
if not (goal > 0 or participant.is_person and goal.amount in (0, -1)):
if not (goal > 0 or participant.is_person and goal.amount in (0, -1, -2)):
raise response.invalid_input(goal, 'goal_custom', 'body')
else:
goal = Money(request.body.get_int("goal", minimum=-1), participant.main_currency).round_down()
if not (goal > 0 or participant.is_person and goal.amount in (0, -1)):
goal = Money(request.body.get_int("goal", minimum=-2), participant.main_currency).round_down()
if not (goal > 0 or participant.is_person and goal.amount in (0, -1, -2)):
raise response.invalid_input(goal, 'goal', 'body')
participant.update_goal(goal)
if goal != participant.goal:
participant.update_goal(goal)
form_post_success(state)

if participant.kind == 'individual':
GRATEFUL = _("I'm grateful for gifts, but don't have a specific funding goal.")
GRATEFUL_PAUSED_GIFTS = _("I'm grateful for gifts, but they are currently paused.")
PATRON = _("I'm here as a patron.")
PATRON_NO_GIFTS = _("I'm here as a patron, and politely decline to receive gifts.")
GOAL_RAW = _("My goal is to receive {0}")
else:
GRATEFUL = _("We're grateful for gifts, but don't have a specific funding goal.")
GRATEFUL_PAUSED_GIFTS = _("We're grateful for gifts, but they are currently paused.")
PATRON = _("We're here as a patron.")
PATRON_NO_GIFTS = _("We're here as a patron, and politely decline to receive gifts.")
GOAL_RAW = _("Our goal is to receive {0}")
Expand Down Expand Up @@ -69,15 +72,21 @@ subhead = _("Goal")
</label><br>

% if participant.kind != 'group'
<label>
<input type="radio" name="goal" id="goal--2" value="-2"
{{ 'checked' if participant.goal == -2 else '' }} />
{{ GRATEFUL_PAUSED_GIFTS }}
</label><br>

<label>
<input type="radio" name="goal" id="goal-0" value="0"
{{ 'checked' if participant.goal == 0 else '' }} />
{{ PATRON }}
</label><br>

<label>
<input type="radio" name="goal" id="goal-negative" value="-1"
{{ 'checked' if participant.goal and participant.goal < 0 else '' }} />
<input type="radio" name="goal" id="goal--1" value="-1"
{{ 'checked' if participant.goal == -1 else '' }} />
{{ PATRON_NO_GIFTS }}
</label>
% endif
Expand Down
4 changes: 4 additions & 0 deletions www/%username/giving/index.html.spt
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ next_payday = compute_next_payday_date()
<p class="text-warning">{{ glyphicon('warning-sign') }} {{ _(
"Inactive because the account of the recipient is closed."
) }}</p>
% elif tippee.goal == -2
<p class="text-info">{{ glyphicon('pause') }} {{ _(
"This donation has been paused by the recipient."
) }}</p>
% elif not tippee.accepts_tips
<p class="text-warning">{{ glyphicon('warning-sign') }} {{ _(
"Inactive because the recipient no longer accepts donations."
Expand Down
Loading