comparison gpp/messages/models.py @ 1:dbd703f7d63a

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