Skip to content

Commit

Permalink
add new endpoint for grant cancellation (#7912)
Browse files Browse the repository at this point in the history
  • Loading branch information
thelostone-mc authored Nov 20, 2020
1 parent bbfdb7a commit 0dbf91e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 31 deletions.
4 changes: 3 additions & 1 deletion app/grants/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -80,6 +80,8 @@
path('v1/api/grants', grants_info, name='grants_info'),
path('v1/api/grant/<int:grant_id>/', grant_details_api, name='grant_details_api'),
path('v1/api/grant/edit/<int:grant_id>/', grant_edit, name='grant_edit'),
path('v1/api/grant/<int:grant_id>/cancel', cancel_grant_v1, name='cancel_grant_v1'),


path('v1/api/<int:grant_id>/cart_payload', get_grant_payload, name='grant_payload'),
path('v1/api/<int:grant_id>/verify', verify_grant, name='verify_grant'),
Expand Down
61 changes: 53 additions & 8 deletions app/grants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1294,14 +1294,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')
Expand Down Expand Up @@ -1857,6 +1849,59 @@ 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)

response = {
'status': 200,
'pk': grant.pk,
'message': 'grant cancelled sucessfully'
}
return JsonResponse(response)

@login_required
def bulk_fund(request):
if request.method != 'POST':
Expand Down
7 changes: 3 additions & 4 deletions app/marketing/mails.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 3 additions & 4 deletions app/retail/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)


Expand Down
7 changes: 0 additions & 7 deletions app/retail/templates/emails/grants/grant_cancellation.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ <h1 style="text-transform: none;">{% trans "Grant" %} '{{ grant.title }}' {% tra
<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 %}" alt="grant logo" >
</a>
<p>{% trans "Thank you for using Gitcoin, we hope you'll come back to build more open source software!" %}</p>
<p>{% trans "You can see the grant cancellation transaction on Etherscan" %}
{% if grant.network == 'mainnet' %}
<a href="http://etherscan.io/tx/{{ grant.cancel_tx_id }}">{% trans "here." %}</a>
{% else %}
<a href="http://{{ grant.network }}.etherscan.io/tx/{{ grant.cancel_tx_id }}">{% trans "here." %}</a>
{% endif %}
</p>
<p class="grant-link">
<a class="grant-button" href="{% url 'grants:details' grant.pk grant.slug %}">{% trans "View Inactive Grant" %}</a>
</p>
Expand Down
7 changes: 0 additions & 7 deletions app/retail/templates/emails/grants/grant_cancellation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand Down

0 comments on commit 0dbf91e

Please sign in to comment.