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

Send one mail instead of n mail to supporter on checkout of n grants #8258

Merged
merged 3 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 33 additions & 3 deletions app/grants/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
from celery.utils.log import get_task_logger
from dashboard.models import Profile
from grants.models import Grant, Subscription
from marketing.mails import new_grant, new_grant_admin, new_supporter, thank_you_for_supporting
from marketing.mails import (
new_grant, new_grant_admin, new_supporter, thank_you_for_supporting,
batch_thank_you_for_supporting
)
from marketing.models import Stat
from perftools.models import JSONStore
from townsquare.models import Comment
Expand Down Expand Up @@ -159,13 +162,14 @@ def update_grant_metadata(self, grant_id, retry: bool = True) -> None:


@app.shared_task(bind=True, max_retries=1)
def process_grant_contribution(self, grant_id, grant_slug, profile_id, package, retry: bool = True) -> None:
def process_grant_contribution(self, grant_id, grant_slug, profile_id, package, send_supporter_mail:bool = True, retry: bool = True):
"""
:param self:
:param grant_id:
:param grant_slug:
:param profile_id:
:param package:
:param send_supporter_mail:
:return:
"""
from grants.views import record_subscription_activity_helper
Expand Down Expand Up @@ -252,8 +256,34 @@ def process_grant_contribution(self, grant_id, grant_slug, profile_id, package,
new_supporter(grant, subscription)

# emails to contributor
thank_you_for_supporting(grant, subscription)
if send_supporter_mail:
thank_you_for_supporting(grant, subscription)

update_grant_metadata.delay(grant_id)
return grant, subscription


@app.shared_task(bind=True, max_retries=1)
def batch_process_grant_contributions(self, grants_with_payload, profile_id, retry: bool = True) -> None:
"""
:param self:
:param grants_with_payload: list of dicts with grant_id, grant_slug and payload
:param profile_id:
:return:
"""
grants_with_subscription = []
for grant_with_payload in grants_with_payload:
grant_id = grant_with_payload["grant_id"]
grant_slug = grant_with_payload["grant_slug"]
payload = grant_with_payload["payload"]
grant, subscription = process_grant_contribution(
grant_id, grant_slug, profile_id, payload, send_supporter_mail=False, retry=retry
)
grants_with_subscription.append({
"grant": grant,
"subscription": subscription
})
batch_thank_you_for_supporting(grants_with_subscription)


@app.shared_task(bind=True, max_retries=1)
Expand Down
16 changes: 10 additions & 6 deletions app/grants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2057,8 +2057,9 @@ def bulk_fund(request):
successes = []
failures = []

batch_grants_mail = []
profile = get_profile(request)
grants_with_payload = []

for (index, grant_id) in enumerate(grant_ids_list):
try:
grant = Grant.objects.get(pk=grant_id)
Expand Down Expand Up @@ -2103,7 +2104,6 @@ def bulk_fund(request):
)

try:
from grants.tasks import process_grant_contribution
payload = {
# Values that are constant for all donations
'checkout_type': request.POST.get('checkout_type'),
Expand Down Expand Up @@ -2139,7 +2139,11 @@ def bulk_fund(request):
'token_symbol': request.POST.get('token_symbol').split(',')[index],
'include_for_clr': json.loads(request.POST.get('include_for_clr', 'true'))
}
process_grant_contribution.delay(grant_id, grant.slug, profile.pk, payload)
grants_with_payload.append({
'grant_id': grant_id,
'grant_slug': grant.slug,
'payload': payload
})
except Exception as e:
failures.append({
'active': 'grant_error',
Expand All @@ -2153,13 +2157,13 @@ def bulk_fund(request):

successes.append({
'title': _('Fund - Grant Funding Processed Successfully'),
'grant':grant_id,
'grant': grant_id,
'text': _('Funding for this grant was successfully processed and saved.'),
'success': True
})
batch_grants_mail.append(grant_id)

# thank_you_for_supporting(batch_grants_mail, profile)
from grants.tasks import batch_process_grant_contributions
batch_process_grant_contributions.delay(grants_with_payload, profile.pk)

return JsonResponse({
'success': True,
Expand Down
24 changes: 23 additions & 1 deletion app/marketing/mails.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
render_subscription_terminated_email, render_successful_contribution_email, render_support_cancellation_email,
render_tax_report, render_thank_you_for_supporting_email, render_tip_email, render_tribe_hackathon_prizes,
render_unread_notification_email_weekly_roundup, render_wallpost, render_weekly_recap,
)
render_batch_thank_you_for_supporting_email)
from sendgrid.helpers.mail import Attachment, Content, Email, Mail, Personalization
from sendgrid.helpers.stats import Category
from townsquare.utils import is_email_townsquare_enabled, is_there_an_action_available
Expand Down Expand Up @@ -322,6 +322,28 @@ def thank_you_for_supporting(grant, subscription):
translation.activate(cur_language)


def batch_thank_you_for_supporting(grants_with_subscription):
positive_subscriptions = list(filter(lambda gws: not gws["subscription"].negative, grants_with_subscription))
if len(positive_subscriptions) == 0:
return

from_email = settings.CONTACT_EMAIL
to_email = positive_subscriptions[0]["subscription"].contributor_profile.email
if not to_email:
to_email = positive_subscriptions[0]["subscription"].contributor_profile.user.email

cur_language = translation.get_language()

try:
setup_lang(to_email)
html, text, subject = render_batch_thank_you_for_supporting_email(grants_with_subscription)

if not should_suppress_notification_email(to_email, 'thank_you_for_supporting'):
send_mail(from_email, to_email, subject, text, html, categories=['transactional', func_name()])
finally:
translation.activate(cur_language)


def support_cancellation(grant, subscription):
if subscription and subscription.negative:
return
Expand Down
8 changes: 8 additions & 0 deletions app/retail/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ def render_thank_you_for_supporting_email(grant, subscription):
return response_html, response_txt, subject


def render_batch_thank_you_for_supporting_email(grants_with_subscription):
params = {'grants_with_subscription': grants_with_subscription}
response_html = premailer_transform(render_to_string("emails/grants/batch_thank_you_for_supporting.html", params))
response_txt = render_to_string("emails/grants/batch_thank_you_for_supporting.txt", params)
subject = _("Thank you for supporting Grants on Gitcoin!")
return response_html, response_txt, subject


def render_support_cancellation_email(grant, subscription):
params = {'grant': grant, 'subscription': subscription}
response_html = premailer_transform(render_to_string("emails/grants/support_cancellation.html", params))
Expand Down
152 changes: 152 additions & 0 deletions app/retail/templates/emails/grants/batch_thank_you_for_supporting.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{% extends 'emails/template.html' %}
{% comment %}
Copyright (C) 2020 Gitcoin Core

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
{% endcomment %}
{% load i18n static grants_extra humanize %}

{% block content %}

<style>
hr {
width: 80%;
height: 2px;
border-radius: 25px;
border: none;
background-color: #D2D2D2;
}

.grant-header {
width: 100%;
text-align: center;
}

.left-grant-header {
display: inline-block;
}

.right-grant-header {
display: inline-block;
}

.right-grant-header > h2 {
font-size: 20px;
line-height: 1.5em;
}

.grant-info {
padding-top: 30px;
padding-bottom: 1%;
font-size: 1rem;
}

.grant-info a img {
margin-bottom: 10px;
}

.grant-info-name {
font-size: 20px;
color: #0D0764;
}

.grant-description {
max-width: 50rem;
margin: 1.5rem auto;
text-align: center;
}

.grant-button {
padding: 12px 36px;
border-radius: 3px;
font-size: 14px;
text-decoration: none;
background-color: #0D0764;
color: white;
}

#grow-oss {
margin-top: 50px;
margin-bottom: 50px;
}

#grant-logo {
max-width: 25rem;
width: 100%;
}

