Skip to content

Commit 0d8d0c4

Browse files
committed
Refactor Django example app
1 parent 1f189bf commit 0d8d0c4

File tree

2 files changed

+130
-115
lines changed

2 files changed

+130
-115
lines changed

examples/django/slackapp/models.py

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
from django.db import models
1+
# ----------------------
2+
# Database tables
3+
# ----------------------
24

5+
from django.db import models
36

47
class SlackBot(models.Model):
58
client_id = models.TextField(null=False)
@@ -54,3 +57,128 @@ class Meta:
5457
class SlackOAuthState(models.Model):
5558
state = models.TextField(null=False)
5659
expire_at = models.DateTimeField(null=False)
60+
61+
62+
# ----------------------
63+
# Bolt store implementations
64+
# ----------------------
65+
66+
67+
from logging import Logger
68+
from typing import Optional
69+
from uuid import uuid4
70+
from django.db.models import F
71+
from django.utils import timezone
72+
from slack_sdk.oauth import InstallationStore, OAuthStateStore
73+
from slack_sdk.oauth.installation_store import Bot, Installation
74+
75+
76+
class DjangoInstallationStore(InstallationStore):
77+
client_id: str
78+
79+
def __init__(
80+
self, client_id: str, logger: Logger,
81+
):
82+
self.client_id = client_id
83+
self._logger = logger
84+
85+
@property
86+
def logger(self) -> Logger:
87+
return self._logger
88+
89+
def save(self, installation: Installation):
90+
i = installation.to_dict()
91+
i["client_id"] = self.client_id
92+
SlackInstallation(**i).save()
93+
b = installation.to_bot().to_dict()
94+
b["client_id"] = self.client_id
95+
SlackBot(**b).save()
96+
97+
def find_bot(
98+
self, *, enterprise_id: Optional[str], team_id: Optional[str]
99+
) -> Optional[Bot]:
100+
rows = (
101+
SlackBot.objects.filter(enterprise_id=enterprise_id)
102+
.filter(team_id=team_id)
103+
.order_by(F("installed_at").desc())[:1]
104+
)
105+
if len(rows) > 0:
106+
b = rows[0]
107+
return Bot(
108+
app_id=b.app_id,
109+
enterprise_id=b.enterprise_id,
110+
team_id=b.team_id,
111+
bot_token=b.bot_token,
112+
bot_id=b.bot_id,
113+
bot_user_id=b.bot_user_id,
114+
bot_scopes=b.bot_scopes,
115+
installed_at=b.installed_at.timestamp(),
116+
)
117+
return None
118+
119+
120+
class DjangoOAuthStateStore(OAuthStateStore):
121+
expiration_seconds: int
122+
123+
def __init__(
124+
self, expiration_seconds: int, logger: Logger,
125+
):
126+
self.expiration_seconds = expiration_seconds
127+
self._logger = logger
128+
129+
@property
130+
def logger(self) -> Logger:
131+
return self._logger
132+
133+
def issue(self) -> str:
134+
state: str = str(uuid4())
135+
expire_at = timezone.now() + timezone.timedelta(seconds=self.expiration_seconds)
136+
row = SlackOAuthState(state=state, expire_at=expire_at)
137+
row.save()
138+
return state
139+
140+
def consume(self, state: str) -> bool:
141+
rows = SlackOAuthState.objects.filter(state=state).filter(
142+
expire_at__gte=timezone.now()
143+
)
144+
if len(rows) > 0:
145+
for row in rows:
146+
row.delete()
147+
return True
148+
return False
149+
150+
# ----------------------
151+
# Slack App
152+
# ----------------------
153+
154+
import logging
155+
import os
156+
from slack_bolt import App
157+
from slack_bolt.oauth.oauth_settings import OAuthSettings
158+
159+
logger = logging.getLogger(__name__)
160+
client_id, client_secret, signing_secret = (
161+
os.environ["SLACK_CLIENT_ID"],
162+
os.environ["SLACK_CLIENT_SECRET"],
163+
os.environ["SLACK_SIGNING_SECRET"],
164+
)
165+
166+
app = App(
167+
signing_secret=signing_secret,
168+
installation_store=DjangoInstallationStore(client_id=client_id, logger=logger,),
169+
oauth_settings=OAuthSettings(
170+
client_id=client_id,
171+
client_secret=client_secret,
172+
state_store=DjangoOAuthStateStore(expiration_seconds=120, logger=logger,),
173+
),
174+
)
175+
176+
177+
@app.event("app_mention")
178+
def event_test(body, say, logger):
179+
logger.info(body)
180+
say("What's up?")
181+
182+
@app.command("/hello-bolt-python")
183+
def command(ack):
184+
ack("This is a Django app!")

