views.py 3.81 KB
Newer Older
1
2
from datetime import datetime, timedelta, date

Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
3
4
from django.conf import settings
from django.contrib.admin.views.decorators import staff_member_required
5
from django.contrib.auth.decorators import permission_required
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
6
from django.core.mail import EmailMultiAlternatives
7
from django.shortcuts import get_object_or_404, redirect, render
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
8
9
from django.template.loader import get_template
from django.utils import translation
10
11
from django.utils.translation import activate, get_language_info

Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
12
13
14
15
16
from members.models import Member
from newsletters.models import Newsletter
from partners.models import Partner


17
def preview(request, pk, lang=None):
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
18
19
20
    newsletter = get_object_or_404(Newsletter, pk=pk)
    partners = Partner.objects.filter(is_main_partner=True)
    main_partner = partners[0] if len(partners) > 0 else None
21
22
23
24
25
26
27
28
29
30
    lang_code = request.LANGUAGE_CODE

    if lang is not None:
        try:
            get_language_info(lang)
            activate(lang)
            lang_code = lang
        except KeyError:
            # Language code not recognised by get_language_info
            pass
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
31
32
33

    return render(request, 'newsletters/email.html', {
        'newsletter': newsletter,
34
        'agenda_events': newsletter.newslettercontent_set.filter(
35
            newsletteritem=None).order_by('newsletterevent__start_datetime'),
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
36
        'main_partner': main_partner,
37
        'lang_code': lang_code
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
38
39
40
    })


41
42
43
44
45
46
47
48
49
50
51
def legacy_redirect(request, year, week):
    newsletter_date = datetime.strptime(
        '%s-%s-1' % (year, week), '%Y-%W-%w')
    if date(int(year), 1, 4).isoweekday() > 4:
        newsletter_date -= timedelta(days=7)

    newsletter = get_object_or_404(Newsletter, date=newsletter_date)

    return redirect(newsletter.get_absolute_url(), permanent=True)


Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
52
@staff_member_required
53
@permission_required('newsletters.send_newsletter')
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def admin_send(request, pk):
    newsletter = get_object_or_404(Newsletter, pk=pk)

    if newsletter.sent:
        return redirect(newsletter)

    if request.POST:
        partners = Partner.objects.filter(is_main_partner=True)
        main_partner = partners[0] if len(partners) > 0 else None

        from_email = settings.NEWSLETTER_FROM_ADDRESS
        html_template = get_template('newsletters/email.html')
        text_template = get_template('newsletters/email.txt')

        for language in settings.LANGUAGES:
            translation.activate(language[0])

71
            recipients = [member.email for member in
72
                          Member.current_members.all().filter(
73
74
                              profile__receive_newsletter=True,
                              profile__language=language[0])
75
                          if member.email]
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
76

77
            subject = '[THALIA] ' + newsletter.title
78

Thom Wiggers's avatar
Thom Wiggers committed
79
            context = {
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
80
                'newsletter': newsletter,
81
82
83
84
85
                'agenda_events': (
                    newsletter.newslettercontent_set
                    .filter(newsletteritem=None)
                    .order_by('newsletterevent__start_datetime')
                ),
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
86
                'main_partner': main_partner,
87
                'lang_code': language[0],
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
88
                'request': request
Thom Wiggers's avatar
Thom Wiggers committed
89
            }
90
91
92

            html_message = html_template.render(context)
            text_message = text_template.render(context)
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

            msg = EmailMultiAlternatives(subject, text_message,
                                         to=[from_email],
                                         bcc=recipients,
                                         from_email=from_email)
            msg.attach_alternative(html_message, "text/html")
            msg.send()

            translation.deactivate()

        newsletter.sent = True
        newsletter.save()

        return redirect('admin:newsletters_newsletter_changelist')
    else:
        return render(request, 'newsletters/admin/send_confirm.html', {
            'newsletter': newsletter
        })