services.py 3.19 KB
Newer Older
1
from django.contrib.auth.models import User
2
3
4
from django.utils import timezone

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


def get_automatic_lists():
    memberships = (CommitteeMembership.active_memberships
                   .filter(committee__board=None)
                   .filter(chair=True)
                   .prefetch_related('member__user'))
15
    committee_chairs = [x.member.member for x in memberships] + [Member(
16
17
18
19
        user=User(
            email='intern@thalia.nu'
        )
    )]
20

21
22
    active_committee_memberships = (CommitteeMembership.active_memberships
                                    .exclude(committee__board__is_board=True)
23
24
                                    .prefetch_related('member__member'))
    active_members = [x.member.member for x in active_committee_memberships]
25

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

34
    lists = []
35

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

57
    return lists
58

59

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

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