@media (min-width : 1386px) {
.left-grant-header > img {
transform: translateY(13.5%);
}
}

@media screen and (min-width: 598px) {
.right-grant-header {
padding-left: 40px;
transform: translateY(-12.5%);
text-align: left;
}
}

</style>
{% for grant_with_subscription in grants_with_subscription %}
{% with subscription=grant_with_subscription.subscription grant=grant_with_subscription.grant %}
<div class="grant-header">
<div class="left-grant-header">
<img src="{% static "v2/images/heart-robot.png" %}" alt="{% trans "Gitcoin Heart Robot" %}" title="{% trans "Gitcoin Heart Robot" %}">
</div>
<div class="right-grant-header">
<h1 style="text-transform: none;">{% trans "Thank you for supporting" %} {{ grant.title }}!
</h1>
<h2 style="text-transform: none;">{% trans "The world of open source is a better place because of you." %}
</h2>
</div>
</div>

<hr>
<br>
<div class="grant-info">
<a href="{% url 'grants:details' grant.id grant.slug %}">
<img id="grant-logo" src="{% if grant.logo and grant.logo.url %}{{ grant.logo.url }}{% else %}{% with grant_logo='v2/images/grants/logos/' id=grant.id|modulo:3 %}{% static grant_logo|addstr:id|add:'.png' %}{% endwith %}{% endif %}" />
</a>
<br>
<a class="grant-info-name" href="{% url 'grants:details' grant.id grant.slug %}">
{{ grant.title }}
</a>
<p class="grant-description">
{{ grant.description|truncatechars:300 }}
</p>
<p>
{% trans "You have contributed" %}
</p>
<p>
<strong>{{ subscription.amount_per_period|floatformat:4|intcomma }} {{ subscription.token_symbol }} </strong>
</p>
<p>{% trans "You can see all the grants you support and your transaction history " %}
<a href="{% url 'grants:profile' %}">{% trans "here." %}</a>
</p>
</div>

<br>
<a class="grant-button" href="{% url 'grants:details' grant.id grant.slug %}">{% trans "View Updates" %}</a>
<br>
<br>
<p>{% trans "If you ever need to, you can cancel your support" %}
<a href="{% url 'grants:subscription_cancel' grant.id grant.slug subscription.id %}">{% trans "here." %}</a>
</p>
<hr>
{% endwith %}
{% endfor %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% load i18n humanize %}

{% trans "Gitcoin Heart Robot" %}

{% for grant_with_subscription in grants_with_subscription %}
{% with subscription=grant_with_subscription.subscription grant=grant_with_subscription.grant %}
{% trans "Thank you for supporting" %} {{ subscription.grant.title }}

{% trans "The world of open source is a better place because of you." %}

{{ subscription.grant.description }}

{% trans "You have contributed" %}

{{ subscription.amount_per_period|floatformat:4|intcomma }} {{ subscription.token_symbol }}

{% trans "You can view the transaction" %} at
{% if subscription.token_symbol == 'ZEC' %}
{% url 'grants:details' grant.id grant.slug %}
{% elif subscription.network == 'mainnet' %}
http://etherscan.io/tx/{{ subscription.sub_new_approve_tx_id }}
{% else %}
http://{{ grant.network }}.etherscan.io/tx/{{ subscription.sub_new_approve_tx_id }}
{% endif %}

{% trans "View Updates" %}

{% trans "If you ever need to you can cancel your support" %} {% trans "here." %}
{% endwith %}
{% endfor %}