Skip to content

Commit a32b35f

Browse files
committed
Refactoring slack channel creation
1 parent 3144158 commit a32b35f

10 files changed

Lines changed: 89 additions & 145 deletions

docker-compose.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ services:
2525

2626
environment:
2727
- ENV_FILE=/hackathon-app/.env
28+
- DEVELOPMENT=1
2829
- STATIC_URL=https://codeinstitute-webpublic.s3.eu-west-1.amazonaws.com/hackathon_staticfiles/1.62-a/
2930
entrypoint: ['python3', 'manage.py', 'runserver', '0.0.0.0:8000']
3031
ports:
@@ -37,11 +38,30 @@ services:
3738
environment:
3839
- ENV_FILE=/hackathon-app/.env
3940
- DEVELOPMENT=1
41+
- STATIC_URL=https://codeinstitute-webpublic.s3.eu-west-1.amazonaws.com/hackathon_staticfiles/1.62-a/
4042
entrypoint: ["celery", "-A", "main", "worker", "-l", "info"]
4143
volumes:
44+
- ./staticfiles/:/hackathon-app/staticfiles/
4245
- ./data/:/hackathon-app/data/
4346
- ./.env:/hackathon-app/.env
4447

48+
# code
49+
- ./accounts/:/hackathon-app/accounts/
50+
- ./competencies/:/hackathon-app/competencies/
51+
- ./custom_slack_provider/:/hackathon-app/custom_slack_provider/
52+
- ./hackadmin/:/hackathon-app/hackadmin/
53+
- ./hackathon/:/hackathon-app/hackathon/
54+
- ./home/:/hackathon-app/home/
55+
- ./images/:/hackathon-app/images/
56+
- ./main/:/hackathon-app/main/
57+
- ./profiles/:/hackathon-app/profiles/
58+
- ./resources/:/hackathon-app/resources/
59+
- ./showcase/:/hackathon-app/showcase/
60+
- ./submissions/:/hackathon-app/submissions/
61+
- ./teams/:/hackathon-app/teams/
62+
- ./templates/:/hackathon-app/templates/
63+
- ./static/:/hackathon-app/static/
64+
4565
mysql:
4666
image: docker.io/mysql:5.6.36
4767
command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci

hackathon/migrations/0047_hackathon_channel_prefix.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

hackathon/migrations/0048_hackathon_channel_url.py

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Generated by Django 3.1.13 on 2023-01-23 12:19
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
11+
('hackathon', '0048_auto_20221219_1655'),
12+
]
13+
14+
operations = [
15+
migrations.AddField(
16+
model_name='hackathon',
17+
name='channel_admins',
18+
field=models.ManyToManyField(blank=True, related_name='administered_channels', to=settings.AUTH_USER_MODEL),
19+
),
20+
migrations.AddField(
21+
model_name='hackathon',
22+
name='channel_name',
23+
field=models.CharField(blank=True, help_text="Only use lowercase and dash ('-') for spaces", max_length=255, null=True),
24+
),
25+
migrations.AddField(
26+
model_name='hackathon',
27+
name='channel_url',
28+
field=models.CharField(blank=True, help_text='Url', max_length=255, null=True),
29+
),
30+
]

hackathon/migrations/0049_hackathon_channel_admins.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

hackathon/migrations/0050_auto_20220120_1052.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

hackathon/migrations/0051_auto_20220120_1053.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

hackathon/migrations/0052_auto_20220126_1049.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

teams/helpers.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,21 +234,48 @@ def calculate_timezone_offset(timezone, timezone_offset):
234234
return offset - timezone_offset
235235

236236

