-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathmodels.py
106 lines (88 loc) · 3.66 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
98
99
100
101
102
103
104
105
106
from django.conf import settings
try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls import reverse
from django.db import models
from django.db.models import signals
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
class MessageManager(models.Manager):
def inbox_for(self, user):
"""
Returns all messages that were received by the given user and are not
marked as deleted.
"""
return self.filter(
recipient=user,
recipient_deleted_at__isnull=True,
)
def outbox_for(self, user):
"""
Returns all messages that were sent by the given user and are not
marked as deleted.
"""
return self.filter(
sender=user,
sender_deleted_at__isnull=True,
)
def trash_for(self, user):
"""
Returns all messages that were either received or sent by the given
user and are marked as deleted.
"""
return self.filter(
recipient=user,
recipient_deleted_at__isnull=False,
) | self.filter(
sender=user,
sender_deleted_at__isnull=False,
)
class Message(models.Model):
"""
A private message from user to user
"""
subject = models.CharField(_("Subject"), max_length=140)
body = models.TextField(_("Body"))
sender = models.ForeignKey(AUTH_USER_MODEL, related_name='sent_messages', verbose_name=_("Sender"), on_delete=models.PROTECT)
recipient = models.ForeignKey(AUTH_USER_MODEL, related_name='received_messages', null=True, blank=True, verbose_name=_("Recipient"), on_delete=models.SET_NULL)
parent_msg = models.ForeignKey('self', related_name='next_messages', null=True, blank=True, verbose_name=_("Parent message"), on_delete=models.SET_NULL)
sent_at = models.DateTimeField(_("sent at"), null=True, blank=True)
read_at = models.DateTimeField(_("read at"), null=True, blank=True)
replied_at = models.DateTimeField(_("replied at"), null=True, blank=True)
sender_deleted_at = models.DateTimeField(_("Sender deleted at"), null=True, blank=True)
recipient_deleted_at = models.DateTimeField(_("Recipient deleted at"), null=True, blank=True)
objects = MessageManager()
def new(self):
"""returns whether the recipient has read the message or not"""
if self.read_at is not None:
return False
return True
def replied(self):
"""returns whether the recipient has written a reply to this message"""
if self.replied_at is not None:
return True
return False
def __str__(self):
return self.subject
def get_absolute_url(self):
return reverse('messages_detail', args=[self.id])
def save(self, **kwargs):
if not self.id:
self.sent_at = timezone.now()
super(Message, self).save(**kwargs)
class Meta:
ordering = ['-sent_at']
verbose_name = _("Message")
verbose_name_plural = _("Messages")
def inbox_count_for(user):
"""
returns the number of unread messages for the given user but does not
mark them seen
"""
return Message.objects.filter(recipient=user, read_at__isnull=True, recipient_deleted_at__isnull=True).count()
# fallback for email notification if django-notification could not be found
if "pinax.notifications" not in settings.INSTALLED_APPS and getattr(settings, 'DJANGO_MESSAGES_NOTIFY', True):
from django_messages.utils import new_message_email
signals.post_save.connect(new_message_email, sender=Message)