services.py 3.01 KB
Newer Older
1
from django.contrib.auth.models import User
2
3
4
from django.utils import timezone

from activemembers.models import CommitteeMembership, Mentorship
5
6
from members.models import Member
from thaliawebsite.settings import settings
7
from utils.snippets import datetime_to_lectureyear
8
9
10
11
12
13
14


def get_automatic_lists():
    memberships = (CommitteeMembership.active_memberships
                   .filter(committee__board=None)
                   .filter(chair=True)
                   .prefetch_related('member__user'))
15
16
17
18
19
    committee_chairs = [x.member for x in memberships] + [Member(
        user=User(
            email='intern@thalia.nu'
        )
    )]
20

21
22
23
24
    active_committee_memberships = (CommitteeMembership.active_memberships
                                    .exclude(committee__board__is_board=True)
                                    .prefetch_related('member__user'))
    active_members = [x.member for x in active_committee_memberships]
25

26
27
28
29
    active_mentorships = Mentorship.objects.filter(
        year=datetime_to_lectureyear(timezone.now()))
    mentors = [x.member for x in active_mentorships]

30
    lists = []
31

32
    lists += _create_automatic_list(
33
34
        ['leden', 'members'], '[THALIA]',
        Member.all_with_membership('member', 'user'), True, True, True)
35
    lists += _create_automatic_list(
36
        ['begunstigers', 'supporters'], '[THALIA]', Member.all_with_membership(
37
38
            'supporter', 'user'), multilingual=True)
    lists += _create_automatic_list(
39
        ['ereleden', 'honorary'], '[THALIA]', Member.all_with_membership(
40
41
            'honorary', 'user'), multilingual=True)
    lists += _create_automatic_list(
42
        ['mentors'], '[THALIA] [MENTORS]', mentors, moderated=False)
43
    lists += _create_automatic_list(
44
        ['activemembers'], '[THALIA] [COMMITTEES]',
45
46
        active_members)
    lists += _create_automatic_list(
47
        ['commissievoorzitters', 'committeechairs'], '[THALIA] [CHAIRS]',
48
49
        committee_chairs, moderated=False)
    lists += _create_automatic_list(
50
        ['optin'], '[THALIA] [OPTIN]', Member.active_members.filter(
51
            receive_optin=True).prefetch_related('user'))
52

53
    return lists
54

55

56
def _create_automatic_list(names, prefix, members,
57
58
                           archived=True, moderated=True, multilingual=False):
    data = {
59
        'names': names,
60
61
62
63
64
65
66
        'prefix': prefix,
        'archived': archived,
        'moderated': moderated,
    }

    if multilingual:
        data['addresses'] = [member.user.email for member in members]
67
        yield data  # this is the complete list, e.g. leden@
68
69
70
71
72
        for language in settings.LANGUAGES:
            localized_data = data.copy()
            localized_data['addresses'] = [
                member.user.email for member in members
                if member.language == language[0]]
73
74
            localized_data['names'] = [
                '{}-{}'.format(n, language[0]) for n in names]
75
            yield localized_data  # these are localized lists, e.g. leden-nl@
76
77
78
    else:
        data['addresses'] = [member.user.email for member in members]
        yield data