services.py 3.66 KB
Newer Older
1
from django.conf import settings
2
3
from django.utils import timezone

4
from activemembers.models import MemberGroupMembership, Mentorship, Board
5
from members.models import Member
6
from utils.snippets import datetime_to_lectureyear
7
8
9


def get_automatic_lists():
10
11
    memberships = (MemberGroupMembership.active_memberships
                   .filter(group__board=None)
12
                   .filter(chair=True)
13
                   .prefetch_related('member'))
14
15
16
    committee_chairs = [x.member for x in memberships] + [
        Member(email='intern@thalia.nu')
    ]
17

18
19
    active_committee_memberships = (MemberGroupMembership.active_memberships
                                    .filter(group__board=None)
20
                                    .prefetch_related('member'))
21
    active_members = [x.member for x in active_committee_memberships]
22

23
24
25
26
    lectureyear = datetime_to_lectureyear(timezone.now())
    # Change to next lecture year after December
    if 0 < timezone.now().month < 9:
        lectureyear += 1
27
    active_mentorships = Mentorship.objects.filter(
28
        year=lectureyear)
29
    mentors = [x.member for x in active_mentorships]
30

31
    lists = []
32

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

55
56
57
58
59
60
61
62
63
    for year in range(Board.objects.earliest('since').since.year, lectureyear):
        board = Board.objects.get(since__year=year)
        if board is not None:
            lists += _create_automatic_list(
                ['bestuur'
                 + str(board.since.year)[-2:] + str(board.until.year)[-2:],
                 'board'
                 + str(board.since.year)[-2:] + str(board.until.year)[-2:]],
                '',
64
65
                [x.member for x in MemberGroupMembership.objects
                    .filter(group=board).prefetch_related('member')],
66
67
68
                archived=False, moderated=False, multilingual=False
            )

69
    return lists
70

71

72
def _create_automatic_list(names, prefix, members,
73
74
                           archived=True, moderated=True, multilingual=False):
    data = {
75
        'names': names,
76
77
78
79
80
81
        'prefix': prefix,
        'archived': archived,
        'moderated': moderated,
    }

    if multilingual:
82
        data['addresses'] = [member.email for member in members]
83
        yield data  # this is the complete list, e.g. leden@
84
85
86
        for language in settings.LANGUAGES:
            localized_data = data.copy()
            localized_data['addresses'] = [
87
88
                member.email for member in members
                if member.profile.language == language[0]]
89
90
            localized_data['names'] = [
                '{}-{}'.format(n, language[0]) for n in names]
91
            yield localized_data  # these are localized lists, e.g. leden-nl@
92
    else:
93
        data['addresses'] = [member.email for member in members]
94
        yield data