examples/django/slackapp/views.py

Lines changed: 1 addition & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,8 @@
1-
import logging
2-
import os
3-
from logging import Logger
4-
from typing import Optional
5-
from uuid import uuid4
6-
7-
from django.db.models import F
8-
from django.db.models.functions import Coalesce
91
from django.http import HttpRequest
10-
from django.utils import timezone
112
from django.views.decorators.csrf import csrf_exempt
12-
from slack_sdk.oauth import InstallationStore, OAuthStateStore
13-
from slack_sdk.oauth.installation_store import Bot, Installation
143

15-
from slack_bolt import App
164
from slack_bolt.adapter.django import SlackRequestHandler
17-
from slack_bolt.oauth.oauth_settings import OAuthSettings
18-
from .models import SlackOAuthState, SlackBot, SlackInstallation
19-
20-
21-
class DjangoInstallationStore(InstallationStore):
22-
client_id: str
23-
24-
def __init__(
25-
self, client_id: str, logger: Logger,
26-
):
27-
self.client_id = client_id
28-
self._logger = logger
29-
30-
@property
31-
def logger(self) -> Logger:
32-
return self._logger
33-
34-
def save(self, installation: Installation):
35-
i = installation.to_dict()
36-
i["client_id"] = self.client_id
37-
SlackInstallation(**i).save()
38-
b = installation.to_bot().to_dict()
39-
b["client_id"] = self.client_id
40-
SlackBot(**b).save()
41-
42-
def find_bot(
43-
self, *, enterprise_id: Optional[str], team_id: Optional[str]
44-
) -> Optional[Bot]:
45-
rows = (
46-
SlackBot.objects.filter(enterprise_id=enterprise_id)
47-
.filter(team_id=team_id)
48-
.order_by(F("installed_at").desc())[:1]
49-
)
50-
if len(rows) > 0:
51-
b = rows[0]
52-
return Bot(
53-
app_id=b.app_id,
54-
enterprise_id=b.enterprise_id,
55-
team_id=b.team_id,
56-
bot_token=b.bot_token,
57-
bot_id=b.bot_id,
58-
bot_user_id=b.bot_user_id,
59-
bot_scopes=b.bot_scopes,
60-
installed_at=b.installed_at.timestamp(),
61-
)
62-
return None
63-
64-
65-
class DjangoOAuthStateStore(OAuthStateStore):
66-
expiration_seconds: int
67-
68-
def __init__(
69-
self, expiration_seconds: int, logger: Logger,
70-
):
71-
self.expiration_seconds = expiration_seconds
72-
self._logger = logger
73-
74-
@property
75-
def logger(self) -> Logger:
76-
return self._logger
77-
78-
def issue(self) -> str:
79-
state: str = str(uuid4())
80-
expire_at = timezone.now() + timezone.timedelta(seconds=self.expiration_seconds)
81-
row = SlackOAuthState(state=state, expire_at=expire_at)
82-
row.save()
83-
return state
84-
85-
def consume(self, state: str) -> bool:
86-
rows = SlackOAuthState.objects.filter(state=state).filter(
87-
expire_at__gte=timezone.now()
88-
)
89-
if len(rows) > 0:
90-
for row in rows:
91-
row.delete()
92-
return True
93-
return False
94-
95-
96-
logger = logging.getLogger(__name__)
97-
client_id, client_secret, signing_secret = (
98-
os.environ["SLACK_CLIENT_ID"],
99-
os.environ["SLACK_CLIENT_SECRET"],
100-
os.environ["SLACK_SIGNING_SECRET"],
101-
)
102-
103-
app = App(
104-
signing_secret=signing_secret,
105-
installation_store=DjangoInstallationStore(client_id=client_id, logger=logger,),
106-
oauth_settings=OAuthSettings(
107-
client_id=client_id,
108-
client_secret=client_secret,
109-
state_store=DjangoOAuthStateStore(expiration_seconds=120, logger=logger,),
110-
),
111-
)
112-
113-
114-
@app.event("app_mention")
115-
def event_test(body, say, logger):
116-
logger.info(body)
117-
say("What's up?")
118-
5+
from .models import app
1196

1207
handler = SlackRequestHandler(app=app)
1218

0 commit comments

Comments
 (0)