Mercurial > public > sg101
comparison messages/models.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/messages/models.py@d0f0800eef0c |
children | 4a4fa174a0ec |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """Models for the messages application.""" | |
2 | |
3 import datetime | |
4 | |
5 from django.db import models | |
6 from django.db.models import Q | |
7 from django.contrib.auth.models import User | |
8 | |
9 from core.markup import site_markup | |
10 | |
11 | |
12 class MessageManager(models.Manager): | |
13 """The manager for the Message model. Provides convenience functions.""" | |
14 | |
15 def inbox(self, user): | |
16 return self.filter(receiver=user, | |
17 receiver_delete_date__isnull=True).select_related('sender') | |
18 | |
19 def outbox(self, user): | |
20 return self.filter(sender=user, | |
21 sender_delete_date__isnull=True).select_related('receiver') | |
22 | |
23 def trash(self, user): | |
24 return self.filter( | |
25 Q(sender=user, sender_delete_date__isnull=False) | | |
26 Q(receiver=user, receiver_delete_date__isnull=False) | |
27 ).select_related() | |
28 | |
29 def unread_count(self, user): | |
30 return self.filter(receiver=user, read_date__isnull=True).count() | |
31 | |
32 | |
33 class Message(models.Model): | |
34 """The Message is a model for a private message between users.""" | |
35 sender = models.ForeignKey(User, related_name='sender') | |
36 receiver = models.ForeignKey(User, related_name='receiver') | |
37 send_date = models.DateTimeField('Date Sent', null=True, blank=True) | |
38 read_date = models.DateTimeField('Date Received', null=True, blank=True) | |
39 reply_date = models.DateTimeField('Date Replied', null=True, blank=True) | |
40 subject = models.CharField(max_length=120) | |
41 message = models.TextField() | |
42 html = models.TextField() | |
43 sender_delete_date = models.DateTimeField(null=True, blank=True) | |
44 receiver_delete_date = models.DateTimeField(null=True, blank=True) | |
45 signature_attached = models.BooleanField(default=False) | |
46 | |
47 objects = MessageManager() | |
48 | |
49 def unread(self): | |
50 """returns True if the message has not been read yet""" | |
51 return self.read_date is None | |
52 | |
53 def replied_to(self): | |
54 """returns True if the message has been replied to""" | |
55 return self.reply_date is not None | |
56 | |
57 def is_deleted(self, user): | |
58 """returns True if the message has been deleted by the user""" | |
59 if (user == self.sender and self.sender_delete_date is not None) or ( | |
60 user == self.receiver and self.receiver_delete_date is not None): | |
61 return True | |
62 return False | |
63 | |
64 def save(self, *args, **kwargs): | |
65 if not self.id: | |
66 self.send_date = datetime.datetime.now() | |
67 self.html = site_markup(self.message) | |
68 super(Message, self).save(*args, **kwargs) | |
69 | |
70 def __unicode__(self): | |
71 return self.subject | |
72 | |
73 class Meta: | |
74 ordering = ('-send_date', ) | |
75 | |
76 | |
77 class OptionsManager(models.Manager): | |
78 """Manager class for Options model.""" | |
79 | |
80 def for_user(self, user): | |
81 try: | |
82 opts = self.get(user=user) | |
83 except Options.DoesNotExist: | |
84 opts = Options(user=user) | |
85 opts.save() | |
86 return opts | |
87 | |
88 | |
89 class Options(models.Model): | |
90 """Options is a model for user private message options.""" | |
91 user = models.ForeignKey(User, editable=False) | |
92 attach_signature = models.BooleanField("Always attach signature?", default=True) | |
93 notify_email = models.BooleanField("Notify me of new messages via email?", default=False) | |
94 | |
95 objects = OptionsManager() | |
96 | |
97 def __unicode__(self): | |
98 return self.user.username | |
99 | |
100 class Meta: | |
101 verbose_name_plural = "Options" | |
102 | |
103 |