Skip to content

Commit

Permalink
Merge pull request #827 from vanessa/fix-787
Browse files Browse the repository at this point in the history
Update Slack integration to use SDK
  • Loading branch information
amakarudze authored Mar 8, 2023
2 parents 71a7af9 + 450a710 commit ef12cb6
Show file tree
Hide file tree
Showing 16 changed files with 834 additions and 551 deletions.
6 changes: 3 additions & 3 deletions core/management_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from django.conf import settings
from django.utils.translation import gettext_lazy as _

from .forms import AddOrganizerForm
from .slack_client import slack
from core.forms import AddOrganizerForm
from core.slack_client import post_message_to_slack

# "Get organizers info" functions used in 'new_event' and 'copy_event' management commands.

Expand Down Expand Up @@ -72,4 +72,4 @@ def brag_on_slack_bang(city, country, team):
f"Congrats {', '.join(['{} {}'.format(x.first_name, x.last_name) for x in team])}!"
)

slack.chat.post_message(channel="#general", text=text, username="Django Girls", icon_emoji=":django_heart:")
post_message_to_slack("#general", text)
4 changes: 2 additions & 2 deletions core/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .event import Event, EventPageContent, EventPageMenu
from .user import User, user_invite
from .user import User, invite_user_to_slack

__all__ = ["Event", "EventPageContent", "EventPageMenu", "User", "user_invite"]
__all__ = ["Event", "EventPageContent", "EventPageMenu", "User", "invite_user_to_slack"]
15 changes: 7 additions & 8 deletions core/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
from django.utils import timezone
from django.utils.safestring import mark_safe
from django_date_extensions.fields import ApproximateDate, ApproximateDateField
from slacker import Error as SlackerError
from slack_sdk.errors import SlackApiError

from core.default_eventpage_content import get_default_eventpage_data, get_default_menu
from core.emails import notify_existing_user, notify_new_user
from core.models.managers.event import EventManager
from core.models.user import User
from core.validators import validate_approximatedate
from pictures.models import StockPicture

from ..default_eventpage_content import get_default_eventpage_data, get_default_menu
from ..emails import notify_existing_user, notify_new_user
from ..validators import validate_approximatedate
from .managers.event import EventManager
from .user import User


class Event(models.Model):
name = models.CharField(max_length=200)
Expand Down Expand Up @@ -187,7 +186,7 @@ def invite_organizer_to_team(self, user, is_new_user, password):
errors = []
try:
user.invite_to_slack()
except (ConnectionError, SlackerError) as e:
except (ConnectionError, SlackApiError) as e:
errors.append(f"Slack invite unsuccessful, reason: {e}")
notify_new_user(user, event=self, password=password, errors=errors)
else:
Expand Down
6 changes: 3 additions & 3 deletions core/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from django.contrib.auth.models import Group
from django.db import models

from ..slack_client import user_invite
from .managers.user import UserManager
from core.models.managers.user import UserManager
from core.slack_client import invite_user_to_slack


class User(auth_models.AbstractBaseUser, auth_models.PermissionsMixin):
Expand All @@ -24,7 +24,7 @@ class Meta:
verbose_name_plural = "Organizers"

def invite_to_slack(self):
user_invite(self.email, self.first_name)
invite_user_to_slack(self.email, self.first_name)

def generate_password(self):
password = User.objects.make_random_password()
Expand Down
20 changes: 16 additions & 4 deletions core/slack_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
from django.conf import settings
from slacker import Slacker
from slack_sdk import WebClient

slack = Slacker(settings.SLACK_API_KEY)
slack_client = WebClient(token=settings.SLACK_BOT_TOKEN)


def user_invite(email, first_name):
return slack.users.post("users.admin.invite", params={"email": email, "first_name": first_name, "set_active": True})
def invite_user_to_slack(email, first_name):
return slack_client.admin_users_invite(
channel_ids=settings.SLACK_INVITE_CHANNEL_IDS,
team_id=settings.SLACK_TEAM_ID,
email=email,
first_name=first_name,
set_active=True,
)


def post_message_to_slack(channel: str, message: str):
return slack_client.chat_postMessage(
channel=channel, text=message, username="Django Girls", icon_emoji=":django_heart:"
)
4 changes: 3 additions & 1 deletion djangogirls/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ def gettext(s):
DEFAULT_FROM_EMAIL = "hello@djangogirls.org"

