Skip to content

Commit

Permalink
Manually add slack channels and option to disable auto update all cha…
Browse files Browse the repository at this point in the history
…nnels (#453)
  • Loading branch information
GDay committed Mar 29, 2024
1 parent a16c417 commit 8920b0b
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 1 deletion.
5 changes: 5 additions & 0 deletions back/admin/settings/templates/settings_integrations.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
<td>{% trans "Slack bot" %}</td>
<td style="text-align: right">
{% if slack_bot is not None %}
<a href="{% url "settings:slack-account-add-channel" %}" class="btn btn-primary">
{% trans "Manually add Slack channel" %}
</a>
{% if not disable_update_channels_list %}
<a href="{% url "settings:slack-account-update-channels" %}" class="btn btn-primary">
{% trans "Update Slack channels list" %}
</a>
{% endif %}
<a href="https://slack.com/oauth/v2/authorize?scope=im:history,users:read,chat:write,users:read.email,im:write,im:read,channels:read,groups:read&client_id={{ slack_bot.client_id }}&redirect_uri={{ base_url }}{% url 'integrations:slack' %}" class="btn btn-primary">
Add to your Slack team
</a>
Expand Down
39 changes: 39 additions & 0 deletions back/admin/settings/templates/slack_channel_create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% extends 'settings_base.html' %}
{% load i18n %}
{% load crispy_forms_tags %}

{% block actions %}
{% if object %}
<a href="{% url 'integrations:builder-detail' object.id %}" class="btn btn-primary">
{% translate "Live edit and test" %}
</a>
{% endif %}
{% endblock %}
{% block settings_content %}
<div class="row" style="min-height: 191px;">
<div class="col-5">
<div class="card-body">
<h3>{% translate "Existing Slack channels" %}</h3>
{% if channels|length == 0 %}
<p>{% translate "No channels yet" %}</p>
{% endif %}
<ul>
{% for item in channels %}
<li>
{{ item.name }}
</li>
{% endfor %}
</ul>
</div>
</div>
<div class="col-7">
<div class="card-body">
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary">{% translate "Add" %}</button>
</form>
</div>
</div>
</div>
{% endblock %}
50 changes: 50 additions & 0 deletions back/admin/settings/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.test import override_settings
from django.urls import reverse
from django_q.models import Schedule

Expand Down Expand Up @@ -572,9 +573,58 @@ def test_slack_channels_update_view(client, admin_factory):
client.force_login(admin_user1)
Integration.objects.create(integration=Integration.Type.SLACK_BOT)

# button is visible
url = reverse("settings:integrations")
response = client.get(url)

assert "Update Slack channels list" in response.content.decode()

url = reverse("settings:slack-account-update-channels")
response = client.get(url)

assert "Newly added channels have been added." not in response.content.decode()
assert SlackChannel.objects.all().count() == 3
assert SlackChannel.objects.filter(name="general", is_private=False).exists()


@pytest.mark.django_db
@override_settings(SLACK_DISABLE_AUTO_UPDATE_CHANNELS=True)
def test_disable_slack_channels_update_view(client, admin_factory):
admin_user1 = admin_factory()
client.force_login(admin_user1)
Integration.objects.create(integration=Integration.Type.SLACK_BOT)

# button is not visible
url = reverse("settings:integrations")
response = client.get(url)

assert "Update Slack channels list" not in response.content.decode()

url = reverse("settings:slack-account-update-channels")
response = client.get(url)

assert response.status_code == 404


@pytest.mark.django_db
def test_manually_add_slack_channels(client, admin_factory):
admin_user1 = admin_factory()
client.force_login(admin_user1)
Integration.objects.create(integration=Integration.Type.SLACK_BOT)

SlackChannel.objects.create(name="test_channel")
url = reverse("settings:slack-account-add-channel")
response = client.get(url)

# channel shows up
assert "test_channel" in response.content.decode()

# add channel
url = reverse("settings:slack-account-add-channel")
response = client.post(
url, data={"name": "test2_channel", "is_private": True}, follow=True
)

assert "test2_channel" in response.content.decode()
# general is created by default
assert SlackChannel.objects.all().count() == 3
5 changes: 5 additions & 0 deletions back/admin/settings/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
views.SlackChannelsUpdateView.as_view(),
name="slack-account-update-channels",
),
path(
"integrations/add_channel/",
views.SlackChannelsCreateView.as_view(),
name="slack-account-add-channel",
),
path(
"integrations/slack_bot/", views.SlackBotSetupView.as_view(), name="slack-bot"
),
Expand Down
23 changes: 22 additions & 1 deletion back/admin/settings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.messages.views import SuccessMessageMixin
from django.http import HttpResponse, HttpResponseRedirect
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse_lazy
from django.utils import translation
Expand Down Expand Up @@ -337,6 +337,9 @@ def get_context_data(self, **kwargs):
integration=Integration.Type.CUSTOM, is_active=False
)
context["add_action"] = reverse_lazy("integrations:create")
context["disable_update_channels_list"] = (
settings.SLACK_DISABLE_AUTO_UPDATE_CHANNELS
)
return context


Expand Down Expand Up @@ -375,6 +378,8 @@ class SlackChannelsUpdateView(LoginRequiredMixin, AdminPermMixin, RedirectView):
pattern_name = "settings:integrations"

def get(self, request, *args, **kwargs):
if settings.SLACK_DISABLE_AUTO_UPDATE_CHANNELS:
raise Http404
SlackChannel.objects.update_channels()
messages.success(
request,
Expand All @@ -384,3 +389,19 @@ def get(self, request, *args, **kwargs):
),
)
return super().get(request, *args, **kwargs)


class SlackChannelsCreateView(LoginRequiredMixin, AdminPermMixin, CreateView):
template_name = "slack_channel_create.html"
model = SlackChannel
fields = ["name", "is_private"]
success_message = _("Slack channel has been added")
success_url = reverse_lazy("settings:slack-account-add-channel")

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = _("Slack channels")
context["subtitle"] = _("settings")
context["button_text"] = _("Enable")
context["channels"] = SlackChannel.objects.all()
return context
3 changes: 3 additions & 0 deletions back/back/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
SLACK_USE_SOCKET = env.bool("SLACK_USE_SOCKET", default=False)
SLACK_APP_TOKEN = env("SLACK_APP_TOKEN", default="")
SLACK_BOT_TOKEN = env("SLACK_BOT_TOKEN", default="")
SLACK_DISABLE_AUTO_UPDATE_CHANNELS = env.bool(
"SLACK_DISABLE_AUTO_UPDATE_CHANNELS", default=False
)

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
Expand Down
10 changes: 10 additions & 0 deletions docs/config/slackbot.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ settings:
8. Go back to your Slack bot and go to "App Home". Then scroll down till you see: "Show Tabs". Enable the "message tab" and check the "Allow users to send Slash commands and messages from the messages tab".

That's it!


## Slack channels
You can import all channels in one go if you click on the "Update Slack channels list" button in the settings.
In some cases, you might want to avoid this at all costs (if you have thousands of channels). You can do that by setting:

`SLACK_DISABLE_AUTO_UPDATE_CHANNELS`

Default: `False`. Setting this to `True` will remove the button and disable this option

0 comments on commit 8920b0b

Please sign in to comment.