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

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


def get_automatic_lists():
    memberships = (CommitteeMembership.active_memberships
                   .filter(committee__board=None)
                   .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 = (CommitteeMembership.active_memberships
                                    .exclude(committee__board__is_board=True)
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
    return lists
56

57

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

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