gremmie@1
|
1 """Models for the messages application."""
|
gremmie@1
|
2
|
gremmie@1
|
3 import datetime
|
gremmie@1
|
4 from django.db import models
|
gremmie@1
|
5 from django.db.models import Q
|
gremmie@1
|
6 from django.contrib.auth.models import User
|
bgneal@124
|
7 from core.markup import markdown
|
bgneal@124
|
8 from smiley import smilify
|
gremmie@1
|
9
|
gremmie@1
|
10
|
gremmie@1
|
11 class MessageManager(models.Manager):
|
gremmie@1
|
12 """The manager for the Message model. Provides convenience functions."""
|
gremmie@1
|
13
|
gremmie@1
|
14 def inbox(self, user):
|
gremmie@1
|
15 return self.filter(receiver=user,
|
gremmie@1
|
16 receiver_delete_date__isnull=True)
|
gremmie@1
|
17
|
gremmie@1
|
18 def outbox(self, user):
|
gremmie@1
|
19 return self.filter(sender=user,
|
gremmie@1
|
20 sender_delete_date__isnull=True)
|
gremmie@1
|
21
|
gremmie@1
|
22 def trash(self, user):
|
gremmie@1
|
23 return self.filter(
|
gremmie@1
|
24 Q(sender=user, sender_delete_date__isnull=False) |
|
gremmie@1
|
25 Q(receiver=user, receiver_delete_date__isnull=False)
|
gremmie@1
|
26 )
|
gremmie@1
|
27
|
gremmie@1
|
28 def unread_count(self, user):
|
gremmie@1
|
29 return self.filter(receiver=user, read_date__isnull=True).count()
|
gremmie@1
|
30
|
gremmie@1
|
31
|
gremmie@1
|
32 class Message(models.Model):
|
gremmie@1
|
33 """The Message is a model for a private message between users."""
|
gremmie@1
|
34 sender = models.ForeignKey(User, related_name='sender')
|
gremmie@1
|
35 receiver = models.ForeignKey(User, related_name='receiver')
|
gremmie@1
|
36 send_date = models.DateTimeField('Date Sent', null=True, blank=True)
|
gremmie@1
|
37 read_date = models.DateTimeField('Date Received', null=True, blank=True)
|
gremmie@1
|
38 reply_date = models.DateTimeField('Date Replied', null=True, blank=True)
|
gremmie@1
|
39 subject = models.CharField(max_length=120)
|
gremmie@1
|
40 message = models.TextField()
|
gremmie@1
|
41 html = models.TextField()
|
gremmie@1
|
42 sender_delete_date = models.DateTimeField(null=True, blank=True)
|
gremmie@1
|
43 receiver_delete_date = models.DateTimeField(null=True, blank=True)
|
gremmie@1
|
44 signature_attached = models.BooleanField(default=False)
|
gremmie@1
|
45
|
gremmie@1
|
46 objects = MessageManager()
|
gremmie@1
|
47
|
gremmie@1
|
48 def unread(self):
|
gremmie@1
|
49 """returns True if the message has not been read yet"""
|
gremmie@1
|
50 return self.read_date is None
|
gremmie@1
|
51
|
gremmie@1
|
52 def replied_to(self):
|
gremmie@1
|
53 """returns True if the message has been replied to"""
|
gremmie@1
|
54 return self.reply_date is not None
|
gremmie@1
|
55
|
gremmie@1
|
56 def is_deleted(self, user):
|
gremmie@1
|
57 """returns True if the message has been deleted by the user"""
|
gremmie@1
|
58 if (user == self.sender and self.sender_delete_date is not None) or (
|
gremmie@1
|
59 user == self.receiver and self.receiver_delete_date is not None):
|
gremmie@1
|
60 return True
|
gremmie@1
|
61 return False
|
gremmie@1
|
62
|
gremmie@1
|
63 def save(self, force_insert = False, force_update = False):
|
gremmie@1
|
64 if not self.id:
|
gremmie@1
|
65 self.send_date = datetime.datetime.now()
|
bgneal@124
|
66 self.html = smilify(markdown(self.message))
|
gremmie@1
|
67 super(Message, self).save(force_insert, force_update)
|
gremmie@1
|
68
|
gremmie@1
|
69 @models.permalink
|
gremmie@1
|
70 def get_absolute_url(self):
|
gremmie@1
|
71 return ('messages.views.view', [str(self.id)])
|
gremmie@1
|
72
|
gremmie@1
|
73 def __unicode__(self):
|
gremmie@1
|
74 return self.subject
|
gremmie@1
|
75
|
gremmie@1
|
76 class Meta:
|
gremmie@1
|
77 ordering = ('-send_date', )
|
gremmie@1
|
78
|
gremmie@1
|
79
|
gremmie@1
|
80 class OptionsManager(models.Manager):
|
gremmie@1
|
81 """Manager class for Options model."""
|
gremmie@1
|
82
|
gremmie@1
|
83 def for_user(self, user):
|
gremmie@1
|
84 try:
|
gremmie@1
|
85 opts = self.get(user=user)
|
gremmie@1
|
86 except Options.DoesNotExist:
|
gremmie@1
|
87 opts = Options(user=user)
|
gremmie@1
|
88 opts.save()
|
gremmie@1
|
89 return opts
|
gremmie@1
|
90
|
gremmie@1
|
91
|
gremmie@1
|
92 class Options(models.Model):
|
gremmie@1
|
93 """Options is a model for user private message options."""
|
gremmie@1
|
94 user = models.ForeignKey(User, editable=False)
|
gremmie@1
|
95 attach_signature = models.BooleanField("Always attach signature?", default=True)
|
gremmie@1
|
96 notify_email = models.BooleanField("Notify me of new messages via email?", default=False)
|
gremmie@1
|
97
|
gremmie@1
|
98 objects = OptionsManager()
|
gremmie@1
|
99
|
gremmie@1
|
100 def __unicode__(self):
|
gremmie@1
|
101 return self.user.username
|
gremmie@1
|
102
|
gremmie@1
|
103
|
gremmie@1
|
104 # vim: ts=4 sw=4
|