This function takes a notification class and arguments to instantiate the notification class in a task with the original arguments. It converts instances of models into a JSON object that allows us to query the rows inside the task so the interface is just simple JSON.
(
NotificationClass: type[BaseNotification], *args: Any, **kwargs: Any
)
| 69 | |
| 70 | @cell_silo_function |
| 71 | def async_send_notification( |
| 72 | NotificationClass: type[BaseNotification], *args: Any, **kwargs: Any |
| 73 | ) -> None: |
| 74 | """ |
| 75 | This function takes a notification class and arguments to instantiate |
| 76 | the notification class in a task with the original arguments. |
| 77 | It converts instances of models into a JSON object that allows us to query |
| 78 | the rows inside the task so the interface is just simple JSON. |
| 79 | """ |
| 80 | class_name = getattr(NotificationClass, "__name__") |
| 81 | if not get(class_name): |
| 82 | raise NotificationClassNotSetException() |
| 83 | |
| 84 | task_args = [] |
| 85 | for arg in args: |
| 86 | if isinstance(arg, Model): |
| 87 | task_args.append(serialize_model(arg)) |
| 88 | elif isinstance(arg, SimpleLazyObject): |
| 89 | task_args.append(serialize_lazy_object_user(arg)) |
| 90 | elif isinstance(arg, AnonymousUser): |
| 91 | task_args.append(serialize_anonymous_user(arg)) |
| 92 | # maybe we need an explicit check if it's a primitive? |
| 93 | else: |
| 94 | task_args.append({"type": "other", "value": arg, "key": None}) |
| 95 | for key, val in kwargs.items(): |
| 96 | if isinstance(val, Model): |
| 97 | task_args.append(serialize_model(val, key)) |
| 98 | elif isinstance(val, SimpleLazyObject): |
| 99 | task_args.append(serialize_lazy_object_user(val, key)) |
| 100 | elif isinstance(val, AnonymousUser): |
| 101 | task_args.append(serialize_anonymous_user(val, key)) |
| 102 | # maybe we need an explicit check if it's a primitive? |
| 103 | else: |
| 104 | task_args.append({"type": "other", "value": val, "key": key}) |
| 105 | |
| 106 | _send_notification.delay(class_name, task_args) |
| 107 | |
| 108 | |
| 109 | @instrumented_task( |