From 1a94f405cadaa6e2f685ddaf6ae4b86ced25f66e Mon Sep 17 00:00:00 2001 From: Aditya Anand M C Date: Fri, 20 Nov 2020 12:18:49 +0530 Subject: [PATCH] add new endpoint for grant cancellation --- app/grants/urls.py | 4 +- app/grants/views.py | 63 ++++++++++++++----- app/marketing/mails.py | 7 +-- app/retail/emails.py | 7 +-- .../emails/grants/grant_cancellation.html | 7 --- .../emails/grants/grant_cancellation.txt | 7 --- 6 files changed, 56 insertions(+), 39 deletions(-) diff --git a/app/grants/urls.py b/app/grants/urls.py index b001e723feb..556037c93f6 100644 --- a/app/grants/urls.py +++ b/app/grants/urls.py @@ -20,7 +20,7 @@ from django.urls import path, re_path from grants.views import ( - add_grant_from_collection, bulk_fund, bulk_grants_for_cart, clr_grants, contribute_to_grants_v1, + add_grant_from_collection, bulk_fund, bulk_grants_for_cart, cancel_grant_v1, clr_grants, contribute_to_grants_v1, contribution_addr_from_all_as_json, contribution_addr_from_grant_as_json, contribution_addr_from_grant_during_round_as_json, contribution_addr_from_round_as_json, create_matching_pledge_v1, flag, get_collection, get_collections_list, get_grant_payload, get_grants, get_interrupted_contributions, @@ -78,6 +78,8 @@ path('v1/api/grants', grants_info, name='grants_info'), path('v1/api/grant//', grant_details_api, name='grant_details_api'), path('v1/api/grant/edit//', grant_edit, name='grant_edit'), + path('v1/api/grant//cancel', cancel_grant_v1, name='cancel_grant_v1'), + path('v1/api//cart_payload', get_grant_payload, name='grant_payload'), path('v1/api//verify', verify_grant, name='verify_grant'), diff --git a/app/grants/views.py b/app/grants/views.py index fe227c5c41d..e24b360a76b 100644 --- a/app/grants/views.py +++ b/app/grants/views.py @@ -1282,14 +1282,6 @@ def grant_details(request, grant_id, grant_slug): grant.save() record_grant_activity_helper('update_grant', grant, profile) return redirect(reverse('grants:details', args=(grant.pk, grant.slug))) - if 'grant_cancel_tx_id' in request.POST: - grant.cancel_tx_id = request.POST.get('grant_cancel_tx_id', '') - grant.active = False - grant.save() - grant_cancellation(grant, user_subscription) - for sub in subscriptions: - subscription_terminated(grant, sub) - record_grant_activity_helper('killed_grant', grant, profile) elif 'edit-title' in request.POST: grant.title = request.POST.get('edit-title') grant.github_project_url = request.POST.get('edit-github_project_url') @@ -1552,14 +1544,6 @@ def grant_edit(request, grant_id): # grant.save() # record_grant_activity_helper('update_grant', grant, profile) # return redirect(reverse('grants:details', args=(grant.pk, grant.slug))) - # if 'grant_cancel_tx_id' in request.POST: - # grant.cancel_tx_id = request.POST.get('grant_cancel_tx_id', '') - # grant.active = False - # grant.save() - # grant_cancellation(grant, user_subscription) - # for sub in subscriptions: - # subscription_terminated(grant, sub) - # record_grant_activity_helper('killed_grant', grant, profile) # title = request_body.get('title', None) # github_project_url = request_body.get('github_project_url', None) @@ -1922,6 +1906,53 @@ def grant_fund(request, grant_id, grant_slug): raise Http404 + +@csrf_exempt +@require_POST +def cancel_grant_v1(request, grant_id): + + response = { + 'status': 400, + 'message': 'error: Bad Request. Unable to contribute to grant' + } + + + user = request.user if request.user.is_authenticated else None + if not user: + response['message'] = 'error: user needs to be authenticated to cancel grant' + return JsonResponse(response) + + profile = request.user.profile if hasattr(request.user, 'profile') else None + + if not profile: + response['message'] = 'error: no matching profile found' + return JsonResponse(response) + + if not request.method == 'POST': + response['message'] = 'error: grant cancellation is a POST operation' + return JsonResponse(response) + + try: + grant = Grant.objects.get(pk=grant_id) + except Grant.DoesNotExist: + response['message'] = 'error: grant cannot be found' + return JsonResponse(response) + + if not is_grant_team_member(grant, profile): + response['message'] = 'error: grant cancellation can be done only by grant owner' + return JsonResponse(response) + + if not grant.active: + response['message'] = 'error: grant is already cancelled' + return JsonResponse(response) + + grant.active = False + grant.save() + + grant_cancellation(grant) + record_grant_activity_helper('killed_grant', grant, profile) + + @login_required def bulk_fund(request): if request.method != 'POST': diff --git a/app/marketing/mails.py b/app/marketing/mails.py index 48b3741560b..42bc3083b4e 100644 --- a/app/marketing/mails.py +++ b/app/marketing/mails.py @@ -336,22 +336,21 @@ def support_cancellation(grant, subscription): translation.activate(cur_language) -def grant_cancellation(grant, subscription): - if subscription and subscription.negative: - return +def grant_cancellation(grant): from_email = settings.CONTACT_EMAIL to_email = grant.admin_profile.email cur_language = translation.get_language() try: setup_lang(to_email) - html, text, subject = render_grant_cancellation_email(grant, subscription) + html, text, subject = render_grant_cancellation_email(grant) if not should_suppress_notification_email(to_email, 'grant_cancellation'): send_mail(from_email, to_email, subject, text, html, categories=['transactional', func_name()]) finally: translation.activate(cur_language) + def grant_txn_failed(failed_contrib): profile, grant, tx_id = failed_contrib.subscription.contributor_profile, failed_contrib.subscription.grant, failed_contrib.tx_id diff --git a/app/retail/emails.py b/app/retail/emails.py index 2ecbee79f83..f406d6b12a7 100644 --- a/app/retail/emails.py +++ b/app/retail/emails.py @@ -149,8 +149,8 @@ def render_support_cancellation_email(grant, subscription): return response_html, response_txt, subject -def render_grant_cancellation_email(grant, subscription): - params = {'grant': grant, 'subscription': subscription} +def render_grant_cancellation_email(grant): + params = {'grant': grant} response_html = premailer_transform(render_to_string("emails/grants/grant_cancellation.html", params)) response_txt = render_to_string("emails/grants/grant_cancellation.txt", params) subject = _("Your Grant on Gitcoin Grants has been cancelled") @@ -215,8 +215,7 @@ def subscription_terminated(request): @staff_member_required def grant_cancellation(request): grant = Grant.objects.first() - subscription = Subscription.objects.filter(grant__pk=grant.pk).first() - response_html, __, __ = render_grant_cancellation_email(grant, subscription) + response_html, __, __ = render_grant_cancellation_email(grant) return HttpResponse(response_html) diff --git a/app/retail/templates/emails/grants/grant_cancellation.html b/app/retail/templates/emails/grants/grant_cancellation.html index 6577d338a7e..e92340eaa01 100644 --- a/app/retail/templates/emails/grants/grant_cancellation.html +++ b/app/retail/templates/emails/grants/grant_cancellation.html @@ -65,13 +65,6 @@

{% trans "Grant" %} '{{ grant.title }}' {% tra

{% trans "Thank you for using Gitcoin, we hope you'll come back to build more open source software!" %}

-

{% trans "You can see the grant cancellation transaction on Etherscan" %} - {% if grant.network == 'mainnet' %} - {% trans "here." %} - {% else %} - {% trans "here." %} - {% endif %} -

diff --git a/app/retail/templates/emails/grants/grant_cancellation.txt b/app/retail/templates/emails/grants/grant_cancellation.txt index 11fe9a5b591..0a20d26b675 100644 --- a/app/retail/templates/emails/grants/grant_cancellation.txt +++ b/app/retail/templates/emails/grants/grant_cancellation.txt @@ -4,13 +4,6 @@ {% trans "Grant" %} {{ grant.title }} {% trans "has been cancelled" %} -{% trans "You can see the transaction on Etherscan" %} at -{% if grant.network == 'mainnet' %} - http://etherscan.io/tx/{{ grant.cancel_tx_id }} -{% else %} - http://{{ grant.network }}.etherscan.io/tx/{{ grant.cancel_tx_id }} -{% endif %} - {% trans "View Inactive Grant here" %} {% url 'grants:details' grant.pk grant.slug %}