ENABLE_SLACK_NOTIFICATIONS = sanitize(os.environ.get("ENABLE_SLACK_NOTIFICATIONS", False), bool)
SLACK_API_KEY = os.environ.get("SLACK_API_KEY")
SLACK_BOT_TOKEN = os.environ.get("SLACK_BOT_TOKEN")
SLACK_TEAM_ID = os.environ.get("SLACK_TEAM_ID")
SLACK_INVITE_CHANNEL_IDS = os.environ.get("SLACK_INVITE_CHANNEL_IDS", "").split(",")

RECAPTCHA_PUBLIC_KEY = os.environ.get("RECAPTCHA_PUBLIC_KEY", "")
RECAPTCHA_PRIVATE_KEY = os.environ.get("RECAPTCHA_PRIVATE_KEY", "")
Expand Down
14 changes: 6 additions & 8 deletions patreonmanager/management/commands/fundraising_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import requests
from django.conf import settings
from django.core.management.base import BaseCommand
from slacker import Error as SlackerError
from slack_sdk.errors import SlackApiError

from core.slack_client import slack
from core.slack_client import post_message_to_slack

from ...models import FundraisingStatus
from patreonmanager.models import FundraisingStatus

DJANGOGIRLS_USER_ID = 483065
BASE_API_URL = "https://api.patreon.com/"
Expand Down Expand Up @@ -35,8 +35,6 @@ def handle(self, *args, **options):

if settings.ENABLE_SLACK_NOTIFICATIONS:
try:
slack.chat.post_message(
channel="#notifications", text=message, username="Django Girls", icon_emoji=":django_heart:"
)
except SlackerError:
logging.warning("Slack message not sent.")
post_message_to_slack("#notifications", message)
except SlackApiError:
logging.exception("[Daily Patreon Update] Slack message not sent.")
7 changes: 5 additions & 2 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ django-gulp-rev==0.2
django-leaflet==0.24.0
django-markdown-deux==1.0.5
django-recaptcha
django-sendgrid-v5==0.8.1
django-storages[boto3]==1.7.1
django-tinymce
django-unused-media==0.1.13
django>=3.2,<4
dnspython
easy-thumbnails
easydict==1.10
pillow==9.3.0
psycopg2-binary==2.8.3
pyquery==1.4.0
Expand All @@ -39,8 +41,9 @@ isort==5.8.0
freezegun
pytest-cov==2.12.1
pytest-django==4.4.0
pytest-dotenv
pytest-dotenv==0.5.2
pytest-env==0.6.2
pytest-mock==3.10.0
pytest==6.2.5
vcrpy

