Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdarkdragon committed Apr 24, 2018
1 parent 66b4a8a commit 4fb0e45
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 62 deletions.
2 changes: 1 addition & 1 deletion app/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
url(r'^settings/matching/?', marketing.views.matching_settings, name='matching_settings'),
url(r'^settings/feedback/?', marketing.views.feedback_settings, name='feedback_settings'),
url(r'^settings/slack/?', marketing.views.slack_settings, name='slack_settings'),
url(r'^settings/(.*)?', marketing.views.email_settings, name='feedback_settings'),
url(r'^settings/(.*)?', marketing.views.email_settings, name='email_settings'),

# marketing views
url(r'^leaderboard/(.*)', marketing.views.leaderboard, name='leaderboard'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 2.0.4 on 2018-04-19 15:17
# Generated by Django 2.0.4 on 2018-04-23 13:43

import django.contrib.postgres.fields
from django.db import migrations, models
Expand All @@ -7,23 +7,22 @@
class Migration(migrations.Migration):

dependencies = [
('marketing', '0021_emailsubscriber_profile'),
('dashboard', '0055_remove_bounty_avatar_url'),
]

operations = [
migrations.AddField(
model_name='emailsubscriber',
model_name='profile',
name='repos',
field=django.contrib.postgres.fields.ArrayField(
base_field=models.CharField(max_length=200), blank=True, default=[], size=None),
field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=200), blank=True, default=[], size=None),
),
migrations.AddField(
model_name='emailsubscriber',
model_name='profile',
name='slack_channel',
field=models.CharField(default='', max_length=255),
),
migrations.AddField(
model_name='emailsubscriber',
model_name='profile',
name='slack_token',
field=models.CharField(default='', max_length=255),
),
Expand Down
5 changes: 4 additions & 1 deletion app/dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
from django.contrib.auth.models import User
from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.contrib.humanize.templatetags.humanize import naturalday, naturaltime
from django.contrib.postgres.fields import JSONField
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.contrib.postgres.fields import ArrayField, JSONField
from django.db import models
from django.db.models.signals import m2m_changed, post_delete, post_save, pre_save
from django.dispatch import receiver
Expand Down Expand Up @@ -845,6 +845,9 @@ class Profile(SuperModel):
last_sync_date = models.DateTimeField(null=True)
email = models.CharField(max_length=255, blank=True, db_index=True)
github_access_token = models.CharField(max_length=255, blank=True, db_index=True)
repos = ArrayField(models.CharField(max_length=200), blank=True, default=[])
slack_token = models.CharField(max_length=255, default='')
slack_channel = models.CharField(max_length=255, default='')
suppress_leaderboard = models.BooleanField(
default=False,
help_text='If this option is chosen, we will remove your profile information from the leaderboard',
Expand Down
20 changes: 11 additions & 9 deletions app/dashboard/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,21 @@ def maybe_market_to_slack(bounty, event_name):


def build_message_for_slack(bounty, event_name):
"""Build message to be posted to slack.
Args:
bounty (dashboard.models.Bounty): The Bounty to be marketed.
event_name (str): The name of the event.
Returns:
str: Message to post to slack.
"""
conv_details = ""
usdt_details = ""
try:
conv_details = f"@ (${round(convert_token_to_usdt(bounty.token_name),2)}/{bounty.token_name})"
usdt_details = f"({bounty.value_in_usdt_now} USD {conv_details} "
except Exception:
pass # no USD conversion rate
except Exception as e:
print(e)
return False

title = bounty.title if bounty.title else bounty.github_url
msg = f"{event_name.replace('bounty', 'funded_issue')} worth {round(bounty.get_natural_value(), 4)} {bounty.token_name} " \
Expand All @@ -215,7 +220,7 @@ def maybe_market_to_user_slack(bounty, event_name):
bool: Whether or not the Slack notification was sent successfully.
"""
from marketing.models import EmailSubscriber
from dashboard.models import Profile
if bounty.get_natural_value() < 0.0001:
return False
if bounty.network != settings.ENABLE_NOTIFICATIONS_ON_NETWORK:
Expand All @@ -232,20 +237,17 @@ def maybe_market_to_user_slack(bounty, event_name):
try:
username = uri_array[1]
repo = uri_array[2]
subscribers = EmailSubscriber.objects.filter(profile__handle=username, repos__contains=[repo])
subscribers = subscribers & EmailSubscriber.objects.exclude(slack_token='', slack_channel='')
subscribers = Profile.objects.filter(handle=username, repos__contains=[repo])
subscribers = subscribers & Profile.objects.exclude(slack_token='', slack_channel='')
for subscriber in subscribers:
try:
sc = SlackClient(subscriber.slack_token)
sc.api_call("chat.postMessage", channel=subscriber.slack_channel, text=msg)
sent = True
except Exception as e:
print(e)
except IndexError:
return False
except Exception as e:
print(e)
return False

return sent

Expand Down
3 changes: 0 additions & 3 deletions app/marketing/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ class EmailSubscriber(SuperModel):
priv = models.CharField(max_length=30, default='')
github = models.CharField(max_length=255, default='')
keywords = ArrayField(models.CharField(max_length=200), blank=True, default=[])
repos = ArrayField(models.CharField(max_length=200), blank=True, default=[])
slack_token = models.CharField(max_length=255, default='')
slack_channel = models.CharField(max_length=255, default='')
profile = models.ForeignKey(
'dashboard.Profile',
on_delete=models.CASCADE,
Expand Down
51 changes: 29 additions & 22 deletions app/marketing/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,28 +401,28 @@ def funnel(request):
}
return TemplateResponse(request, 'funnel.html', params)

settings_navs = [
{

def get_settings_navs():
return [{
'body': 'Email',
'href': '/settings/email',
'href': reverse('settings_email', args=('', ))
},
{
'body': 'Privacy',
'href': '/settings/privacy',
'href': reverse('privacy_settings'),
},
{
'body': 'Matching',
'href': '/settings/matching',
'href': reverse('matching_settings'),
},
{
'body': 'Feedback',
'href': '/settings/feedback',
'href': reverse('feedback_settings'),
},
{
'body': 'Slack',
'href': '/settings/slack',
},
]
'href': reverse('slack_settings'),
}]


def settings_helper_get_auth(request, key=None):
Expand Down Expand Up @@ -491,7 +491,7 @@ def privacy_settings(request):
'nav': 'internal',
'active': '/settings/privacy',
'title': _('Privacy Settings'),
'navs': settings_navs,
'navs': get_settings_navs(),
'is_logged_in': is_logged_in,
'msg': msg,
}
Expand Down Expand Up @@ -519,7 +519,7 @@ def matching_settings(request):
else:
es.metadata['ip'].append(ip)
es.save()
msg = "Updated your preferences. "
msg = _('Updated your preferences.')

context = {
'keywords': ",".join(es.keywords),
Expand All @@ -529,7 +529,7 @@ def matching_settings(request):
'nav': 'internal',
'active': '/settings/matching',
'title': _('Matching Settings'),
'navs': settings_navs,
'navs': get_settings_navs(),
'msg': msg,
}
return TemplateResponse(request, 'settings/matching.html', context)
Expand All @@ -556,13 +556,13 @@ def feedback_settings(request):
else:
es.metadata['ip'].append(ip)
es.save()
msg = "We've received your feedback. "
msg = _('We\'ve received your feedback.')

context = {
'nav': 'internal',
'active': '/settings/feedback',
'title': _('Feedback'),
'navs': settings_navs,
'navs': get_settings_navs(),
'msg': msg,
}
return TemplateResponse(request, 'settings/feedback.html', context)
Expand Down Expand Up @@ -608,20 +608,25 @@ def email_settings(request, key):
else:
es.metadata['ip'].append(ip)
es.save()
msg = "Updated your preferences. "
msg = _('Updated your preferences.')
context = {
'nav': 'internal',
'active': '/settings/email',
'title': _('Email Settings'),
'es': es,
'msg': msg,
'navs': settings_navs,
'navs': get_settings_navs(),
}
return TemplateResponse(request, 'settings/email.html', context)


def slack_settings(request):
"""Displays and saves user's slack settings.
Returns:
TemplateResponse: The user's slack settings template response.
"""
# setup
profile, es, user, is_logged_in = settings_helper_get_auth(request)
if not es:
Expand All @@ -634,25 +639,27 @@ def slack_settings(request):
token = request.POST.get('token', '')
repos = request.POST.get('repos').split(',')
channel = request.POST.get('channel', '')
es.slack_token = token
es.repos = repos
es.slack_channel = channel
profile.slack_token = token
profile.repos = repos
profile.slack_channel = channel
ip = get_ip(request)
if not es.metadata.get('ip', False):
es.metadata['ip'] = [ip]
else:
es.metadata['ip'].append(ip)
es.save()
msg = "Updated your preferences. "
profile.save()
msg = _('Updated your preferences.')

context = {
'repos': ",".join(es.repos),
'repos': ",".join(profile.repos),
'is_logged_in': is_logged_in,
'nav': 'internal',
'active': '/settings/slack',
'title': _('Slack Settings'),
'navs': settings_navs,
'navs': get_settings_navs(),
'es': es,
'profile': profile,
'msg': msg,
}
return TemplateResponse(request, 'settings/slack.html', context)
Expand Down
37 changes: 18 additions & 19 deletions app/retail/templates/settings/slack.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
{% extends 'settings/settings.html' %}
{% load i18n static %}

{% block settings_content %}
<form id="settings" method="POST">
<div class="form-group">
<h5>{% trans "Slack Integration" %}</h5>
<label class="form__label" for="token">{% trans "Slack API token" %}</label>
<input type="text" name="token" class="form__input" placeholder='API token' value="{{ es.slack_token }}">
</div>
<div class="form-group">
<label class="form__label" for="repos">{% trans "Github repos to monitor" %}</label>
<input type="text" name="repos" class="form__input" placeholder='comma, separated, list' value="{{ repos }}">
</div>
<div class="form-group">
<label class="form__label" for="channel">{% trans "Slack channel to post to" %}</label>
<input type="text" name="channel" class="form__input" value="{{ es.slack_channel }}">
</div>
{% csrf_token %}
<input class='button button--primary' type='submit' name='submit' value="Go">
</form>
<form id="settings" method="POST">
<div class="form-group">
<h5>{% trans "Slack Integration" %}</h5>
<label class="form__label" for="token">{% trans "Slack API token" %}</label>
<input type="text" name="token" class="form__input" placeholder='API token' value="{{ profile.slack_token }}">
</div>
{% endblock %}
<div class="form-group">
<label class="form__label" for="repos">{% trans "Github repos to monitor" %}</label>
<input type="text" name="repos" class="form__input" placeholder='comma, separated, list' value="{{ repos }}">
</div>
<div class="form-group">
<label class="form__label" for="channel">{% trans "Slack channel to post to" %}</label>
<input type="text" name="channel" class="form__input" value="{{ profile.slack_channel }}">
</div>
{% csrf_token %}
<input class='button button--primary' type='submit' name='submit' value="Go">
</form>
</div>
{% endblock %}

0 comments on commit 4fb0e45

Please sign in to comment.