services.py 2.95 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
25
    active_mentorships = Mentorship.objects.filter(
        year=datetime_to_lectureyear(timezone.now()))
    mentors = [x.member for x in active_mentorships]

    # name, prefix, members, archived, moderated, multilingual
26
27
28
29
30
31
32
33
34
35
36
37
38
    lists = [
        ('leden', '[THALIA]', Member.all_with_membership(
            'member', 'user'), True, True, True),
        ('begunstigers', '[THALIA]', Member.all_with_membership(
            'supporter', 'user'), True, True, True),
        ('ereleden', '[THALIA]', Member.all_with_membership(
            'honorary', 'user'), True, True, True),
        ('members', '[THALIA]', Member.all_with_membership(
            'member', 'user'), True, True, True),
        ('supporters', '[THALIA]', Member.all_with_membership(
            'supporter', 'user'), True, True, True),
        ('honorary', '[THALIA]', Member.all_with_membership(
            'honorary', 'user'), True, True, True),
39
40
41
42
43
        ('mentors', '[THALIA] [MENTORS]',
            mentors, True, False, False),
        ('activemembers', '[THALIA] [COMMITTEES]',
            active_members, True, True, False),
        ('commissievoorzitters', '[THALIA] [CHAIRS]',
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
            committee_chairs, True, False, False),
        ('optin', '[THALIA] [OPTIN]', Member.active_members.filter(
            receive_optin=True).prefetch_related('user'), True, True, False),
    ]

    return_data = []
    for list in lists:
        data = {
            'name': list[0],
            'prefix': list[1],
            'archived': list[3],
            'moderated': list[4],
        }

        if list[5]:
            data['addresses'] = (member.user.email for member in list[2])
            return_data.append(data)
            for language in settings.LANGUAGES:
                localized_data = data.copy()
                localized_data['addresses'] = [
                    member.user.email for member in list[2]
                    if member.language == language[0]]
                localized_data['name'] += '-{}'.format(language[0])
                return_data.append(localized_data)
        else:
            data['addresses'] = (member.user.email for member in list[2])
            return_data.append(data)

    return return_data