emails.py 7.3 KB
Newer Older
1
from django.conf import settings
2
from datetime import timedelta
3
4
from django.core import mail
from django.template import loader
5
6
from django.template.defaultfilters import floatformat
from django.urls import reverse
7
from django.utils import translation
8
from django.utils.datetime_safe import datetime
9
10
from django.utils.translation import ugettext as _

11
from members.models import Member, Membership
12
13
14


def send_membership_announcement(dry_run=False):
15
    members = (Member.current_members
16
               .filter(membership__until__isnull=True)
17
               .exclude(membership__type=Membership.HONORARY)
18
               .distinct())
19
20
21
22

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

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


def send_information_request(dry_run=False):
50
    members = Member.current_members.all()
51
52
53
54

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

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


def send_expiration_announcement(dry_run=False):
    expiry_date = datetime.now() + timedelta(days=31)
84
    members = (Member.current_members
85
               .filter(membership__until__lte=expiry_date)
86
               .exclude(membership__until__isnull=True)
87
               .distinct())
88
89
90

    with mail.get_connection() as connection:
        for member in members:
91
            print("Send email to {} ({})".format(member.get_full_name(),
92
                                                 member.email))
93
            if not dry_run:
94
                with translation.override(member.profile.language):
95
96
                    email_body = loader.render_to_string(
                        'members/email/expiration_announcement.txt',
97
98
99
                        {
                            'name': member.get_full_name(),
                            'membership_price': floatformat(
100
                                settings.MEMBERSHIP_PRICES['year'], 2
101
                            ),
102
103
104
105
                            'renewal_url': '{}{}'.format(
                                'https://thalia.nu',
                                reverse('registrations:renew')
                            )
106
                        })
107
                    mail.EmailMessage(
108
109
                        '[THALIA] {}'.format(
                            _('Membership expiration announcement')),
110
111
                        email_body,
                        settings.WEBSITE_FROM_ADDRESS,
112
                        [member.email],
113
114
115
                        bcc=[settings.BOARD_NOTIFICATION_ADDRESS],
                        connection=connection
                    ).send()
116
117
118
119
120
121
122
123
124

        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,
            )
125
126
127
128
129
130
131
132
133
134
135
136
137
138


def send_welcome_message(user, password, language):
    with translation.override(language):
        email_body = loader.render_to_string(
            'members/email/welcome.txt',
            {
                'full_name': user.get_full_name(),
                'username': user.username,
                'password': password
            })
        user.email_user(
            _('Welcome to Study Association Thalia'),
            email_body)
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158


def send_email_change_confirmation_messages(change_request):
    member = change_request.member
    with translation.override(member.profile.language):
        mail.EmailMessage(
            '[THALIA] {}'.format(_('Please confirm your email change')),
            loader.render_to_string(
                'members/email/email_change_confirm.txt',
                {
                    'confirm_link': '{}{}'.format(
                        'https://thalia.nu',
                        reverse(
                            'members:email-change-confirm',
                            args=[change_request.confirm_key]
                        )),
                    'name': member.first_name
                }
            ),
            settings.WEBSITE_FROM_ADDRESS,
159
            [member.email]
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
        ).send()

        mail.EmailMessage(
            '[THALIA] {}'.format(_('Please verify your email address')),
            loader.render_to_string(
                'members/email/email_change_verify.txt',
                {
                    'confirm_link': '{}{}'.format(
                        'https://thalia.nu',
                        reverse(
                            'members:email-change-verify',
                            args=[change_request.verify_key]
                        )),
                    'name': member.first_name
                }
            ),
            settings.WEBSITE_FROM_ADDRESS,
            [change_request.email]
        ).send()


def send_email_change_completion_message(change_request):
    change_request.member.email_user(
        '[THALIA] {}'.format(_('Your email address has been changed')),
        loader.render_to_string(
            'members/email/email_change_completed.txt',
            {
                'name': change_request.member.first_name
            }
        ))