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
9
10
11
12
13
14
15
16
17
18
19
20
from django.utils.translation import ugettext as _

from members import models
from thaliawebsite.settings import settings


def send_membership_announcement(dry_run=False):
    members = (models.Member.active_members
               .filter(user__membership__until__isnull=True))

    with mail.get_connection() as connection:
        for member in members:
            print("Send email to {} ({})".format(member.get_full_name(),
                                                 member.user.email))
21
22
23
24
25
26
27
28
29
30
31
32
33
            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,
                        [member.user.email],
                        bcc=[settings.BOARD_NOTIFICATION_ADDRESS],
                        connection=connection
                    ).send()
34
35
36
37
38
39
40
41
42

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


def send_information_request(dry_run=False):
    members = models.Member.active_members.all()

    with mail.get_connection() as connection:
        for member in members:
            print("Send email to {} ({})".format(member.get_full_name(),
                                                 member.user.email))
52
53
54
55
56
57
58
59
60
61
62
63
64
65
            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,
                        [member.user.email],
                        bcc=[settings.BOARD_NOTIFICATION_ADDRESS],
                        connection=connection
                    ).send()
66
67
68

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


def send_expiration_announcement(dry_run=False):
    expiry_date = datetime.now() + timedelta(days=31)
    members = (models.Member.active_members
               .filter(user__membership__until__lte=expiry_date))

    with mail.get_connection() as connection:
        for member in members:
84
85
            print("Send email to {} ({})".format(member.get_full_name(),
                                                 member.user.email))
86
87
88
89
90
91
92
93
94
95
96
97
98
            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,
                        [member.user.email],
                        bcc=[settings.BOARD_NOTIFICATION_ADDRESS],
                        connection=connection
                    ).send()
99
100
101
102
103
104
105
106
107

        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,
            )