emails.py 2.18 KB
Newer Older
1
2
3
4
5
6
from django.core.mail import EmailMessage
from django.template.loader import get_template
from django.utils import translation
from django.utils.translation import ugettext_lazy as _

from events.models import Registration
7
from members.models import Profile
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from thaliawebsite.templatetags import baseurl


def notify_first_waiting(request, event):
    if (event.max_participants is not None and
        Registration.objects
                    .filter(event=event, date_cancelled=None)
                    .count() > event.max_participants):
        # Prepare email to send to the first person on the waiting list
        first_waiting = (Registration.objects
                         .filter(event=event, date_cancelled=None)
                         .order_by('date')[event.max_participants])
        first_waiting_member = first_waiting.member

22
        text_template = get_template('events/member_email.txt')
23

24
        if first_waiting_member.profile:
Thom Wiggers's avatar
Thom Wiggers committed
25
            language = first_waiting_member.profile.language
26
        else:
27
            language = Profile._meta.get_field('language').default
28
29

        with translation.override(language):
30
31
32
33
34
35
36
            subject = _("[THALIA] Notification about your "
                        "registration for '{}'").format(
                event.title)
            text_message = text_template.render({
                'event': event,
                'reg': first_waiting,
                'member': first_waiting_member,
37
                'base_url': baseurl.baseurl(context={'request': request})
38
39
40
41
42
            })

            EmailMessage(
                subject,
                text_message,
43
                to=[first_waiting_member.email]
44
            ).send()
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63


def notify_organiser(event, registration):
    if event.organiser is None or event.organiser.contact_mailinglist is None:
        return

    text_template = get_template('events/organiser_email.txt')
    subject = 'Registration for {} cancelled by member'.format(
        event.title)
    text_message = text_template.render({
        'event': event,
        'registration': registration
    })

    EmailMessage(
        subject,
        text_message,
        to=[event.organiser.contact_mailinglist.name + "@thalia.nu"]
    ).send()