Skip to content

Commit

Permalink
Custom exceptions added
Browse files Browse the repository at this point in the history
  • Loading branch information
jvorcak committed Feb 15, 2020
1 parent 0549920 commit 1714dba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

setup(
version="1.0.33",
version="1.0.34",
install_requires=["slackclient"]
)
13 changes: 13 additions & 0 deletions slack_app/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ class SlackAccountNotLinkedException(Exception):
"""
Thrown if user is not linked with a User model yet.
"""

def __init__(self, slack_user_mapping):
super().__init__()
self.slack_user_mapping = slack_user_mapping


class SlackReceiverDoesNotExist(Exception):
pass


class SlackInteractivityTypeDoesNotExist(SlackReceiverDoesNotExist):
pass


class SlackCommandDoesNotExist(SlackReceiverDoesNotExist):
pass
19 changes: 16 additions & 3 deletions slack_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from django.urls import reverse
from django.views.decorators.http import require_http_methods

from .exceptions import SlackAppNotInstalledProperlyException, SlackAccountNotLinkedException
from .exceptions import SlackAppNotInstalledProperlyException, SlackAccountNotLinkedException, SlackCommandDoesNotExist, \
SlackInteractivityTypeDoesNotExist
from .models import SlackWorkspace, SlackUserMapping
from .decorators import slack_verify_request
from .settings import SLACK_LOGIN_OAUTH_REDIRECT_URL, SLACK_INSTALL_OAUTH_REDIRECT_URL
Expand Down Expand Up @@ -65,7 +66,14 @@ def slack_login_callback(request):
@require_http_methods(["POST"])
def slack_interactivity(request):
payload = json.loads(request.POST.get('payload'))
fn, required_linked_account = slack_interactivity_callbacks.get(payload.get('type'), None)
payload_type = payload.get("type")
callback = slack_interactivity_callbacks.get(payload_type, None)

if callback is None:
raise SlackInteractivityTypeDoesNotExist(
f"Interactivity type '{payload_type}' is not linked using @slack_interactivity decorator")

fn, required_linked_account = callback
if fn:
if required_linked_account:
try:
Expand Down Expand Up @@ -110,7 +118,12 @@ def get_slack_user_and_workspace(team_id, user_id) -> Tuple[SlackUserMapping, Sl
@require_http_methods(["POST"])
def slack_command(request, name: str):
payload = request.POST
fn, required_linked_account = slack_commands.get(name, None)
callback = slack_commands.get(name, None)

if callback is None:
raise SlackCommandDoesNotExist(f"Command '{name}' is not linked using @slack_command decorator")

fn, required_linked_account = callback
if fn:
if required_linked_account:
try:
Expand Down

0 comments on commit 1714dba

Please sign in to comment.