1
1
from abc import ABC
2
- from typing import Any , Type
2
+ from typing import TYPE_CHECKING , Any , Type
3
3
4
4
from .channels import EmailChannel , NotificationChannel
5
5
from .frequencies import DailyFrequency , NotificationFrequency , RealtimeFrequency
6
- from .models import DisabledNotificationTypeChannel , EmailFrequency , Notification
7
6
from .registry import registry
8
7
8
+ if TYPE_CHECKING :
9
+ from .models import Notification
10
+
9
11
10
12
class NotificationType (ABC ):
11
13
"""
@@ -22,7 +24,7 @@ def __str__(self) -> str:
22
24
return self .name
23
25
24
26
@classmethod
25
- def should_save (cls , notification : Notification ) -> bool :
27
+ def should_save (cls , notification : " Notification" ) -> bool :
26
28
"""
27
29
A hook to prevent the saving of a new notification. You can use
28
30
this hook to find similar (unread) notifications and then instead
@@ -34,14 +36,14 @@ def should_save(cls, notification: Notification) -> bool:
34
36
"""
35
37
return True
36
38
37
- def get_subject (self , notification : Notification ) -> str :
39
+ def get_subject (self , notification : " Notification" ) -> str :
38
40
"""
39
41
Generate dynamic subject based on notification data.
40
42
Override this in subclasses for custom behavior.
41
43
"""
42
44
return ""
43
45
44
- def get_text (self , notification : Notification ) -> str :
46
+ def get_text (self , notification : " Notification" ) -> str :
45
47
"""
46
48
Generate dynamic text based on notification data.
47
49
Override this in subclasses for custom behavior.
@@ -57,6 +59,7 @@ def set_email_frequency(cls, user: Any, frequency: Type[NotificationFrequency])
57
59
user: The user to set the frequency for
58
60
frequency: NotificationFrequency class
59
61
"""
62
+ from .models import EmailFrequency
60
63
61
64
EmailFrequency .objects .update_or_create (
62
65
user = user , notification_type = cls .key , defaults = {"frequency" : frequency .key }
@@ -73,6 +76,7 @@ def get_email_frequency(cls, user: Any) -> Type[NotificationFrequency]:
73
76
Returns:
74
77
NotificationFrequency class (either user preference or default)
75
78
"""
79
+ from .models import EmailFrequency
76
80
77
81
try :
78
82
user_frequency = EmailFrequency .objects .get (user = user , notification_type = cls .key )
@@ -88,6 +92,7 @@ def reset_email_frequency_to_default(cls, user: Any) -> None:
88
92
Args:
89
93
user: The user to reset the frequency for
90
94
"""
95
+ from .models import EmailFrequency
91
96
92
97
EmailFrequency .objects .filter (user = user , notification_type = cls .key ).delete ()
93
98
@@ -103,6 +108,7 @@ def get_enabled_channels(cls, user: Any) -> list[Type[NotificationChannel]]:
103
108
Returns:
104
109
List of enabled NotificationChannel classes
105
110
"""
111
+ from .models import DisabledNotificationTypeChannel
106
112
107
113
# Get all disabled channel keys for this user/notification type in one query
108
114
disabled_channel_keys = set (
@@ -131,6 +137,7 @@ def is_channel_enabled(cls, user: Any, channel: Type[NotificationChannel]) -> bo
131
137
Returns:
132
138
True if channel is enabled, False if disabled
133
139
"""
140
+ from .models import DisabledNotificationTypeChannel
134
141
135
142
return not DisabledNotificationTypeChannel .objects .filter (
136
143
user = user , notification_type = cls .key , channel = channel .key
@@ -145,6 +152,7 @@ def disable_channel(cls, user: Any, channel: Type[NotificationChannel]) -> None:
145
152
user: User instance
146
153
channel: NotificationChannel class
147
154
"""
155
+ from .models import DisabledNotificationTypeChannel
148
156
149
157
DisabledNotificationTypeChannel .objects .get_or_create (user = user , notification_type = cls .key , channel = channel .key )
150
158
@@ -157,6 +165,7 @@ def enable_channel(cls, user: Any, channel: Type[NotificationChannel]) -> None:
157
165
user: User instance
158
166
channel: NotificationChannel class
159
167
"""
168
+ from .models import DisabledNotificationTypeChannel
160
169
161
170
DisabledNotificationTypeChannel .objects .filter (
162
171
user = user , notification_type = cls .key , channel = channel .key
0 commit comments