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 email alert on low priced bounties #3174

Closed
wants to merge 3 commits into from
Closed
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
13 changes: 13 additions & 0 deletions app/dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
_AUTH, HEADERS, TOKEN_URL, build_auth_dict, get_gh_issue_details, get_issue_comments, issue_number, org_name,
repo_name,
)
from marketing.mails import low_bounty_alert
from marketing.models import LeaderboardRank
from rest_framework import serializers
from web3 import Web3
Expand Down Expand Up @@ -988,6 +989,18 @@ def additional_funding_summary_sentence(self):
return sentence


LOW_BOUNTY_THRESHOLD = 5.0


@receiver(post_save, sender=Bounty)
def alert_low_bounty(sender, **kwargs):
"""Alert when a bounty with a low price is created."""
bounty = kwargs.get('instance')
usdt_value = bounty.get_value_in_usdt_now
if(kwargs.get('created') is True and usdt_value and usdt_value < LOW_BOUNTY_THRESHOLD):
low_bounty_alert(bounty)


class BountyFulfillmentQuerySet(models.QuerySet):
"""Handle the manager queryset for BountyFulfillments."""

Expand Down
47 changes: 47 additions & 0 deletions app/dashboard/tests/test_dashboard_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

"""
from datetime import date, datetime, timedelta
from unittest.mock import patch

from django.conf import settings
from django.contrib.humanize.templatetags.humanize import naturaltime
Expand Down Expand Up @@ -464,3 +465,49 @@ def test_bounty_clean_gh_url_on_save():
)
assert bounty.github_url == 'https://github.com/gitcoinco/web/issues/305'
bounty.delete()

@patch('marketing.mails.send_mail')
def test_low_bounty_alert(self, mock_send_mail):
"""Test that an alert email is sent when a bounty with a low value is created"""
Bounty.objects.create(
title='Low Value Bounty',
value_in_token=1 * 1e18,
token_name='ETH',
web3_created=datetime(2008, 10, 31, tzinfo=pytz.UTC),
github_url='https://github.com/gitcoinco/web/issues/305#issuecomment-999999999',
token_address='0x0000000000000000000000000000000000000000',
issue_description='hello world',
bounty_owner_github_username='flintstone',
is_open=False,
accepted=False,
expires_date=datetime(2008, 11, 30, tzinfo=pytz.UTC),
idx_project_length=5,
project_length='Months',
bounty_type='Feature',
experience_level='Intermediate',
raw_data={},
)
assert mock_send_mail.call_count == 1

@patch('marketing.mails.send_mail')
def test_no_bounty_alert_for_reasonable_bounties(self, mock_send_mail):
"""Test that no alert email is sent when a bounty with a reasonable price is created."""
Bounty.objects.create(
title='Reasonable Value Bounty',
value_in_token=3 * 1e18,
token_name='ETH',
web3_created=datetime(2008, 10, 31, tzinfo=pytz.UTC),
github_url='https://github.com/gitcoinco/web/issues/305#issuecomment-999999999',
token_address='0x0000000000000000000000000000000000000000',
issue_description='hello world',
bounty_owner_github_username='flintstone',
is_open=False,
accepted=False,
expires_date=datetime(2008, 11, 30, tzinfo=pytz.UTC),
idx_project_length=5,
project_length='Months',
bounty_type='Feature',
experience_level='Intermediate',
raw_data={},
)
assert mock_send_mail.call_count == 0
23 changes: 23 additions & 0 deletions app/marketing/mails.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,29 @@ def setup_lang(to_email):
translation.activate(preferred_language)


def low_bounty_alert(bounty):
to_email = '[email protected]'
from_email = bounty.bounty_owner_email or settings.SERVER_EMAIL
cur_language = translation.get_language()
try:
setup_lang(to_email)
subject = _("Low Bounty Alert")
body_str = _("A low bounty was set from")
body = f"{body_str} {bounty.bounty_owner_name} ({bounty.bounty_owner_email}):"\
f"{settings.BASE_URL}_administrationdashboard/bounty/{bounty.pk}/change\n\n"\
f"Github: {bounty.github_url}\n"
send_mail(
from_email,
to_email,
subject,
body,
from_name=_("No Reply from Gitcoin.co"),
categories=['admin'],
)
finally:
translation.activate(cur_language)


def new_bounty_request(model):
to_email = '[email protected]'
from_email = model.requested_by.email or settings.SERVER_EMAIL
Expand Down