From 4fb0e453577034f3643f6c952570e9a70b7813b2 Mon Sep 17 00:00:00 2001 From: Ivan Tivonenko Date: Mon, 23 Apr 2018 18:02:50 +0300 Subject: [PATCH] fixes #259 --- app/app/urls.py | 2 +- .../migrations/0056_auto_20180423_1343.py} | 13 +++-- app/dashboard/models.py | 5 +- app/dashboard/notifications.py | 20 ++++---- app/marketing/models.py | 3 -- app/marketing/views.py | 51 +++++++++++-------- app/retail/templates/settings/slack.html | 37 +++++++------- 7 files changed, 69 insertions(+), 62 deletions(-) rename app/{marketing/migrations/0022_auto_20180419_1517.py => dashboard/migrations/0056_auto_20180423_1343.py} (57%) diff --git a/app/app/urls.py b/app/app/urls.py index d921ab02249..38ad3b3a657 100644 --- a/app/app/urls.py +++ b/app/app/urls.py @@ -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'), diff --git a/app/marketing/migrations/0022_auto_20180419_1517.py b/app/dashboard/migrations/0056_auto_20180423_1343.py similarity index 57% rename from app/marketing/migrations/0022_auto_20180419_1517.py rename to app/dashboard/migrations/0056_auto_20180423_1343.py index 601b722d801..9243b88cc33 100644 --- a/app/marketing/migrations/0022_auto_20180419_1517.py +++ b/app/dashboard/migrations/0056_auto_20180423_1343.py @@ -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 @@ -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), ), diff --git a/app/dashboard/models.py b/app/dashboard/models.py index 1e8ec459545..782d6a7f102 100644 --- a/app/dashboard/models.py +++ b/app/dashboard/models.py @@ -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 @@ -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', diff --git a/app/dashboard/notifications.py b/app/dashboard/notifications.py index aa1e30af67e..3a07de43e08 100644 --- a/app/dashboard/notifications.py +++ b/app/dashboard/notifications.py @@ -186,6 +186,14 @@ 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: @@ -193,9 +201,6 @@ def build_message_for_slack(bounty, event_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} " \ @@ -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: @@ -232,8 +237,8 @@ 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) @@ -241,11 +246,8 @@ def maybe_market_to_user_slack(bounty, event_name): sent = True except Exception as e: print(e) - except IndexError: - return False except Exception as e: print(e) - return False return sent diff --git a/app/marketing/models.py b/app/marketing/models.py index 803e788042a..fd11cd516f1 100644 --- a/app/marketing/models.py +++ b/app/marketing/models.py @@ -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, diff --git a/app/marketing/views.py b/app/marketing/views.py index 980cd620976..b7edde29a0e 100644 --- a/app/marketing/views.py +++ b/app/marketing/views.py @@ -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): @@ -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, } @@ -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), @@ -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) @@ -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) @@ -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: @@ -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) diff --git a/app/retail/templates/settings/slack.html b/app/retail/templates/settings/slack.html index 87fb58daf4d..08cb9cf4ae2 100644 --- a/app/retail/templates/settings/slack.html +++ b/app/retail/templates/settings/slack.html @@ -1,23 +1,22 @@ {% extends 'settings/settings.html' %} {% load i18n static %} - {% block settings_content %} -
-
-
{% trans "Slack Integration" %}
- - -
-
- - -
-
- - -
- {% csrf_token %} - -
+
+
+
{% trans "Slack Integration" %}
+ +
-{% endblock %} \ No newline at end of file +
+ + +
+
+ + +
+ {% csrf_token %} + +
+ +{% endblock %}