-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathmodels.py
97 lines (88 loc) · 3.07 KB
/
models.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from django.db import models
from django.utils.translation import gettext_lazy as _
from course.models import CourseInstance
from exercise.models import Submission
from lib.models import UrlMixin
from userprofile.models import UserProfile
class Notification(UrlMixin, models.Model):
"""
A user notification of some event, for example manual assessment.
"""
subject = models.CharField(
verbose_name=_('LABEL_SUBJECT'),
max_length=255,
blank=True,
)
notification = models.TextField(
verbose_name=_('LABEL_NOTIFICATION'),
blank=True,
)
sender = models.ForeignKey(UserProfile,
verbose_name=_('LABEL_SENDER'),
on_delete=models.SET_NULL,
related_name="sent_notifications",
blank=True, null=True,
)
recipient = models.ForeignKey(UserProfile,
verbose_name=_('LABEL_RECIPIENT'),
on_delete=models.CASCADE,
related_name="received_notifications",
)
timestamp = models.DateTimeField(
verbose_name=_('LABEL_TIMESTAMP'),
auto_now_add=True,
)
seen = models.BooleanField(
verbose_name=_('LABEL_SEEN'),
default=False,
)
regrade_when_seen = models.BooleanField(
verbose_name=_('LABEL_REGRADE_WHEN_SEEN'),
default=False,
)
course_instance = models.ForeignKey(CourseInstance,
verbose_name=_('LABEL_COURSE_INSTANCE'),
on_delete=models.CASCADE,
)
submission = models.ForeignKey(Submission,
verbose_name=_('LABEL_SUBMISSION'),
on_delete=models.CASCADE,
related_name="notifications",
blank=True, null=True,
)
class Meta:
verbose_name = _('MODEL_NAME_NOTIFICATION')
verbose_name_plural = _('MODEL_NAME_NOTIFICATION_PLURAL')
ordering = ['-timestamp']
def __str__(self):
return (
"To:" + self.recipient.user.username + ", "
+ (str(self.submission.exercise) if self.submission else self.subject)
)
@classmethod
def send(cls, sender: UserProfile, submission: Submission, regrade_when_seen: bool = False) -> None:
for recipient in submission.submitters.all():
if not Notification.objects.filter(
submission=submission,
recipient=recipient,
seen=False,
).exists():
notification = Notification(
sender=sender,
recipient=recipient,
course_instance=submission.exercise.course_instance,
submission=submission,
regrade_when_seen=regrade_when_seen
)
notification.save()
@classmethod
def remove(cls, submission):
Notification.objects.filter(
submission=submission,
recipient__in=submission.submitters.all(),
seen=False,
).delete()
ABSOLUTE_URL_NAME = "notify"
def get_url_kwargs(self):
# pylint: disable-next=use-dict-literal
return dict(notification_id=self.id, **self.course_instance.get_url_kwargs())