-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathnotifications.py
More file actions
52 lines (38 loc) · 1.31 KB
/
notifications.py
File metadata and controls
52 lines (38 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import enum
from typing import Iterable, Optional
from lms.lmsdb.models import MailMessage, Notification, User
class NotificationKind(enum.Enum):
CHECKED = 1
FLAKE8_ERROR = 2
UNITTEST_ERROR = 3
USER_RESPONSE = 4
def get(user: User) -> Iterable[Notification]:
return Notification.fetch(user)
def read(user: Optional[User] = None, id_: Optional[int] = None) -> bool:
if id_:
try:
notification = Notification.get_by_id(id_)
except Notification.DoesNotExist:
return False
if user and (user.id != notification.user.id):
return False
notification.read()
return True
assert user, 'Must provide user or id_' # noqa: B101, S101
is_success = [msg.read() for msg in get(user)]
return all(is_success) # Not gen to prevent lazy evaluation
def read_related(related_id: int, user: int):
for n in Notification.of(related_id, user):
n.read()
def send(
user: User,
kind: NotificationKind,
message: str,
related_id: Optional[int] = None,
action_url: Optional[str] = None,
) -> Notification:
notification = Notification.send(
user, kind.value, message, related_id, action_url,
)
MailMessage.create(user=user, notification=notification)
return notification