services.py 3.13 KB
Newer Older
1
2
3
from django.utils import timezone

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


def get_automatic_lists():
    memberships = (CommitteeMembership.active_memberships
                   .filter(committee__board=None)
                   .filter(chair=True)
                   .prefetch_related('member__user'))
    committee_chairs = [x.member for x in memberships]

16
17
18
19
    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]
20

21
22
23
24
    active_mentorships = Mentorship.objects.filter(
        year=datetime_to_lectureyear(timezone.now()))
    mentors = [x.member for x in active_mentorships]

25
    lists = []
26

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

57
    return lists
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

def _create_automatic_list(name, prefix, members,
                           archived=True, moderated=True, multilingual=False):
    data = {
        'name': name,
        'prefix': prefix,
        'archived': archived,
        'moderated': moderated,
    }

    if multilingual:
        data['addresses'] = [member.user.email for member in members]
        yield data
        for language in settings.LANGUAGES:
            localized_data = data.copy()
            localized_data['addresses'] = [
                member.user.email for member in members
                if member.language == language[0]]
            localized_data['name'] += '-{}'.format(language[0])
            yield localized_data
    else:
        data['addresses'] = [member.user.email for member in members]
        yield data