Expand All @@ -53,5 +56,5 @@ icalendar==4.0.3
oauth2client==4.1.3
py-trello==0.13.0
sentry-sdk
slacker==0.9.65
slack_sdk==3.19.5
stripe
57 changes: 34 additions & 23 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
# pip-compile --output-file=requirements.txt --resolver=backtracking requirements.in
#
asgiref==3.6.0
# via django
# via
# django
# django-countries
attrs==22.2.0
# via pytest
autoflake==1.4
Expand All @@ -14,30 +16,30 @@ black==22.3.0
# via -r requirements.in
bleach[css]==5.0.1
# via django-bleach
boto3==1.26.50
boto3==1.26.85
# via django-storages
botocore==1.29.50
botocore==1.29.85
# via
# boto3
# s3transfer
build==0.10.0
# via pip-tools
cachetools==5.2.1
cachetools==5.3.0
# via google-auth
certifi==2022.12.7
# via
# requests
# sentry-sdk
cfgv==3.3.1
# via pre-commit
charset-normalizer==3.0.1
charset-normalizer==3.1.0
# via requests
click==8.1.3
# via
# black
# django-click
# pip-tools
coverage==7.0.5
coverage==7.2.1
# via pytest-cov
cssselect==1.2.0
# via pyquery
Expand Down Expand Up @@ -67,15 +69,15 @@ django-bleach==3.0.1
# via -r requirements.in
django-click==2.3.0
# via -r requirements.in
django-countries==7.5
django-countries==7.5.1
# via -r requirements.in
django-date-extensions==3.1.2
# via -r requirements.in
django-debug-toolbar==3.8.1
# via -r requirements.in
django-extensions==3.2.1
# via -r requirements.in
django-formtools==2.3.0
django-formtools==2.3
# via -r requirements.in
django-gulp-rev==0.2
# via -r requirements.in
Expand All @@ -93,10 +95,12 @@ django-tinymce==3.5.0
# via -r requirements.in
django-unused-media==0.1.13
# via -r requirements.in
dnspython==2.2.1
dnspython==2.3.0
# via -r requirements.in
easy-thumbnails==2.8.5
# via -r requirements.in
easydict==1.10
# via -r requirements.in
filelock==3.9.0
# via
# urlextract
Expand All @@ -111,7 +115,7 @@ google-api-core==2.11.0
# via google-api-python-client
google-api-python-client==2.37.0
# via -r requirements.in
google-auth==2.16.0
google-auth==2.16.2
# via
# google-api-core
# google-api-python-client
Expand All @@ -127,7 +131,7 @@ httplib2==0.21.0
# oauth2client
icalendar==4.0.3
# via -r requirements.in
identify==2.5.13
identify==2.5.18
# via pre-commit
idna==3.4
# via
Expand All @@ -144,13 +148,13 @@ jmespath==1.0.1
# botocore
lxml==4.9.2
# via pyquery
markdown2==2.4.6
markdown2==2.4.8
# via django-markdown-deux
mccabe==0.6.1
# via flake8
multidict==6.0.4
# via yarl
mypy-extensions==0.4.3
mypy-extensions==1.0.0
# via black
nodeenv==1.7.0
# via pre-commit
Expand All @@ -162,15 +166,15 @@ packaging==23.0
# via
# build
# pytest
pathspec==0.10.3
pathspec==0.11.0
# via black
pillow==9.3.0
# via
# -r requirements.in
# easy-thumbnails
pip-tools==6.12.1
# via -r requirements.in
platformdirs==2.6.2
platformdirs==3.1.0
# via
# black
# urlextract
Expand All @@ -179,7 +183,7 @@ pluggy==1.0.0
# via pytest
pre-commit==2.21.0
# via -r requirements.in
protobuf==4.21.12
protobuf==4.22.0
# via
# google-api-core
# googleapis-common-protos
Expand Down Expand Up @@ -217,6 +221,7 @@ pytest==6.2.5
# pytest-django
# pytest-dotenv
# pytest-env
# pytest-mock
pytest-cov==2.12.1
# via -r requirements.in
pytest-django==4.4.0
Expand All @@ -225,6 +230,8 @@ pytest-dotenv==0.5.2
# via -r requirements.in
pytest-env==0.6.2
# via -r requirements.in
pytest-mock==3.10.0
# via -r requirements.in
python-dateutil==2.8.2
# via
# botocore
Expand Down Expand Up @@ -253,7 +260,6 @@ requests==2.28.2
# google-api-core
# py-trello
# requests-oauthlib
# slacker
# stripe
requests-oauthlib==1.3.1
# via py-trello
Expand All @@ -265,7 +271,7 @@ s3transfer==0.6.0
# via boto3
sendgrid==6.9.7
# via django-sendgrid-v5
sentry-sdk==1.13.0
sentry-sdk==1.16.0
# via -r requirements.in
six==1.16.0
# via
Expand All @@ -276,23 +282,28 @@ six==1.16.0
# oauth2client
# python-dateutil
# vcrpy
slacker==0.9.65
slack-sdk==3.19.5
# via -r requirements.in
sqlparse==0.4.3
# via
# django
# django-debug-toolbar
starkbank-ecdsa==2.2.0
# via sendgrid
stripe==5.0.0
stripe==5.2.0
# via -r requirements.in
tinycss2==1.1.1
# via bleach
toml==0.10.2
# via
# pytest
# pytest-cov
typing-extensions==4.4.0
tomli==2.0.1
# via
# black
# build
# pyproject-hooks
typing-extensions==4.5.0
# via django-countries
uritemplate==4.1.1
# via google-api-python-client
Expand All @@ -307,15 +318,15 @@ urllib3==1.26.14
# sentry-sdk
vcrpy==4.2.1
# via -r requirements.in
virtualenv==20.17.1
virtualenv==20.20.0
# via pre-commit
webencodings==0.5.1
# via
# bleach
# tinycss2
wheel==0.38.4
# via pip-tools
wrapt==1.14.1
wrapt==1.15.0
# via vcrpy
yarl==1.8.2
# via vcrpy
Expand Down
Loading

0 comments on commit ef12cb6

Please sign in to comment.