emails.py 4.29 KB
Newer Older
1
from datetime import timedelta
2

3
4
5
from django.core import mail
from django.template import loader
from django.utils import translation
6
from django.utils.datetime_safe import datetime
7
8
from django.utils.translation import ugettext as _

9
from members.models import Member
10
11
12
13
from thaliawebsite.settings import settings


def send_membership_announcement(dry_run=False):
14
15
    members = (Member.active_members
               .filter(membership__until__isnull=True)
16
               .distinct())
17
18
19
20

    with mail.get_connection() as connection:
        for member in members:
            print("Send email to {} ({})".format(member.get_full_name(),
21
                                                 member.email))
22
23
24
25
26
27
28
29
30
            if not dry_run:
                with translation.override(member.language):
                    email_body = loader.render_to_string(
                        'members/email/membership_announcement.txt',
                        {'name': member.get_full_name()})
                    mail.EmailMessage(
                        _('Membership announcement'),
                        email_body,
                        settings.WEBSITE_FROM_ADDRESS,
31
                        [member.email],
32
33
34
                        bcc=[settings.BOARD_NOTIFICATION_ADDRESS],
                        connection=connection
                    ).send()
35
36
37
38
39
40
41
42
43

        if not dry_run:
            mail.mail_managers(
                _('Membership announcement sent'),
                loader.render_to_string(
                    'members/email/membership_announcement_notification.txt',
                    {'members': members}),
                connection=connection,
            )
44
45
46


def send_information_request(dry_run=False):
47
    members = Member.active_members.all()
48
49
50
51

    with mail.get_connection() as connection:
        for member in members:
            print("Send email to {} ({})".format(member.get_full_name(),
52
                                                 member.email))
53
54
55
56
57
58
59
60
61
62
            if not dry_run:
                with translation.override(member.language):
                    email_body = loader.render_to_string(
                        'members/email/information_check.txt',
                        {'name': member.get_full_name(),
                         'member': member})
                    mail.EmailMessage(
                        _('Membership information check'),
                        email_body,
                        settings.WEBSITE_FROM_ADDRESS,
63
                        [member.email],
64
65
66
                        bcc=[settings.BOARD_NOTIFICATION_ADDRESS],
                        connection=connection
                    ).send()
67
68
69

        if not dry_run:
            mail.mail_managers(
70
                _('Membership information check sent'),
71
                loader.render_to_string(
72
                    'members/email/information_check_notification.txt',
73
74
75
                    {'members': members}),
                connection=connection,
            )
76
77
78
79


def send_expiration_announcement(dry_run=False):
    expiry_date = datetime.now() + timedelta(days=31)
80
81
    members = (Member.active_members
               .filter(membership__until__lte=expiry_date)
82
               .distinct())
83
84
85

    with mail.get_connection() as connection:
        for member in members:
86
            print("Send email to {} ({})".format(member.get_full_name(),
87
                                                 member.email))
88
89
90
91
92
93
94
95
96
            if not dry_run:
                with translation.override(member.language):
                    email_body = loader.render_to_string(
                        'members/email/expiration_announcement.txt',
                        {'name': member.get_full_name()})
                    mail.EmailMessage(
                        _('Membership expiration announcement'),
                        email_body,
                        settings.WEBSITE_FROM_ADDRESS,
97
                        [member.email],
98
99
100
                        bcc=[settings.BOARD_NOTIFICATION_ADDRESS],
                        connection=connection
                    ).send()
101
102
103
104
105
106
107
108
109

        if not dry_run:
            mail.mail_managers(
                _('Membership expiration announcement sent'),
                loader.render_to_string(
                    'members/email/expiration_announcement_notification.txt',
                    {'members': members}),
                connection=connection,
            )