candale icon

Notification Problem | Python

candale | PRO | 06/10/21 07:39:24 AM UTC (Edited) | 0 ⭐ | 229 👁️ | Never ⏰ | []
Python |

3.01 KB

|

None

|

0 👍

/

0 👎

"""
We have a bunch of objects and a bunch of methods that manipulate the objects.
For each object manipulation there is a corresponding Activy that's created.
The activity has a kind and additional information, depending on the type of
activity.
There are also three methods of notifying people about activities; the
ones currently available are email, sms and push notification but other could
be added at any point. Each of the notifications may require more or less
verbosity, depending on the medium.
 
Requirement:
Build on the code below such that for every action in the main function block
(user/issue/task creation, or issue assignment), an activity is
created with the appropriate kind. Also, for every activity created, an appropriate
notification is sent through the appropriate mediums to all available users.
Is it desirable that new notification mediums can be easily added and the texts
can be easily tailored based on the medium and activity; for example an SMS might include a
lot less information than an email. That is to say, for a certain activity,
the notification text that's sent by email may differ from the one send by SMS;
some users may only use certain notification mediums, not all
of the available ones.
 
If we take the example of creating a user, the notification will look like this:
 
SMS
A new user was created. Go to https://bitly.is/3g65Hus to find out more
 
EMAIL
A new user was created with username {username} on the date {date}. The user
has registered with email address {email}
 
PUSH NOTIFICATION
Checkout the new user that was created: https://bitly.is/3g65Hus
"""
 
from dataclasses import dataclass
import enum
 
 
def send_email(emails: list[str], text: str):
    print(f"EMAIL TO: {emails.join(',')} | {text}")
 
 
def send_sms(emails: list[str], text: str):
    print(f"SMS TO: {emails.join(',')} | {text}")
 
 
def push_notification(emails: list[str], text: str):
    print(f"PUSH NOTIFICATION TO: {emails.join(',')} | {text}")
 
 
class ActivityKind(enum.Enum):
 
    USER_CREATED = 1
    ISSUE_CREATED = 2
    TASK_CREATED = 3
    TASK_ASSIGNED = 4
 
 
@dataclass
class User:
 
    id: int
    username: str
    email: str
 
 
@dataclass
class Issue:
 
    id: int
    title: str
    reporter: User
    description: str
 
 
@dataclass
class Task:
 
    id: int
    title: str
    created_by: User
    issue_id: int
    assignee: User
 
 
@dataclass
class Activity:
 
    kind: ActivityKind
    additional_context: dict
 
 
def main():
    john = User(id=1, username='cooljohn93', email='[email protected]')
    anna = User(id=1, username='anna', email='[email protected]')
    dave = User(id=1, username='dave', email='[email protected]')
 
    network_issue = Issue(reporter=john, id=1, title='Network not working', description='Does not work on my machine')
    task = Task(id=1, title='Fix network err', issue_id=network_issue.id, created_by=anna, assignee=dave)
 
    # assign a task
    task.assignee = john
 
 
if __name__ == '__main__':
    main()
 

Comments