forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
61 lines (52 loc) · 2.04 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
from django.db import models
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(max_length=255, blank=True)
notification = models.TextField(blank=True)
sender = models.ForeignKey(UserProfile,
related_name="sent_notifications", blank=True, null=True)
recipient = models.ForeignKey(UserProfile,
related_name="received_notifications")
timestamp = models.DateTimeField(auto_now_add=True)
seen = models.BooleanField(default=False)
course_instance = models.ForeignKey(CourseInstance)
submission = models.ForeignKey(Submission,
related_name="notifications", blank=True, null=True)
class Meta:
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, submission):
for recipient in submission.submitters.all():
if Notification.objects.filter(
submission=submission,
recipient=recipient,
seen=False,
).count() == 0:
notification = Notification(
sender=sender,
recipient=recipient,
course_instance=submission.exercise.course_instance,
submission=submission,
)
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):
return dict(notification_id=self.id, **self.course_instance.get_url_kwargs())