Language in API.
[wolnelektury.git] / src / newsletter / views.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from django.http import Http404
5 from django.http.response import HttpResponseRedirect
6 from django.shortcuts import render, get_object_or_404
7 from django.urls import reverse
8 from django.utils.translation import ugettext_lazy as _
9
10 from newsletter.forms import UnsubscribeForm, SubscribeForm
11 from newsletter.models import Subscription
12
13
14 def subscribe_form(request, mailing_list='general', title=None):
15     if request.POST:
16         form = SubscribeForm(mailing_list, request.POST)
17         if form.is_valid():
18             form.save()
19             return HttpResponseRedirect(reverse('subscribed'))
20     else:
21         form = SubscribeForm(mailing_list)
22     return render(request, 'newsletter/subscribe_form.html', {
23         'page_title': title or _('Subscribe To Newsletter'),
24         'form': form,
25     })
26
27
28 def subscribed(request):
29     return render(request, 'newsletter/subscribed.html', {
30         'page_title': _('Subscribed'),
31     })
32
33
34 def check_subscription(subscription, hashcode):
35     if hashcode != subscription.hashcode():
36         raise Http404
37
38
39 def confirm_subscription(request, subscription_id, hashcode):
40     subscription = get_object_or_404(Subscription, id=subscription_id)
41     check_subscription(subscription, hashcode)
42     subscription.active = True
43     subscription.save()
44     return render(request, 'newsletter/confirm_subscription.html', {
45         'page_title': _('Subscription confirmed')
46     })
47
48
49 def unsubscribe_form(request):
50     if request.POST:
51         form = UnsubscribeForm(request.POST)
52         if form.is_valid():
53             form.save()
54             return HttpResponseRedirect(reverse('unsubscribed'))
55     else:
56         form = UnsubscribeForm()
57     return render(request, 'newsletter/unsubscribe_form.html', {
58         'page_title': _('Unsubscribe'),
59         'form': form,
60     })
61
62
63 def unsubscribed(request):
64     return render(request, 'newsletter/unsubscribed.html', {
65         'page_title': _('Unsubscribed'),
66     })