diff --git a/app/grants/sync/helpers.py b/app/grants/sync/helpers.py index 0649768a0a8..e3c055c1435 100644 --- a/app/grants/sync/helpers.py +++ b/app/grants/sync/helpers.py @@ -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}") diff --git a/app/grants/tasks.py b/app/grants/tasks.py index 30d18d1b6f1..a357d07ae8a 100644 --- a/app/grants/tasks.py +++ b/app/grants/tasks.py @@ -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 @@ -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 @@ -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) diff --git a/app/grants/views.py b/app/grants/views.py index 095ef778e0f..63dcd3cc92f 100644 --- a/app/grants/views.py +++ b/app/grants/views.py @@ -2063,8 +2063,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) @@ -2109,7 +2110,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'), @@ -2145,7 +2145,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', @@ -2159,13 +2163,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, diff --git a/app/marketing/mails.py b/app/marketing/mails.py index 55253a56b19..b583cb6277b 100644 --- a/app/marketing/mails.py +++ b/app/marketing/mails.py @@ -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()]) diff --git a/app/retail/emails.py b/app/retail/emails.py index e36bbb40855..16403a0ddf5 100644 --- a/app/retail/emails.py +++ b/app/retail/emails.py @@ -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!") diff --git a/app/retail/templates/emails/grants/thank_you_for_supporting.html b/app/retail/templates/emails/grants/thank_you_for_supporting.html index adbd78c0ad0..a00ec72ca26 100644 --- a/app/retail/templates/emails/grants/thank_you_for_supporting.html +++ b/app/retail/templates/emails/grants/thank_you_for_supporting.html @@ -101,6 +101,8 @@ } +{% for grant_with_subscription in grants_with_subscription %} +{% with subscription=grant_with_subscription.subscription grant=grant_with_subscription.grant %}