services.py 2.78 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
    lists += _create_automatic_list(
28
29
        ['leden', 'members'], '[THALIA]',
        Member.all_with_membership('member', 'user'), True, True, True)
30
    lists += _create_automatic_list(
31
        ['begunstigers', 'supporters'], '[THALIA]', Member.all_with_membership(
32
33
            'supporter', 'user'), multilingual=True)
    lists += _create_automatic_list(
34
        ['ereleden', 'honorary'], '[THALIA]', Member.all_with_membership(
35
36
            'honorary', 'user'), multilingual=True)
    lists += _create_automatic_list(
37
        ['mentors'], '[THALIA] [MENTORS]', mentors, moderated=False)
38
    lists += _create_automatic_list(
39
        ['activemembers'], '[THALIA] [COMMITTEES]',
40
41
        active_members)
    lists += _create_automatic_list(
42
        ['commissievoorzitters'], '[THALIA] [CHAIRS]',
43
44
        committee_chairs, moderated=False)
    lists += _create_automatic_list(
45
        ['optin'], '[THALIA] [OPTIN]', Member.active_members.filter(
46
            receive_optin=True).prefetch_related('user'))
47

48
    return lists
49

50

51
def _create_automatic_list(names, prefix, members,
52
53
                           archived=True, moderated=True, multilingual=False):
    data = {
54
        'names': names,
55
56
57
58
59
60
61
62
63
64
65
66
67
        '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]]
68
69
            localized_data['names'] = [
                '{}-{}'.format(n, language[0]) for n in names]
70
71
72
73
            yield localized_data
    else:
        data['addresses'] = [member.user.email for member in members]
        yield data