view messages/models.py @ 693:ad69236e8501

For issue #52, update many 3rd party Javascript libraries. Updated to jquery 1.10.2, jquery ui 1.10.3. This broke a lot of stuff. - Found a newer version of the jquery cycle all plugin (3.0.3). - Updated JPlayer to 2.4.0. - Updated to MarkItUp 1.1.14. This also required me to add multiline attributes set to true on various buttons in the markdown set. - As per a stackoverflow post, added some code to get multiline titles in a jQuery UI dialog. They removed that functionality but allow you to put it back. Tweaked the MarkItUp preview CSS to show blockquotes in italic. Did not update TinyMCE at this time. I'm not using the JQuery version and this version appears to work ok for now. What I should do is make a repo for MarkItUp and do a vendor branch thing so I don't have to futz around diffing directories to figure out if I'll lose changes when I update.
author Brian Neal <bgneal@gmail.com>
date Wed, 04 Sep 2013 19:55:20 -0500
parents ee87ea74d46b
children 4a4fa174a0ec
line wrap: on
line source
"""Models for the messages application."""

import datetime

from django.db import models
from django.db.models import Q
from django.contrib.auth.models import User

from core.markup import site_markup


class MessageManager(models.Manager):
    """The manager for the Message model. Provides convenience functions."""

    def inbox(self, user):
        return self.filter(receiver=user,
            receiver_delete_date__isnull=True).select_related('sender')

    def outbox(self, user):
        return self.filter(sender=user,
            sender_delete_date__isnull=True).select_related('receiver')

    def trash(self, user):
        return self.filter(
            Q(sender=user, sender_delete_date__isnull=False) |
            Q(receiver=user, receiver_delete_date__isnull=False)
        ).select_related()

    def unread_count(self, user):
        return self.filter(receiver=user, read_date__isnull=True).count()


class Message(models.Model):
    """The Message is a model for a private message between users."""
    sender = models.ForeignKey(User, related_name='sender')
    receiver = models.ForeignKey(User, related_name='receiver')
    send_date = models.DateTimeField('Date Sent', null=True, blank=True)
    read_date = models.DateTimeField('Date Received', null=True, blank=True)
    reply_date = models.DateTimeField('Date Replied', null=True, blank=True)
    subject = models.CharField(max_length=120)
    message = models.TextField()
    html = models.TextField()
    sender_delete_date = models.DateTimeField(null=True, blank=True)
    receiver_delete_date = models.DateTimeField(null=True, blank=True)
    signature_attached = models.BooleanField(default=False)

    objects = MessageManager()

    def unread(self):
        """returns True if the message has not been read yet"""
        return self.read_date is None

    def replied_to(self):
        """returns True if the message has been replied to"""
        return self.reply_date is not None

    def is_deleted(self, user):
        """returns True if the message has been deleted by the user"""
        if (user == self.sender and self.sender_delete_date is not None) or (
            user == self.receiver and self.receiver_delete_date is not None):
            return True
        return False

    def save(self, *args, **kwargs):
        if not self.id:
            self.send_date = datetime.datetime.now()
        self.html = site_markup(self.message)
        super(Message, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.subject

    class Meta:
        ordering = ('-send_date', )


class OptionsManager(models.Manager):
    """Manager class for Options model."""

    def for_user(self, user):
        try:
            opts = self.get(user=user)
        except Options.DoesNotExist:
            opts = Options(user=user)
            opts.save()
        return opts


class Options(models.Model):
    """Options is a model for user private message options."""
    user = models.ForeignKey(User, editable=False)
    attach_signature = models.BooleanField("Always attach signature?", default=True)
    notify_email = models.BooleanField("Notify me of new messages via email?", default=False)

    objects = OptionsManager()

    def __unicode__(self):
        return self.user.username

    class Meta:
        verbose_name_plural = "Options"