models.py 4.75 KB
Newer Older
1
from django.core import validators
2
from django.core.exceptions import ValidationError
3
from django.db import models
4
from django.utils import timezone
5
from django.utils.translation import ugettext_lazy as _
6

7
from activemembers.models import MemberGroup
8
from members.models import Member
9
10
11


class MailingList(models.Model):
12
13
14
15
16
17
18
    name = models.CharField(
        verbose_name=_("Name"),
        max_length=100,
        validators=[validators.RegexValidator(
            regex=r'^[a-zA-Z0-9]+$',
            message=_('Enter a simpler name'))
        ],
19
        unique=True,
20
        help_text=_('Enter the name for the list (i.e. name@thalia.nu).'),
21
22
23
24
    )

    prefix = models.CharField(
        verbose_name=_("Prefix"),
25
        blank=True,
26
27
28
        max_length=200,
        help_text=_('Enter a prefix that should be prefixed to subjects '
                    'of all emails sent via this mailinglist.'),
29
30
31
32
    )

    archived = models.BooleanField(
        verbose_name=_("Archived"),
33
34
        default=True,
        help_text=_('Indicate whether an archive should be kept.')
35
36
37
38
    )

    moderated = models.BooleanField(
        verbose_name=_("Moderated"),
39
40
        default=False,
        help_text=_('Indicate whether emails to the list require approval.')
41
42
    )

43
    members = models.ManyToManyField(
44
        Member,
45
46
47
48
49
50
        verbose_name=_("Members"),
        blank=True,
        help_text=_('Select individual members to include in the list.'),
    )

    committees = models.ManyToManyField(
51
        MemberGroup,
52
53
54
55
        verbose_name=_("Committees"),
        help_text=_('Select entire committees to include in the list.'),
        blank=True,
    )
56

57
58
59
60
61
62
63
64
65
66
67
68
    autoresponse_enabled = models.BooleanField(
        verbose_name=_("Automatic response enabled"),
        default=False,
        help_text=_('Indicate whether emails will get an automatic response.')
    )

    autoresponse_text = models.TextField(
        verbose_name=_("Autoresponse text"),
        null=True,
        blank=True,
    )

69
70
    def all_addresses(self):
        for member in self.members.all():
71
            yield member.email
72
73

        for committee in self.committees.all().prefetch_related("members"):
74
            for member in committee.members.exclude(
Thom Wiggers's avatar
RIP    
Thom Wiggers committed
75
                    committeemembership__until__lt=timezone.now().date()):
76
                yield member.email
77

78
79
        for verbatimaddress in self.addresses.all():
            yield verbatimaddress.address
80

81
82
83
84
85
86
87
88
89
90
91
92
    def clean(self):
        super().clean()
        if (ListAlias.objects
                .filter(alias=self.name).count() > 0):
            raise ValidationError({
                'name': _("%(model_name)s with this "
                          "%(field_label)s already exists.") % {
                             'model_name': _("Mailing list"),
                             'field_label': _("List alias")
                         }
            })

93
94
95
96
97
        if not self.autoresponse_text and self.autoresponse_enabled:
            raise ValidationError({
                'autoresponse_text': _('Enter a text for the auto response.')
            })

98
99
100
101
102
    def __str__(self):
        return self.name


class VerbatimAddress(models.Model):
103
104
105
106
107
    address = models.EmailField(
        verbose_name=_("Email address"),
        help_text=_('Enter an explicit email address to include in the list.'),
    )

108
    mailinglist = models.ForeignKey(MailingList,
109
                                    verbose_name=_("Mailing list"),
110
111
                                    on_delete=models.CASCADE,
                                    related_name='addresses')
112

113
114
115
    def __str__(self):
        return self.address

116
117
118
119
    class Meta:
        verbose_name = _("Verbatim address")
        verbose_name_plural = _("Verbatim addresses")

120
121

class ListAlias(models.Model):
122
    alias = models.CharField(
123
        verbose_name=_("Alternative name"),
124
125
126
127
128
        max_length=100,
        validators=[validators.RegexValidator(
            regex=r'^[a-zA-Z0-9]+$',
            message=_('Enter a simpler name'))
        ],
129
        unique=True,
130
        help_text=_('Enter an alternative name for the list.'),
131
    )
132
    mailinglist = models.ForeignKey(MailingList,
133
                                    verbose_name=_("Mailing list"),
134
135
                                    on_delete=models.CASCADE,
                                    related_name='aliasses')
136

137
138
139
140
141
142
143
144
145
146
147
148
    def clean(self):
        super().clean()
        if (MailingList.objects
                .filter(name=self.alias).count() > 0):
            raise ValidationError({
                'alias': _("%(model_name)s with this "
                           "%(field_label)s already exists.") % {
                    'model_name': _("Mailing list"),
                    'field_label': _("Name")
                }
            })

149
150
151
    class Meta:
        verbose_name = _("List alias")
        verbose_name_plural = _("List aliasses")