models.py 2.88 KB
Newer Older
Dylann Cordel's avatar
Dylann Cordel committed
1
2
3
4
5
6
7
8
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.conf import settings
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext as _

Dylann Cordel's avatar
wip    
Dylann Cordel committed
9
from admin_message.conf import app_settings
Dylann Cordel's avatar
Dylann Cordel committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25


###################################################################################################
###################################################################################################
@python_2_unicode_compatible
class MessageBase(models.Model):
    creation_date = models.DateTimeField(
        verbose_name=_('creation date'),
        auto_now_add=True)
    modification_date = models.DateTimeField(
        verbose_name=_('modification date'),
        auto_now=True)
    post_date = models.DateTimeField(
        verbose_name=_('posting date'),
        blank=True, null=True)
    author = models.ForeignKey(
Dylann Cordel's avatar
wip    
Dylann Cordel committed
26
        settings.AUTH_USER_MODEL,
Dylann Cordel's avatar
Dylann Cordel committed
27
28
29
30
31
32
        on_delete=models.CASCADE,
        verbose_name=_('author'),
        related_name='%(app_label)s_%(class)s_related')
    test = models.BooleanField(
        verbose_name=_('is test'),
        default=False)
Dylann Cordel's avatar
wip    
Dylann Cordel committed
33
34
35
36
    message_type = models.CharField(
        max_length=14,
        verbose_name=_('is test'),
        default=app_settings)
Dylann Cordel's avatar
Dylann Cordel committed
37
38
39
40
41
42
43
    sender = models.EmailField(
        verbose_name=_('sender'))
    recipients = models.TextField(
        verbose_name=_('recipients'))
    subject = models.CharField(
        verbose_name=_('subject'),
        max_length=200)
Dylann Cordel's avatar
wip    
Dylann Cordel committed
44
45
46
47
48
49
50
51
    message = models.TextField(
        verbose_name=_('message'),
        blank=True, null=True)
    sent_txt = models.TextField(
        verbose_name=_('TXT content sent'),
        blank=True, null=True)
    sent_html = models.TextField(
        verbose_name=_('HTML content sent'),
Dylann Cordel's avatar
Dylann Cordel committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
        blank=True, null=True)

    class Meta:
        abstract = True
        ordering = ['-post_date']
        verbose_name = _('message')
        verbose_name_plural = _('messages')

    def __str__(self):
        return self.subject if self.subject else _('Pending message')


###################################################################################################
###################################################################################################
class Message(MessageBase):
    pass


###################################################################################################
###################################################################################################
class RecipientMixin(object):

    def get_final_recipients(self, force=False):
Dylann Cordel's avatar
wip    
Dylann Cordel committed
75
        return []
Dylann Cordel's avatar
Dylann Cordel committed
76

Dylann Cordel's avatar
wip    
Dylann Cordel committed
77
    def get_final_recipients_count(self, message_type=None, force=False):
Dylann Cordel's avatar
Dylann Cordel committed
78
79
        """Returns the number of recipients"""
        if force or not hasattr(self, '_final_recipients_count'):
Dylann Cordel's avatar
wip    
Dylann Cordel committed
80
            self._final_recipients_count = len(self.get_final_recipients(message_type, force))
Dylann Cordel's avatar
Dylann Cordel committed
81
        return self._final_recipients_count