-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathviews.py
More file actions
50 lines (41 loc) · 1.68 KB
/
views.py
File metadata and controls
50 lines (41 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import requests
from django.conf import settings
from django.shortcuts import redirect, render
from django.utils.translation import ugettext as _
from .forms import SlackInviteForm
MISSING_SCOPE_ERROR_TEXT = """Missing admin scope: The token you provided is for
an account that is not an admin. You must provide a token from an admin account
in order to invite users through the Slack API."""
ALREADY_INVITED_ERROR_TEXT = """You have already been invited to Slack. Check
for an email from [email protected]."""
def slack_invite(request):
if request.method == 'POST':
form = SlackInviteForm(request.POST)
if form.is_valid():
response = requests.post(
"https://{}.slack.com/api/users.admin.invite".format(settings.SLACK_ORG),
data={
'email': form.cleaned_data['email'],
'token': settings.SLACK_API_TOKEN,
},
)
body = response.json()
ok = body['ok']
if ok:
pass
else:
error = body['error']
if error == 'missing_scope':
error_text = MISSING_SCOPE_ERROR_TEXT
elif error == 'already_invited':
error_text = ALREADY_INVITED_ERROR_TEXT
elif error == 'already_in_team':
return redirect('https://{}.slack.com'.format(
settings.SLACK_ORG,
))
else:
error_text = error
form.add_error(None, _(error_text))
else:
form = SlackInviteForm()
return render(request, 'slack/invite.html', {'form': form})