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 2 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
6 changes: 5 additions & 1 deletion app/grants/sync/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ def record_contribution_activity(contribution):
# successful_contribution(grant, subscription, contribution)
# update_grant_metadata.delay(grant.pk)
new_supporter(grant, subscription)
thank_you_for_supporting(grant, subscription)
grants_with_subscription = [{
'grant': grant,
'subscription': subscription
}]
thank_you_for_supporting(grants_with_subscription)

except Exception as e:
logger.error(f"error in record_contribution_activity: {e} - {contribution}")
39 changes: 36 additions & 3 deletions app/grants/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
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
)
from marketing.models import Stat
from perftools.models import JSONStore
from townsquare.models import Comment
Expand Down Expand Up @@ -159,13 +161,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 +255,38 @@ 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:
grants_with_subscription = [{
'grant': grant,
'subscription': subscription
}]
thank_you_for_supporting(grants_with_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
})
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
11 changes: 6 additions & 5 deletions app/marketing/mails.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,20 +301,21 @@ def new_supporter(grant, subscription):
translation.activate(cur_language)


def thank_you_for_supporting(grant, subscription):
if subscription and subscription.negative:
def 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 = subscription.contributor_profile.email
to_email = positive_subscriptions[0]["subscription"].contributor_profile.email
if not to_email:
to_email = subscription.contributor_profile.user.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_thank_you_for_supporting_email(grant, subscription)
html, text, subject = render_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()])
Expand Down
4 changes: 2 additions & 2 deletions app/retail/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def render_new_supporter_email(grant, subscription):
return response_html, response_txt, subject


def render_thank_you_for_supporting_email(grant, subscription):
params = {'grant': grant, 'subscription': subscription}
def render_thank_you_for_supporting_email(grants_with_subscription):
params = {'grants_with_subscription': grants_with_subscription}
response_html = premailer_transform(render_to_string("emails/grants/thank_you_for_supporting.html", params))
response_txt = render_to_string("emails/grants/thank_you_for_supporting.txt", params)
subject = _("Thank you for supporting Grants on Gitcoin!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
}

</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" %}">
Expand Down Expand Up @@ -145,4 +147,6 @@ <h2 style="text-transform: none;">{% trans "The world of open source is a better
<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
Expand Up @@ -2,7 +2,8 @@

{% trans "Gitcoin Heart Robot" %}

{% for subscription in subscriptions %}
{% 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." %}
Expand All @@ -25,4 +26,5 @@
{% trans "View Updates" %}

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