views.py 2.55 KB
Newer Older
1
"""Views provided by the newsletters package"""
2
3
4
import os

from django.conf import settings
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
5
from django.contrib.admin.views.decorators import staff_member_required
6
from django.contrib.auth.decorators import permission_required
7
from django.shortcuts import get_object_or_404, redirect, render
8
from django.utils.translation import activate, get_language_info
9
from sendfile import sendfile
10

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


16
def preview(request, pk, lang=None):
17
18
    """
    View that renders the newsletter as HTML
19

20
21
22
23
24
    :param request: the request object
    :param pk: the newsletter's primary key
    :param lang: the language of the render
    :return: HttpResponse 200 containing the newsletter HTML
    """
25
26
27
28
29
30
31
32
33
34
    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
35

36
37
38
39
40
41
42
43
44
45
    # Send cached file, if it exists
    file_path = os.path.join(
        settings.MEDIA_ROOT,
        'newsletters',
        f'{pk}_{lang_code}.html'
    )
    if os.path.isfile(file_path):
        return sendfile(request, file_path)

    newsletter = get_object_or_404(Newsletter, pk=pk)
46
    events = services.get_agenda(newsletter.date) if newsletter.date else None
47

Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
48
49
    return render(request, 'newsletters/email.html', {
        'newsletter': newsletter,
50
        'agenda_events': events,
51
52
        'main_partner': Partner.objects.filter(is_main_partner=True).first(),
        'local_partner': Partner.objects.filter(is_local_partner=True).first(),
53
        'lang_code': lang_code
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
54
55
56
57
    })


@staff_member_required
58
@permission_required('newsletters.send_newsletter')
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
59
def admin_send(request, pk):
60
61
62
63
    """
    If this is a GET request this view will render a confirmation
    page for the administrator. If it is a POST request the newsletter
    will be sent to all recipients
64

65
66
67
    :param request: the request object
    :param pk: the newsletter's primary key
    :return: 302 RedirectResponse if POST else 200 with the
Joren Vrancken's avatar
Joren Vrancken committed
68
             confirmation page HTML
69
    """
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
70
71
72
73
74
75
    newsletter = get_object_or_404(Newsletter, pk=pk)

    if newsletter.sent:
        return redirect(newsletter)

    if request.POST:
76
        services.send_newsletter(newsletter)
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
77
78
79
80
81
82

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