emails.py 3.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
from django.core import mail
from django.template import loader
from django.template.defaultfilters import floatformat
from django.urls import reverse
from django.utils import translation
from django.utils.translation import ugettext_lazy as _

from thaliawebsite.settings import settings

Thom Wiggers's avatar
Thom Wiggers committed
10
11
from . import models

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

def send_registration_email_confirmation(registration):
    with translation.override(registration.language):
        _send_email(
            registration.email,
            _('Confirm email address'),
            'registrations/email/registration_confirm_mail.txt',
            {
                'name': registration.get_full_name(),
                'confirm_link': '{}{}'.format(
                    'https://thalia.nu',
                    reverse('registrations:confirm-email',
                            args=[registration.pk])
                )
            }
        )


def send_registration_accepted_message(registration, payment):
    with translation.override(registration.language):
        _send_email(
            registration.email,
            _('Registration accepted'),
            'registrations/email/registration_accepted.txt',
            {
                'name': registration.get_full_name(),
                'fees': floatformat(payment.amount, 2)
            }
        )


def send_registration_rejected_message(registration):
    with translation.override(registration.language):
        _send_email(
            registration.email,
            _('Registration rejected'),
            'registrations/email/registration_rejected.txt',
            {
                'name': registration.get_full_name()
            }
        )


def send_new_registration_board_message(entry):
    try:
        _send_email(
            settings.BOARD_NOTIFICATION_ADDRESS,
            'New registration',
            'registrations/email/registration_board.txt',
            {
                'name': entry.registration.get_full_name(),
                'url': reverse('admin:registrations_registration_change',
                               args=[entry.registration.pk])
            }
        )
    except models.Registration.DoesNotExist:
        pass


def send_renewal_accepted_message(renewal, payment):
    with translation.override(renewal.member.profile.language):
        _send_email(
            renewal.member.email,
            _('Renewal accepted'),
            'registrations/email/renewal_accepted.txt',
            {
                'name': renewal.member.get_full_name(),
                'fees': floatformat(payment.amount, 2)
            }
        )


def send_renewal_rejected_message(renewal):
    with translation.override(renewal.member.profile.language):
        _send_email(
            renewal.member.email,
            _('Registration rejected'),
            'registrations/email/renewal_rejected.txt',
            {
                'name': renewal.member.get_full_name()
            }
        )


def send_renewal_complete_message(renewal):
    with translation.override(renewal.member.profile.language):
        _send_email(
            renewal.member.email,
            _('Renewal successful'),
            'registrations/email/renewal_complete.txt',
            {
                'name': renewal.member.get_full_name()
            }
        )


def send_new_renewal_board_message(renewal):
    _send_email(
        settings.BOARD_NOTIFICATION_ADDRESS,
        'New renewal',
        'registrations/email/renewal_board.txt',
        {
            'name': renewal.member.get_full_name(),
            'url': reverse('admin:registrations_renewal_change',
                           args=[renewal.pk])
        }
    )


def _send_email(to, subject, body_template, context):
    mail.EmailMessage(
        '[THALIA] {}'.format(subject),
        loader.render_to_string(body_template, context),
        settings.WEBSITE_FROM_ADDRESS,
        [to]
    ).send()