gremmie@1: """Models for the messages application.""" gremmie@1: gremmie@1: import datetime gremmie@1: from django.db import models gremmie@1: from django.db.models import Q gremmie@1: from django.contrib.auth.models import User gremmie@1: from django.template.loader import render_to_string gremmie@1: gremmie@1: gremmie@1: class MessageManager(models.Manager): gremmie@1: """The manager for the Message model. Provides convenience functions.""" gremmie@1: gremmie@1: def inbox(self, user): gremmie@1: return self.filter(receiver=user, gremmie@1: receiver_delete_date__isnull=True) gremmie@1: gremmie@1: def outbox(self, user): gremmie@1: return self.filter(sender=user, gremmie@1: sender_delete_date__isnull=True) gremmie@1: gremmie@1: def trash(self, user): gremmie@1: return self.filter( gremmie@1: Q(sender=user, sender_delete_date__isnull=False) | gremmie@1: Q(receiver=user, receiver_delete_date__isnull=False) gremmie@1: ) gremmie@1: gremmie@1: def unread_count(self, user): gremmie@1: return self.filter(receiver=user, read_date__isnull=True).count() gremmie@1: gremmie@1: gremmie@1: class Message(models.Model): gremmie@1: """The Message is a model for a private message between users.""" gremmie@1: sender = models.ForeignKey(User, related_name='sender') gremmie@1: receiver = models.ForeignKey(User, related_name='receiver') gremmie@1: send_date = models.DateTimeField('Date Sent', null=True, blank=True) gremmie@1: read_date = models.DateTimeField('Date Received', null=True, blank=True) gremmie@1: reply_date = models.DateTimeField('Date Replied', null=True, blank=True) gremmie@1: subject = models.CharField(max_length=120) gremmie@1: message = models.TextField() gremmie@1: html = models.TextField() gremmie@1: sender_delete_date = models.DateTimeField(null=True, blank=True) gremmie@1: receiver_delete_date = models.DateTimeField(null=True, blank=True) gremmie@1: signature_attached = models.BooleanField(default=False) gremmie@1: gremmie@1: objects = MessageManager() gremmie@1: gremmie@1: def unread(self): gremmie@1: """returns True if the message has not been read yet""" gremmie@1: return self.read_date is None gremmie@1: gremmie@1: def replied_to(self): gremmie@1: """returns True if the message has been replied to""" gremmie@1: return self.reply_date is not None gremmie@1: gremmie@1: def is_deleted(self, user): gremmie@1: """returns True if the message has been deleted by the user""" gremmie@1: if (user == self.sender and self.sender_delete_date is not None) or ( gremmie@1: user == self.receiver and self.receiver_delete_date is not None): gremmie@1: return True gremmie@1: return False gremmie@1: gremmie@1: def save(self, force_insert = False, force_update = False): gremmie@1: if not self.id: gremmie@1: self.send_date = datetime.datetime.now() gremmie@1: html = render_to_string('messages/markdown.html', {'data': self.message}) gremmie@1: self.html = html.strip() gremmie@1: super(Message, self).save(force_insert, force_update) gremmie@1: gremmie@1: @models.permalink gremmie@1: def get_absolute_url(self): gremmie@1: return ('messages.views.view', [str(self.id)]) gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return self.subject gremmie@1: gremmie@1: class Meta: gremmie@1: ordering = ('-send_date', ) gremmie@1: gremmie@1: gremmie@1: class OptionsManager(models.Manager): gremmie@1: """Manager class for Options model.""" gremmie@1: gremmie@1: def for_user(self, user): gremmie@1: try: gremmie@1: opts = self.get(user=user) gremmie@1: except Options.DoesNotExist: gremmie@1: opts = Options(user=user) gremmie@1: opts.save() gremmie@1: return opts gremmie@1: gremmie@1: gremmie@1: class Options(models.Model): gremmie@1: """Options is a model for user private message options.""" gremmie@1: user = models.ForeignKey(User, editable=False) gremmie@1: attach_signature = models.BooleanField("Always attach signature?", default=True) gremmie@1: notify_email = models.BooleanField("Notify me of new messages via email?", default=False) gremmie@1: gremmie@1: objects = OptionsManager() gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return self.user.username gremmie@1: gremmie@1: gremmie@1: # vim: ts=4 sw=4