4646labels = get_labels ()
4747
4848
49+ def get_manager_class_instance ():
50+ default_manager = NotificationManager
51+ notification_manager_class = default_manager
52+ if isinstance (
53+ (
54+ notification_manager := getattr (
55+ settings ,
56+ "NOTIFICATION_MANAGER" ,
57+ default_manager ,
58+ )
59+ ),
60+ str ,
61+ ):
62+ with suppress (ModuleNotFoundError ):
63+ module_name , _separator , class_name = notification_manager .rpartition ("." )
64+ module = importlib .import_module (module_name )
65+ notification_manager_class = getattr (module , class_name )
66+ return notification_manager_class ()
67+
68+
4969def create_notification (
5070 event : str | None = None ,
5171 title : str | None = None ,
@@ -63,23 +83,7 @@ def create_notification(
6383 ** kwargs : dict ,
6484) -> None :
6585 """Create an instance of a NotificationManager and dispatch the notification."""
66- default_manager = NotificationManager
67- notification_manager_class = default_manager
68- if isinstance (
69- (
70- notification_manager := getattr (
71- settings ,
72- "NOTIFICATION_MANAGER" ,
73- default_manager ,
74- )
75- ),
76- str ,
77- ):
78- with suppress (ModuleNotFoundError ):
79- module_name , _separator , class_name = notification_manager .rpartition ("." )
80- module = importlib .import_module (module_name )
81- notification_manager_class = getattr (module , class_name )
82- notification_manager_class ().create_notification (
86+ get_manager_class_instance ().create_notification (
8387 event = event ,
8488 title = title ,
8589 finding = finding ,
@@ -870,30 +874,30 @@ def _process_notifications(
870874@app .task
871875def send_slack_notification (event : str , user_id : int | None = None , ** kwargs : dict ) -> None :
872876 user = Dojo_User .objects .get (pk = user_id ) if user_id else None
873- SlackNotificationManger ( ).send_slack_notification (event , user = user , ** kwargs )
877+ get_manager_class_instance (). _get_manager_instance ( "slack" ).send_slack_notification (event , user = user , ** kwargs )
874878
875879
876880@app .task
877881def send_msteams_notification (event : str , user_id : int | None = None , ** kwargs : dict ) -> None :
878882 user = Dojo_User .objects .get (pk = user_id ) if user_id else None
879- MSTeamsNotificationManger ( ).send_msteams_notification (event , user = user , ** kwargs )
883+ get_manager_class_instance (). _get_manager_instance ( "msteams" ).send_msteams_notification (event , user = user , ** kwargs )
880884
881885
882886@app .task
883887def send_mail_notification (event : str , user_id : int | None = None , ** kwargs : dict ) -> None :
884888 user = Dojo_User .objects .get (pk = user_id ) if user_id else None
885- EmailNotificationManger ( ).send_mail_notification (event , user = user , ** kwargs )
889+ get_manager_class_instance (). _get_manager_instance ( "mail" ).send_mail_notification (event , user = user , ** kwargs )
886890
887891
888892@app .task
889893def send_webhooks_notification (event : str , user_id : int | None = None , ** kwargs : dict ) -> None :
890894 user = Dojo_User .objects .get (pk = user_id ) if user_id else None
891- WebhookNotificationManger ( ).send_webhooks_notification (event , user = user , ** kwargs )
895+ get_manager_class_instance (). _get_manager_instance ( "webhooks" ).send_webhooks_notification (event , user = user , ** kwargs )
892896
893897
894898@app .task (ignore_result = True )
895899def webhook_reactivation (endpoint_id : int , ** _kwargs : dict ) -> None :
896- WebhookNotificationManger ( )._webhook_reactivation (endpoint_id = endpoint_id )
900+ get_manager_class_instance (). _get_manager_instance ( "webhooks" )._webhook_reactivation (endpoint_id = endpoint_id )
897901
898902
899903@app .task (ignore_result = True )
0 commit comments