emails.py 9.45 KB
Newer Older
1
from datetime import timedelta
2
3
4
import logging

from django.conf import settings
5
6
from django.core import mail
from django.template import loader
7
8
from django.template.defaultfilters import floatformat
from django.urls import reverse
9
from django.utils import timezone, translation
10
from django.utils.datetime_safe import datetime
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
46
                        email_body,
                        settings.WEBSITE_FROM_ADDRESS,
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
87
88
89
90
91
92
93
94
                    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,
                            '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()
                    }
95
96
                    email_body = loader.render_to_string(
                        'members/email/information_check.txt',
97
                        email_context)
98
                    mail.EmailMessage(
99
100
                        '[THALIA] {}'.format(
                            _('Membership information check')),
101
102
                        email_body,
                        settings.WEBSITE_FROM_ADDRESS,
103
                        [member.email],
104
105
                        connection=connection
                    ).send()
106
107
108

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


def send_expiration_announcement(dry_run=False):
118
119
120
121
122
123
    """
    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
    """
124
    expiry_date = datetime.now() + timedelta(days=31)
125
    members = (Member.current_members
126
               .filter(membership__until__lte=expiry_date)
127
               .exclude(membership__until__isnull=True)
128
               .exclude(email='')
129
               .distinct())
130
131
132

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

        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,
            )
168
169
170


def send_welcome_message(user, password, language):
171
172
173
174
175
176
177
    """
    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
    """
178
179
180
181
182
183
184
185
186
187
188
    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)
189
190
191


def send_email_change_confirmation_messages(change_request):
192
193
194
195
196
197
    """
    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
    """
198
199
200
201
202
203
204
205
    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(
206
                        settings.BASE_URL,
207
208
209
210
211
212
213
214
                        reverse(
                            'members:email-change-confirm',
                            args=[change_request.confirm_key]
                        )),
                    'name': member.first_name
                }
            ),
            settings.WEBSITE_FROM_ADDRESS,
215
            [member.email]
216
217
218
219
220
221
222
223
        ).send()

        mail.EmailMessage(
            '[THALIA] {}'.format(_('Please verify your email address')),
            loader.render_to_string(
                'members/email/email_change_verify.txt',
                {
                    'confirm_link': '{}{}'.format(
224
                        settings.BASE_URL,
225
226
227
228
229
230
231
232
233
234
235
236
237
                        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):
238
239
240
241
242
    """
    Sends email to the member to confirm the email change

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