diff --git a/hypha/apply/activity/messaging.py b/hypha/apply/activity/messaging.py index e73025fe572ddd68c2585b2a52fce32d6f5ffcff..5ac52916b8b889f31bfac5ecdef8fea2ec908663 100644 --- a/hypha/apply/activity/messaging.py +++ b/hypha/apply/activity/messaging.py @@ -597,14 +597,18 @@ class SlackAdapter(AdapterBase): return f'<{user.slack}>' def slack_channels(self, source, **kwargs): + # Set the default room as a start. target_rooms = [self.target_room] try: - extra_rooms = source.get_from_parent('slack_channel').split(',') + fund_slack_channel = source.get_from_parent('slack_channel').split(',') except AttributeError: - # Not a submission object, no extra rooms. + # Not a submission object. pass else: - target_rooms.extend(extra_rooms) + # If there are custom rooms, set them in place of the default room + custom_rooms = [channel for channel in fund_slack_channel if channel] + if len(custom_rooms) > 0: + target_rooms = custom_rooms try: comment = kwargs['comment'] @@ -621,7 +625,7 @@ class SlackAdapter(AdapterBase): # Make sure each channel name starts with a "#". target_rooms = [ - room if room.startswith('#') else '#' + room + room.strip() if room.startswith('#') else '#' + room.strip() for room in target_rooms if room ] diff --git a/hypha/apply/activity/tests/test_messaging.py b/hypha/apply/activity/tests/test_messaging.py index 73e9ab5ad252fc929c7e429bfd612261ff014541..6aa2fe27c6eb193c9d99c685275276796a2ad850 100644 --- a/hypha/apply/activity/tests/test_messaging.py +++ b/hypha/apply/activity/tests/test_messaging.py @@ -380,7 +380,7 @@ class TestSlackAdapter(AdapterMixin, TestCase): SLACK_DESTINATION_ROOM=target_room, ) @responses.activate - def test_round_slack_channel(self): + def test_fund_custom_slack_channel(self): responses.add(responses.POST, self.target_url, status=200, body='OK') submission = ApplicationSubmissionFactory(round__parent__slack_channel='dummy') adapter = SlackAdapter() @@ -390,7 +390,27 @@ class TestSlackAdapter(AdapterMixin, TestCase): self.assertDictEqual( json.loads(responses.calls[0].request.body), { - 'room': [self.target_room, '#dummy'], + 'room': ['#dummy'], + 'message': message, + } + ) + + @override_settings( + SLACK_DESTINATION_URL=target_url, + SLACK_DESTINATION_ROOM=target_room, + ) + @responses.activate + def test_fund_multiple_custom_slack_channel(self): + responses.add(responses.POST, self.target_url, status=200, body='OK') + submission = ApplicationSubmissionFactory(round__parent__slack_channel='dummy1, dummy2') + adapter = SlackAdapter() + message = 'my message' + adapter.send_message(message, '', source=submission) + self.assertEqual(len(responses.calls), 1) + self.assertDictEqual( + json.loads(responses.calls[0].request.body), + { + 'room': ['#dummy1', '#dummy2'], 'message': message, } ) diff --git a/hypha/apply/funds/migrations/0075_auto_20200629_1707.py b/hypha/apply/funds/migrations/0075_auto_20200629_1707.py new file mode 100644 index 0000000000000000000000000000000000000000..96dc9feef8d52bd10911fa83084307c1881503e0 --- /dev/null +++ b/hypha/apply/funds/migrations/0075_auto_20200629_1707.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.13 on 2020-06-29 17:07 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('funds', '0074_add_word_limit_to_text_blocks'), + ] + + operations = [ + migrations.AlterField( + model_name='applicationbase', + name='slack_channel', + field=models.CharField(blank=True, help_text='The slack #channel for notifications. If left empty, notifications will go to the default channel.', max_length=128), + ), + ] diff --git a/hypha/apply/funds/models/applications.py b/hypha/apply/funds/models/applications.py index a38bad9047d54c26e4453d6204df18e841912a16..19d1a52289e538580122569cd1bbcda6f3dadc70 100644 --- a/hypha/apply/funds/models/applications.py +++ b/hypha/apply/funds/models/applications.py @@ -79,7 +79,7 @@ class ApplicationBase(EmailForm, WorkflowStreamForm): # type: ignore guide_link = models.URLField(blank=True, max_length=255, help_text=_('Link to the apply guide.')) - slack_channel = models.CharField(blank=True, max_length=128, help_text=_('The slack #channel for notifications.')) + slack_channel = models.CharField(blank=True, max_length=128, help_text=_('The slack #channel for notifications. If left empty, notifications will go to the default channel.')) objects = PageManager.from_queryset(ApplicationBaseManager)()