annotate messages/models.py @ 1206:02181fa5ac9d modernize tip

Update to Django 1.9.
author Brian Neal <bgneal@gmail.com>
date Wed, 22 Jan 2025 17:58:16 -0600
parents f0ac48aa8c64
children
rev   line source
gremmie@1 1 """Models for the messages application."""
gremmie@1 2
gremmie@1 3 import datetime
bgneal@128 4
gremmie@1 5 from django.db import models
gremmie@1 6 from django.db.models import Q
gremmie@1 7 from django.contrib.auth.models import User
bgneal@128 8
bgneal@128 9 from core.markup import site_markup
gremmie@1 10
gremmie@1 11
gremmie@1 12 class MessageManager(models.Manager):
gremmie@1 13 """The manager for the Message model. Provides convenience functions."""
gremmie@1 14
gremmie@1 15 def inbox(self, user):
gremmie@1 16 return self.filter(receiver=user,
bgneal@425 17 receiver_delete_date__isnull=True).select_related('sender')
gremmie@1 18
gremmie@1 19 def outbox(self, user):
gremmie@1 20 return self.filter(sender=user,
bgneal@425 21 sender_delete_date__isnull=True).select_related('receiver')
gremmie@1 22
gremmie@1 23 def trash(self, user):
gremmie@1 24 return self.filter(
gremmie@1 25 Q(sender=user, sender_delete_date__isnull=False) |
gremmie@1 26 Q(receiver=user, receiver_delete_date__isnull=False)
bgneal@425 27 ).select_related()
gremmie@1 28
gremmie@1 29 def unread_count(self, user):
gremmie@1 30 return self.filter(receiver=user, read_date__isnull=True).count()
gremmie@1 31
gremmie@1 32
gremmie@1 33 class Message(models.Model):
gremmie@1 34 """The Message is a model for a private message between users."""
bgneal@1206 35 sender = models.ForeignKey(User, related_name='sender',
bgneal@1206 36 on_delete=models.CASCADE)
bgneal@1206 37 receiver = models.ForeignKey(User, related_name='receiver',
bgneal@1206 38 on_delete=models.CASCADE)
gremmie@1 39 send_date = models.DateTimeField('Date Sent', null=True, blank=True)
gremmie@1 40 read_date = models.DateTimeField('Date Received', null=True, blank=True)
gremmie@1 41 reply_date = models.DateTimeField('Date Replied', null=True, blank=True)
gremmie@1 42 subject = models.CharField(max_length=120)
gremmie@1 43 message = models.TextField()
gremmie@1 44 html = models.TextField()
gremmie@1 45 sender_delete_date = models.DateTimeField(null=True, blank=True)
gremmie@1 46 receiver_delete_date = models.DateTimeField(null=True, blank=True)
gremmie@1 47 signature_attached = models.BooleanField(default=False)
gremmie@1 48
gremmie@1 49 objects = MessageManager()
gremmie@1 50
gremmie@1 51 def unread(self):
gremmie@1 52 """returns True if the message has not been read yet"""
gremmie@1 53 return self.read_date is None
gremmie@1 54
gremmie@1 55 def replied_to(self):
gremmie@1 56 """returns True if the message has been replied to"""
gremmie@1 57 return self.reply_date is not None
gremmie@1 58
gremmie@1 59 def is_deleted(self, user):
gremmie@1 60 """returns True if the message has been deleted by the user"""
gremmie@1 61 if (user == self.sender and self.sender_delete_date is not None) or (
gremmie@1 62 user == self.receiver and self.receiver_delete_date is not None):
gremmie@1 63 return True
gremmie@1 64 return False
gremmie@1 65
bgneal@182 66 def save(self, *args, **kwargs):
gremmie@1 67 if not self.id:
gremmie@1 68 self.send_date = datetime.datetime.now()
bgneal@1022 69
bgneal@1064 70 self.html = kwargs.pop('html', '')
bgneal@1022 71 if not self.html and self.message:
bgneal@1022 72 self.html = site_markup(self.message)
bgneal@182 73 super(Message, self).save(*args, **kwargs)
gremmie@1 74
gremmie@1 75 def __unicode__(self):
gremmie@1 76 return self.subject
gremmie@1 77
gremmie@1 78 class Meta:
gremmie@1 79 ordering = ('-send_date', )
gremmie@1 80
gremmie@1 81
gremmie@1 82 class OptionsManager(models.Manager):
gremmie@1 83 """Manager class for Options model."""
gremmie@1 84
gremmie@1 85 def for_user(self, user):
gremmie@1 86 try:
gremmie@1 87 opts = self.get(user=user)
gremmie@1 88 except Options.DoesNotExist:
gremmie@1 89 opts = Options(user=user)
gremmie@1 90 opts.save()
gremmie@1 91 return opts
gremmie@1 92
gremmie@1 93
gremmie@1 94 class Options(models.Model):
gremmie@1 95 """Options is a model for user private message options."""
bgneal@1206 96 user = models.ForeignKey(User, editable=False, on_delete=models.CASCADE)
gremmie@1 97 attach_signature = models.BooleanField("Always attach signature?", default=True)
gremmie@1 98 notify_email = models.BooleanField("Notify me of new messages via email?", default=False)
gremmie@1 99
gremmie@1 100 objects = OptionsManager()
gremmie@1 101
gremmie@1 102 def __unicode__(self):
gremmie@1 103 return self.user.username
gremmie@1 104
bgneal@206 105 class Meta:
bgneal@206 106 verbose_name_plural = "Options"
gremmie@1 107
bgneal@206 108
bgneal@810 109 class Flag(models.Model):
bgneal@810 110 """The Flag model is used to represent when a receiver of a private message
bgneal@810 111 has flagged the message for spam or abuse.
bgneal@810 112
bgneal@810 113 """
bgneal@1206 114 message = models.OneToOneField(Message, on_delete=models.CASCADE)
bgneal@810 115 flag_date = models.DateTimeField()
bgneal@810 116 comments = models.TextField(blank=True)
bgneal@810 117
bgneal@810 118 def __unicode__(self):
bgneal@810 119 msg = self.message
bgneal@810 120 return "{} has flagged a PM from {}".format(msg.receiver.username,
bgneal@810 121 msg.sender.username)
bgneal@810 122
bgneal@810 123 def save(self, *args, **kwargs):
bgneal@810 124 if not self.id:
bgneal@810 125 self.flag_date = datetime.datetime.now()
bgneal@810 126 super(Flag, self).save(*args, **kwargs)
bgneal@810 127