emails.py 9.6 KB
Newer Older
1
"""The emails defined by the members package"""
2
from datetime import timedelta
3
4
5
import logging

from django.conf import settings
6
7
from django.core import mail
from django.template import loader
8
9
from django.template.defaultfilters import floatformat
from django.urls import reverse
10
from django.utils import timezone, translation
11
12
from django.utils.translation import ugettext as _

13
from members.models import Member, Membership
14

15
16
17
logger = logging.getLogger(__name__)


18
def send_membership_announcement(dry_run=False):
19
20
21
22
23
24
    """
    Sends an email to all members with a never ending membership
    excluding honorary members

    :param dry_run: does not really send emails if True
    """
25
    members = (Member.current_members
26
               .filter(membership__since__lt=timezone.now())
27
               .filter(membership__until__isnull=True)
28
               .exclude(membership__type=Membership.HONORARY)
29
               .exclude(email='')
30
               .distinct())
31
32
33

    with mail.get_connection() as connection:
        for member in members:
34
35
36
            logger.info("Sent email to %s (%s)",
                        member.get_full_name(),
                        member.email)
37
            if not dry_run:
Thom Wiggers's avatar
Thom Wiggers committed
38
                with translation.override(member.profile.language):
39
40
41
42
                    email_body = loader.render_to_string(
                        'members/email/membership_announcement.txt',
                        {'name': member.get_full_name()})
                    mail.EmailMessage(
43
44
                        '[THALIA] {}'.format(
                            _('Membership announcement')),
45
                        email_body,
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
46
                        settings.DEFAULT_FROM_EMAIL,
47
                        [member.email],
48
49
50
                        bcc=[settings.BOARD_NOTIFICATION_ADDRESS],
                        connection=connection
                    ).send()
51
52
53
54
55
56
57
58
59

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


def send_information_request(dry_run=False):
63
64
65
66
67
    """
    Sends an email to all members to have them check their personal information

    :param dry_run: does not really send emails if True
    """
68
    members = Member.current_members.all().exclude(email='')
69
70
71

    with mail.get_connection() as connection:
        for member in members:
72
73
74
            logger.info("Sent email to %s (%s)",
                        member.get_full_name(),
                        member.email)
75
            if not dry_run:
Thom Wiggers's avatar
Thom Wiggers committed
76
                with translation.override(member.profile.language):
77
78
79
80
81
82
83
84
85
86
                    email_context = {
                        k: x if x else '' for k, x in {
                            'name': member.first_name,
                            'username': member.username,
                            'full_name': member.get_full_name(),
                            'address_street': member.profile.address_street,
                            'address_street2': member.profile.address_street2,
                            'address_postal_code':
                                member.profile.address_postal_code,
                            'address_city': member.profile.address_city,
87
88
                            'address_country':
                                member.profile.get_address_country_display(),
89
90
91
92
93
94
95
96
                            'phone_number': member.profile.phone_number,
                            'birthday': member.profile.birthday,
                            'email': member.email,
                            'student_number': member.profile.student_number,
                            'starting_year': member.profile.starting_year,
                            'programme': member.profile.get_programme_display()
                        }.items()
                    }
97
98
                    email_body = loader.render_to_string(
                        'members/email/information_check.txt',
99
                        email_context)
100
                    mail.EmailMessage(
101
102
                        '[THALIA] {}'.format(
                            _('Membership information check')),
103
                        email_body,
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
104
                        settings.DEFAULT_FROM_EMAIL,
105
                        [member.email],
106
107
                        connection=connection
                    ).send()
108
109
110

        if not dry_run:
            mail.mail_managers(
111
                _('Membership information check sent'),
112
                loader.render_to_string(
113
                    'members/email/information_check_notification.txt',
114
115
116
                    {'members': members}),
                connection=connection,
            )
117
118
119


def send_expiration_announcement(dry_run=False):
120
121
122
123
124
125
    """
    Sends an email to all members whose membership will end in the next 31 days
    to warn them about this

    :param dry_run: does not really send emails if True
    """
126
    expiry_date = timezone.now() + timedelta(days=31)
127
    members = (Member.current_members
128
               .filter(membership__until__lte=expiry_date)
129
               .exclude(membership__until__isnull=True)
130
               .exclude(email='')
131
               .distinct())
132
133
134

    with mail.get_connection() as connection:
        for member in members:
135
136
137
            logger.info("Sent email to %s (%s)",
                        member.get_full_name(),
                        member.email)
138
            if not dry_run:
Thom Wiggers's avatar
Thom Wiggers committed
139
                with translation.override(member.profile.language):
140
141
                    email_body = loader.render_to_string(
                        'members/email/expiration_announcement.txt',
142
143
144
                        {
                            'name': member.get_full_name(),
                            'membership_price': floatformat(
145
                                settings.MEMBERSHIP_PRICES['year'], 2
146
                            ),
147
                            'renewal_url': '{}{}'.format(
148
                                settings.BASE_URL,
149
150
                                reverse('registrations:renew')
                            )
151
                        })
152
                    mail.EmailMessage(
153
154
                        '[THALIA] {}'.format(
                            _('Membership expiration announcement')),
155
                        email_body,
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
156
                        settings.DEFAULT_FROM_EMAIL,
157
                        [member.email],
158
159
160
                        bcc=[settings.BOARD_NOTIFICATION_ADDRESS],
                        connection=connection
                    ).send()
161
162
163
164
165
166
167
168
169

        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,
            )
170
171
172


def send_welcome_message(user, password, language):
173
174
175
176
177
178
179
    """
    Sends an email to a new mail welcoming them

    :param user: the new user
    :param password: randomly generated password
    :param language: selected language during registration
    """
180
181
182
183
184
185
    with translation.override(language):
        email_body = loader.render_to_string(
            'members/email/welcome.txt',
            {
                'full_name': user.get_full_name(),
                'username': user.username,
186
187
                'password': password,
                'url': settings.BASE_URL
188
189
190
191
            })
        user.email_user(
            _('Welcome to Study Association Thalia'),
            email_body)
192
193
194


def send_email_change_confirmation_messages(change_request):
195
196
197
198
199
200
    """
    Sends emails to the old and new email address of a member to
    confirm the email change

    :param change_request the email change request entered by the user
    """
201
202
203
204
205
206
207
208
    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(
209
                        settings.BASE_URL,
210
211
212
213
214
215
216
                        reverse(
                            'members:email-change-confirm',
                            args=[change_request.confirm_key]
                        )),
                    'name': member.first_name
                }
            ),
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
217
            settings.DEFAULT_FROM_EMAIL,
218
            [member.email]
219
220
221
222
223
224
225
226
        ).send()

        mail.EmailMessage(
            '[THALIA] {}'.format(_('Please verify your email address')),
            loader.render_to_string(
                'members/email/email_change_verify.txt',
                {
                    'confirm_link': '{}{}'.format(
227
                        settings.BASE_URL,
228
229
230
231
232
233
234
                        reverse(
                            'members:email-change-verify',
                            args=[change_request.verify_key]
                        )),
                    'name': member.first_name
                }
            ),
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
235
            settings.DEFAULT_FROM_EMAIL,
236
237
238
239
240
            [change_request.email]
        ).send()


def send_email_change_completion_message(change_request):
241
242
243
244
245
    """
    Sends email to the member to confirm the email change

    :param change_request the email change request entered by the user
    """
246
247
248
249
250
251
252
253
    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
            }
        ))