From 31d458e067c6ef394fcf0dbeb825d715d4fd2ecd Mon Sep 17 00:00:00 2001 From: Will O'Beirne Date: Mon, 25 Feb 2019 14:26:43 -0500 Subject: [PATCH] Make contribution code handle not having a user in all cases. --- backend/grant/admin/example_emails.py | 2 ++ backend/grant/email/send.py | 2 +- backend/grant/proposal/views.py | 24 ++++++++++--------- backend/grant/task/jobs.py | 13 +++++----- .../emails/proposal_contribution.html | 4 ++++ .../emails/proposal_contribution.txt | 6 +++-- backend/grant/utils/stubs.py | 2 +- 7 files changed, 32 insertions(+), 21 deletions(-) diff --git a/backend/grant/admin/example_emails.py b/backend/grant/admin/example_emails.py index 5d08bf94..508a3072 100644 --- a/backend/grant/admin/example_emails.py +++ b/backend/grant/admin/example_emails.py @@ -80,9 +80,11 @@ example_email_args = { 'proposal': proposal, 'contribution': contribution, 'contributor': user, + # 'contributor': None, 'funded': '50', 'proposal_url': 'http://someproposal.com', 'contributor_url': 'http://someuser.com', + # 'contributor_url': None, }, 'proposal_comment': { 'author': user, diff --git a/backend/grant/email/send.py b/backend/grant/email/send.py index 6022a23d..1fc7fbf2 100644 --- a/backend/grant/email/send.py +++ b/backend/grant/email/send.py @@ -86,7 +86,7 @@ def proposal_contribution(email_args): 'subject': 'You just got a contribution!', 'title': 'You just got a contribution', 'preview': '{} just contributed {} to your proposal {}'.format( - email_args['contributor'].display_name, + email_args['contributor'].display_name if email_args['contributor'] else 'An anonymous contributor', email_args['contribution'].amount, email_args['proposal'].title, ), diff --git a/backend/grant/proposal/views.py b/backend/grant/proposal/views.py index 3d5af79b..9c2543db 100644 --- a/backend/grant/proposal/views.py +++ b/backend/grant/proposal/views.py @@ -375,11 +375,12 @@ def post_proposal_update(proposal_id, title, content): # Send email to all contributors (even if contribution failed) contributions = ProposalContribution.query.filter_by(proposal_id=proposal_id).all() for c in contributions: - send_email(c.user.email_address, 'contribution_update', { - 'proposal': g.current_proposal, - 'proposal_update': update, - 'update_url': make_url(f'/proposals/{proposal_id}?tab=updates&update={update.id}'), - }) + if c.user: + send_email(c.user.email_address, 'contribution_update', { + 'proposal': g.current_proposal, + 'proposal_update': update, + 'update_url': make_url(f'/proposals/{proposal_id}?tab=updates&update={update.id}'), + }) dumped_update = proposal_update_schema.dump(update) return dumped_update, 201 @@ -553,11 +554,12 @@ def post_contribution_confirmation(contribution_id, to, amount, txid): else: # Send to the user - send_email(contribution.user.email_address, 'contribution_confirmed', { - 'contribution': contribution, - 'proposal': contribution.proposal, - 'tx_explorer_url': f'{EXPLORER_URL}transactions/{txid}', - }) + if contribution.user: + send_email(contribution.user.email_address, 'contribution_confirmed', { + 'contribution': contribution, + 'proposal': contribution.proposal, + 'tx_explorer_url': f'{EXPLORER_URL}transactions/{txid}', + }) # Send to the full proposal gang for member in contribution.proposal.team: @@ -567,7 +569,7 @@ def post_contribution_confirmation(contribution_id, to, amount, txid): 'contributor': contribution.user, 'funded': contribution.proposal.funded, 'proposal_url': make_url(f'/proposals/{contribution.proposal.id}'), - 'contributor_url': make_url(f'/profile/{contribution.user.id}'), + 'contributor_url': make_url(f'/profile/{contribution.user.id}') if contribution.user else '', }) # TODO: Once we have a task queuer in place, queue emails to everyone diff --git a/backend/grant/task/jobs.py b/backend/grant/task/jobs.py index 7a5108f4..5d41c28a 100644 --- a/backend/grant/task/jobs.py +++ b/backend/grant/task/jobs.py @@ -78,12 +78,13 @@ class ProposalDeadline: 'proposal': proposal, }) for c in proposal.contributions: - send_email(c.user.email_address, 'contribution_proposal_failed', { - 'contribution': c, - 'proposal': proposal, - 'refund_address': c.user.settings.refund_address, - 'account_settings_url': make_url('/profile/settings?tab=account') - }) + if c.user: + send_email(c.user.email_address, 'contribution_proposal_failed', { + 'contribution': c, + 'proposal': proposal, + 'refund_address': c.user.settings.refund_address, + 'account_settings_url': make_url('/profile/settings?tab=account') + }) JOBS = { diff --git a/backend/grant/templates/emails/proposal_contribution.html b/backend/grant/templates/emails/proposal_contribution.html index f2f1cfde..b21232bf 100644 --- a/backend/grant/templates/emails/proposal_contribution.html +++ b/backend/grant/templates/emails/proposal_contribution.html @@ -1,7 +1,11 @@

Your proposal {{ args.proposal.title }} just got a {{ args.contribution.amount }} ZEC contribution from + {% if args.contributor %} {{ args.contributor.display_name }}. + {% else %} + an anonymous contributor. + {% endif %} Your proposal is now at {{ args.funded }} / {{ args.proposal.target }} ZEC.

diff --git a/backend/grant/templates/emails/proposal_contribution.txt b/backend/grant/templates/emails/proposal_contribution.txt index ddabad05..c73e428b 100644 --- a/backend/grant/templates/emails/proposal_contribution.txt +++ b/backend/grant/templates/emails/proposal_contribution.txt @@ -1,6 +1,8 @@ Your proposal "{{ args.proposal.title }}" just got a -{{ args.contribution.amount }} ZEC contribution from -{{ args.contributor.display_name }}. Your proposal is now at {{ args.funded }} / {{ args.proposal.target }} ZEC. +{{ args.contribution.amount }} ZEC contribution from {{ args.contributor.display_name if args.contributor else 'an anonymous contributor' }}. +Your proposal is now at {{ args.funded }} / {{ args.proposal.target }} ZEC. +{% if args.contributor %} See {{ args.contributor.display_name }}'s profile: {{ args.contributor_url }} +{% endif %} View your proposal: {{ args.proposal_url }} \ No newline at end of file diff --git a/backend/grant/utils/stubs.py b/backend/grant/utils/stubs.py index a5c34b1e..36794638 100644 --- a/backend/grant/utils/stubs.py +++ b/backend/grant/utils/stubs.py @@ -2,7 +2,7 @@ anonymous_user = { 'userid': 0, 'display_name': 'Anonymous', - 'title': 'Anonymous', + 'title': 'N/A', 'avatar': None, 'social_medias': [], 'email_verified': True,