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

6
from activemembers.models import Committee
7
from members.models import Member
8 9 10


class MailingList(models.Model):
11 12 13 14 15 16
    name = models.CharField(max_length=100,
                            validators=[validators.RegexValidator(
                                        regex=r'^[a-zA-Z0-9]+$',
                                        message=_('Enter a simpler name'))
                                        ],
                            )
17 18 19 20 21 22 23 24
    prefix = models.CharField(max_length=200)
    archived = models.BooleanField(default=True)
    moderated = models.BooleanField(default=False)
    members = models.ManyToManyField(Member, blank=True)
    committees = models.ManyToManyField(Committee, blank=True)

    def all_addresses(self):
        for member in self.members.all():
25
            yield member.user.email
26 27

        for committee in self.committees.all().prefetch_related("members"):
28 29
            for member in committee.members.exclude(
                    commiteemembership__until__lt=timezone.now().date()):
30
                yield member.user.email
31 32 33 34 35 36 37 38 39 40

        for address in self.addresses.all():
            yield address

    def __str__(self):
        return self.name


class VerbatimAddress(models.Model):
    address = models.EmailField()
41 42 43
    mailinglist = models.ForeignKey(MailingList,
                                    on_delete=models.CASCADE,
                                    related_name='addresses')
44

45 46 47
    def __str__(self):
        return self.address

48 49

class ListAlias(models.Model):
50 51 52 53 54 55
    alias = models.CharField(max_length=100,
                             validators=[validators.RegexValidator(
                                         regex=r'^[a-zA-Z0-9]+$',
                                         message=_('Enter a simpler name'))
                                         ],
                             )
56 57 58
    mailinglist = models.ForeignKey(MailingList,
                                    on_delete=models.CASCADE,
                                    related_name='aliasses')