237+
def create_slack_channel(endpoint, headers, params):
238+
create_response = requests.get(endpoint, params=params, headers=headers)
239+
if create_response.status_code != 200:
240+
return {
241+
'ok': False,
242+
'error': ('An error occurred creating the Private Slack Channel. '
243+
f'Error code: {create_response.get("error")}')
244+
}
245+
246+
create_response = create_response.json()
247+
if not create_response.get('ok'):
248+
if create_response.get('error') == 'name_taken':
249+
error_msg = (f'An error occurred creating the Private Slack Channel. '
250+
f'A channel with the name "{params["name"]}" already '
251+
f'exists. Please change your team name and try again '
252+
f'or contact an administrator')
253+
else:
254+
error_msg = (f'An error occurred creating the Private Slack Channel. '
255+
f'Error code: {create_response.get("error")}')
256+
return {
257+
'ok': False,
258+
'error': error_msg
259+
}
260+
261+
return create_response
262+
263+
237264
def invite_users_to_slack_channel(endpoint, headers, params):
238265
response = requests.post(endpoint, params=params, headers=headers)
239-
if not response.status_code == 200:
266+
if response.status_code != 200:
240267
return {
241268
'ok': False,
242269
'error': ('An unexpected error occurred creating the '
243270
'Private Slack Channel.')
244271
}
245-
272+
246273
response = response.json()
247274
if not response.get('ok'):
248275
return {
249276
'ok': False,
250277
'error': ('An error occurred creating the Private Slack Channel. '
251278
f'Error code: {response.get("error")}')
252279
}
253-
280+
254281
return {'ok': True, 'response': response}

teams/views.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
choose_team_levels, find_all_combinations,
2121
distribute_participants_to_teams,
2222
create_teams_in_view, update_team_participants,
23-
calculate_timezone_offset, invite_users_to_slack_channel)
23+
calculate_timezone_offset, invite_users_to_slack_channel,
24+
create_slack_channel)
2425
from teams.forms import HackProjectForm, EditTeamName
2526

2627
SLACK_CHANNEL_ENDPOINT = 'https://slack.com/api/conversations.create'
@@ -246,9 +247,9 @@ def create_private_channel(request, team_id):
246247
return redirect(reverse('view_team', kwargs={'team_id': team_id}))
247248

248249
# Create new channel
249-
date_str = datetime.now().strftime('%y%m')
250+
date_str = datetime.now().strftime('%b-%y').lower()
250251
team_name = re.sub('[^A-Za-z0-9]+', '', team.display_name.lower())
251-
channel_name = f'{date_str}-hackathon-{team_name}'
252+
channel_name = f'{date_str}-hackathon-{team.hackathon.id}-{team_name}'
252253
params = {
253254
'team_id': settings.SLACK_TEAM_ID,
254255
'name': channel_name,
@@ -257,27 +258,12 @@ def create_private_channel(request, team_id):
257258
# Cannot use Bot Token to create a channel if workspace settings
258259
# specify only Admins and Owners can create channels
259260
headers = {'Authorization': f'Bearer {settings.SLACK_ADMIN_TOKEN}'}
260-
create_response = requests.get(SLACK_CHANNEL_ENDPOINT, params=params,
261-
headers=headers)
262-
if not create_response.status_code == 200:
263-
messages.error(request, (f'An error occurred creating the Private Slack Channel. '
264-
f'Error code: {create_response.get("error")}'))
265-
return redirect(reverse('view_team', kwargs={'team_id': team_id}))
266-
267-
create_response = create_response.json()
268-
if not create_response.get('ok'):
269-
if create_response.get('error') == 'name_taken':
270-
error_msg = (f'An error occurred creating the Private Slack Channel. '
271-
f'A channel with the name "{channel_name}" already '
272-
f'exists. Please change your team name and try again '
273-
f'or contact an administrator')
274-
else:
275-
error_msg = (f'An error occurred creating the Private Slack Channel. '
276-
f'Error code: {create_response.get("error")}')
277-
messages.error(request, error_msg)
261+
channel_response = create_slack_channel(SLACK_CHANNEL_ENDPOINT, headers, params)
262+
if not response['ok']:
263+
messages.error(request, response['error'])
278264
return redirect(reverse('view_team', kwargs={'team_id': team_id}))
279-
280-
channel = create_response.get('channel', {}).get('id')
265+
266+
channel = channel_response.get('channel', {}).get('id')
281267
communication_channel = (f'https://{settings.SLACK_WORKSPACE}.slack.com/'
282268
f'app_redirect?channel={channel}')
283269
team.communication_channel = communication_channel

0 commit comments

Comments
 (0)