Skip to content

Commit ae5096f

Browse files
committed
use an enum for message type #330 #338
1 parent 26ba0df commit ae5096f

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

mapswipe_workers/mapswipe_workers/definitions.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import logging
21
import logging.config
32
import os
4-
53
import sentry_sdk
64
from xdg import XDG_DATA_HOME
7-
85
from mapswipe_workers.config import SENTRY_DSN
6+
from enum import Enum
97

108

119
class CustomError(Exception):
1210
pass
1311

1412

13+
class MessageType(Enum):
14+
SUCCESS = 1
15+
FAIL = 2
16+
NOTIFICATION_90 = 3
17+
NOTIFICATION_100 = 4
18+
19+
1520
DATA_PATH = os.path.join(XDG_DATA_HOME, "mapswipe_workers")
1621
if not os.path.exists(DATA_PATH):
1722
os.makedirs(DATA_PATH)

mapswipe_workers/mapswipe_workers/mapswipe_workers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import click
1010
from mapswipe_workers import auth
11-
from mapswipe_workers.definitions import CustomError, logger, sentry
11+
from mapswipe_workers.definitions import CustomError, logger, sentry, MessageType
1212
from mapswipe_workers.firebase_to_postgres import (
1313
archive_project,
1414
delete_project,
@@ -87,12 +87,12 @@ def run_create_projects():
8787
project.calc_required_results()
8888
# Save project and its groups and tasks to Firebase and Postgres.
8989
project.save_project()
90-
send_slack_message("success", project_name, project.projectId)
90+
send_slack_message(MessageType.SUCCESS, project_name, project.projectId)
9191
logger.info("Success: Project Creation ({0})".format(project_name))
9292
except CustomError:
9393
ref = fb_db.reference(f"v2/projectDrafts/{project_draft_id}")
9494
ref.set({})
95-
send_slack_message("fail", project_name, project.projectId)
95+
send_slack_message(MessageType.FAIL, project_name, project.projectId)
9696
logger.exception("Failed: Project Creation ({0}))".format(project_name))
9797
sentry.capture_exception()
9898
continue

mapswipe_workers/mapswipe_workers/utils/slack_helper.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
from mapswipe_workers.definitions import logger
33
from mapswipe_workers import auth
44
from mapswipe_workers.config import SLACK_CHANNEL, SLACK_TOKEN
5+
from mapswipe_workers.definitions import MessageType
56

67

7-
def send_slack_message(message_type: str, project_name: str, project_id: str = None):
8+
def send_slack_message(
9+
message_type: MessageType, project_name: str, project_id: str = None
10+
):
811
"""Initialize slack client with values provided in environment."""
912
if SLACK_TOKEN is None or SLACK_CHANNEL is None:
1013
logger.info(
@@ -15,7 +18,7 @@ def send_slack_message(message_type: str, project_name: str, project_id: str = N
1518

1619
slack_client = slack.WebClient(token=SLACK_TOKEN)
1720

18-
if message_type == "success":
21+
if message_type == MessageType.SUCCESS:
1922
message = (
2023
"### PROJECT CREATION SUCCESSFUL ###\n"
2124
+ f"Project Name: {project_name}\n"
@@ -24,22 +27,22 @@ def send_slack_message(message_type: str, project_name: str, project_id: str = N
2427
+ "Happy Swiping. :)"
2528
)
2629
slack_client.chat_postMessage(channel=SLACK_CHANNEL, text=message)
27-
elif message_type == "fail":
30+
elif message_type == MessageType.FAIL:
2831
message = (
2932
"### PROJECT CREATION FAILED ###\n"
3033
+ f"Project Name: {project_name}\n"
3134
+ "Project draft is deleted."
3235
)
3336
slack_client.chat_postMessage(channel=SLACK_CHANNEL, text=message)
34-
elif message_type == "notification_90":
37+
elif message_type == MessageType.NOTIFICATION_90:
3538
message = (
3639
"### ALMOST THERE! PROJECT REACHED 90% ###\n"
3740
+ f"Project Name: {project_name}\n"
3841
+ f"Project Id: {project_id}\n\n"
3942
+ "Get your next projects ready."
4043
)
4144
slack_client.chat_postMessage(channel="mapswipe_managers", text=message)
42-
elif message_type == "notification_100":
45+
elif message_type == MessageType.NOTIFICATION_100:
4346
message = (
4447
"### GREAT! PROJECT REACHED 100% ###\n"
4548
+ f"Project Name: {project_name}\n"
@@ -73,10 +76,10 @@ def send_progress_notification(project_id: int):
7376

7477
if progress >= 90 and not notification_90_sent:
7578
# send notification and set value in firebase
76-
send_slack_message("notification_90", project_name, project_id)
79+
send_slack_message(MessageType.NOTIFICATION_90, project_name, project_id)
7780
fb_db.reference(f"v2/projects/{project_id}/notification_90_sent").set(True)
7881

7982
if progress >= 100 and not notification_100_sent:
8083
# send notification and set value in firebase
81-
send_slack_message("notification_100", project_name, project_id)
84+
send_slack_message(MessageType.NOTIFICATION_100, project_name, project_id)
8285
fb_db.reference(f"v2/projects/{project_id}/notification_100_sent").set(True)

0 commit comments

Comments
 (0)