services.py 3.85 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
12
13
14
15
    current_committee_chairs = (MemberGroupMembership.active_objects
                                .filter(group__board=None)
                                .filter(group__society=None)
                                .filter(chair=True)
                                .prefetch_related('member'))
    committee_chair_emails = [x.member for x in current_committee_chairs] + [
16
17
        Member(email='intern@thalia.nu')
    ]
18

19
    active_committee_memberships = (MemberGroupMembership.active_objects
20
                                    .filter(group__board=None)
21
                                    .filter(group__society=None)
22
                                    .prefetch_related('member'))
23
    active_members = [x.member for x in active_committee_memberships]
24

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

33
    lists = []
34

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

57
58
59
60
61
62
63
64
65
    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:]],
                '',
66
67
                [x.member for x in MemberGroupMembership.objects
                    .filter(group=board).prefetch_related('member')],
68
69
70
                archived=False, moderated=False, multilingual=False
            )

71
    return lists
72

73

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

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