candale

Notification Problem | Python

Jun 9th, 2021 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. """
  2. We have a bunch of objects and a bunch of methods that manipulate the objects.
  3. For each object manipulation there is a corresponding Activy that's created.
  4. The activity has a kind and additional information, depending on the type of
  5. activity.
  6. There are also three methods of notifying people about activities; the
  7. ones currently available are email, sms and push notification but other could
  8. be added at any point. Each of the notifications may require more or less
  9. verbosity, depending on the medium.
  10.  
  11. Requirement:
  12. Build on the code below such that for every action in the main function block
  13. (user/issue/task creation, or issue assignment), an activity is
  14. created with the appropriate kind. Also, for every activity created, an appropriate
  15. notification is sent through the appropriate mediums to all available users.
  16. Is it desirable that new notification mediums can be easily added and the texts
  17. can be easily tailored based on the medium and activity; for example an SMS might include a
  18. lot less information than an email. That is to say, for a certain activity,
  19. the notification text that's sent by email may differ from the one send by SMS;
  20. some users may only use certain notification mediums, not all
  21. of the available ones.
  22.  
  23. If we take the example of creating a user, the notification will look like this:
  24.  
  25. SMS
  26. A new user was created. Go to https://bitly.is/3g65Hus to find out more
  27.  
  28. EMAIL
  29. A new user was created with username {username} on the date {date}. The user
  30. has registered with email address {email}
  31.  
  32. PUSH NOTIFICATION
  33. Checkout the new user that was created: https://bitly.is/3g65Hus
  34. """
  35.  
  36. from dataclasses import dataclass
  37. import enum
  38.  
  39.  
  40. def send_email(emails: list[str], text: str):
  41.     print(f"EMAIL TO: {emails.join(',')} | {text}")
  42.  
  43.  
  44. def send_sms(emails: list[str], text: str):
  45.     print(f"SMS TO: {emails.join(',')} | {text}")
  46.  
  47.  
  48. def push_notification(emails: list[str], text: str):
  49.     print(f"PUSH NOTIFICATION TO: {emails.join(',')} | {text}")
  50.  
  51.  
  52. class ActivityKind(enum.Enum):
  53.  
  54.     USER_CREATED = 1
  55.     ISSUE_CREATED = 2
  56.     TASK_CREATED = 3
  57.     TASK_ASSIGNED = 4
  58.  
  59.  
  60. @dataclass
  61. class User:
  62.  
  63.     id: int
  64.     username: str
  65.     email: str
  66.  
  67.  
  68. @dataclass
  69. class Issue:
  70.  
  71.     id: int
  72.     title: str
  73.     reporter: User
  74.     description: str
  75.  
  76.  
  77. @dataclass
  78. class Task:
  79.  
  80.     id: int
  81.     title: str
  82.     created_by: User
  83.     issue_id: int
  84.     assignee: User
  85.  
  86.  
  87. @dataclass
  88. class Activity:
  89.  
  90.     kind: ActivityKind
  91.     additional_context: dict
  92.  
  93.  
  94. def main():
  95.     john = User(id=1, username='cooljohn93', email='john93@gmail.com')
  96.     anna = User(id=1, username='anna', email='anna@yahoo.com')
  97.     dave = User(id=1, username='dave', email='dave@gmail.com')
  98.  
  99.     network_issue = Issue(reporter=john, id=1, title='Network not working', description='Does not work on my machine')
  100.     task = Task(id=1, title='Fix network err', issue_id=network_issue.id, created_by=anna, assignee=dave)
  101.  
  102.     # assign a task
  103.     task.assignee = john
  104.  
  105.  
  106. if __name__ == '__main__':
  107.     main()
  108.  
Add Comment
Please, Sign In to add comment