summaryrefslogtreecommitdiff
path: root/django/archives/mailarchives/models.py
blob: 4a6a055db0ed106b7011a673eee4dca2ea4135c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from django.db import models
from django.contrib.auth.models import User

from email.utils import parseaddr

# Reason a message was hidden.
# We're intentionally putting the prefix text in the array here, since
# we might need that flexibility in the future.
hide_reasons = [
    None,                                # placeholder for 0
    'This message has been hidden because a virus was found in the message.',  # 1
    'This message has been hidden because the message violated policies.',     # 2
    'This message has been hidden because for privacy reasons.',               # 3
    'This message was corrupt',                                                # 4
]


class Message(models.Model):
    threadid = models.IntegerField(null=False, blank=False)
    mailfrom = models.TextField(null=False, db_column='_from')
    to = models.TextField(null=False, db_column='_to')
    cc = models.TextField(null=False)
    subject = models.TextField(null=False)
    date = models.DateTimeField(null=False)
    messageid = models.TextField(null=False)
    bodytxt = models.TextField(null=False)
    # rawtxt is a bytea field, which django doesn't support (easily)
    parentid = models.IntegerField(null=False, blank=False)
    has_attachment = models.BooleanField(null=False, default=False)
    hiddenstatus = models.IntegerField(null=True)
    # fti is a tsvector field, which django doesn't support (easily)

    class Meta:
        db_table = 'messages'

    @property
    def printdate(self):
        return self.date.strftime("%Y-%m-%d %H:%M:%S")

    @property
    def shortdate(self):
        return self.date.strftime("%Y%m%d%H%M")

    @property
    def dateonly(self):
        return self.date.strftime("%Y%m%d")

    def from_name_only(self):
        try:
            return parseaddr(self.mailfrom)[0]
        except Exception:
            return ''

    # We explicitly cache the attachments here, so we can use them
    # multiple times from templates without generating multiple queries
    # to the database.
    _attachments = None

    @property
    def attachments(self):
        if not self._attachments:
            self._attachments = self.attachment_set.extra(select={'len': 'length(attachment)'}).all()
        return self._attachments

    @property
    def hiddenreason(self):
        if not self.hiddenstatus:
            return None
        try:
            return hide_reasons[self.hiddenstatus]
        except Exception:
            # Weird value
            return 'This message has been hidden.'


class ListGroup(models.Model):
    groupid = models.IntegerField(null=False, primary_key=True)
    groupname = models.CharField(max_length=200, null=False, blank=False)
    sortkey = models.IntegerField(null=False)

    class Meta:
        db_table = 'listgroups'


class List(models.Model):
    listid = models.IntegerField(null=False, primary_key=True)
    listname = models.CharField(max_length=200, null=False, blank=False, unique=True)
    shortdesc = models.TextField(null=False, blank=False)
    description = models.TextField(null=False, blank=False)
    active = models.BooleanField(null=False, blank=False)
    group = models.ForeignKey(ListGroup, db_column='groupid', on_delete=models.CASCADE)
    subscriber_access = models.BooleanField(null=False, blank=False, default=False, help_text="Subscribers can access contents (default is admins only)")

    @property
    def maybe_shortdesc(self):
        if self.shortdesc:
            return self.shortdesc
        return self.listname

    class Meta:
        db_table = 'lists'


class Attachment(models.Model):
    message = models.ForeignKey(Message, null=False, blank=False, db_column='message', on_delete=models.CASCADE)
    filename = models.CharField(max_length=1000, null=False, blank=False)
    contenttype = models.CharField(max_length=1000, null=False, blank=False)
    # attachment = bytea, not supported by django at this point

    class Meta:
        db_table = 'attachments'
        # Predictable same-as-insert order
        ordering = ('id',)

    def inlineable(self):
        # Return True if this image should be inlined
        if self.contenttype in ('image/png', 'image/gif', 'image/jpg', 'image/jpeg'):
            # Note! len needs to be set with extra(select=)
            if self.len < 75000:
                return True
        return False


class ListSubscriber(models.Model):
    # Only used when public access is not allowed.
    # We set the username of the community account instead of a
    # foreign key, because the user might not exist.
    list = models.ForeignKey(List, null=False, blank=False, on_delete=models.CASCADE)
    username = models.CharField(max_length=30, null=False, blank=False)

    class Meta:
        unique_together = (('list', 'username'), )
        db_table = 'listsubscribers'


class ResendMessage(models.Model):
    message = models.ForeignKey(Message, null=False, blank=False, on_delete=models.CASCADE)
    sendto = models.ForeignKey(User, null=False, blank=False, on_delete=models.CASCADE)
    registeredat = models.DateTimeField(null=False, blank=False)

    class Meta:
        unique_together = (('message', 'sendto'), )


class LastResentMessage(models.Model):
    sentto = models.OneToOneField(User, null=False, blank=False, primary_key=True, on_delete=models.CASCADE)
    sentat = models.DateTimeField(null=False, blank=False)