7fd67b278951363b837a3923d8e247b124dd9973
[wolnelektury.git] / src / newsletter / views.py
1 from django.http import Http404
2 from django.http.response import HttpResponseRedirect
3 from django.shortcuts import render, get_object_or_404
4 from django.urls import reverse
5 from django.utils.translation import ugettext_lazy as _
6
7 from newsletter.forms import UnsubscribeForm, SubscribeForm
8 from newsletter.models import Subscription
9
10
11 def subscribe_form(request):
12     if request.POST:
13         form = SubscribeForm(request.POST)
14         if form.is_valid():
15             form.save()
16             return HttpResponseRedirect(reverse('subscribed'))
17     else:
18         form = SubscribeForm()
19     return render(request, 'newsletter/subscribe_form.html', {
20         'page_title': _(u'Subscribe To Newsletter'),
21         'form': form,
22     })
23
24
25 def subscribed(request):
26     return render(request, 'newsletter/subscribed.html', {
27         'page_title': _(u'Subscribed'),
28     })
29
30
31 def check_subscription(subscription, hashcode):
32     if hashcode != subscription.hashcode():
33         raise Http404
34
35
36 def confirm_subscription(request, subscription_id, hashcode):
37     subscription = get_object_or_404(Subscription, id=subscription_id)
38     check_subscription(subscription, hashcode)
39     subscription.active = True
40     subscription.save()
41     return render(request, 'newsletter/confirm_subscription.html', {
42         'page_title': _(u'Subscription confirmed')
43     })
44
45
46 def unsubscribe_form(request):
47     if request.POST:
48         form = UnsubscribeForm(request.POST)
49         if form.is_valid():
50             form.save()
51             return HttpResponseRedirect(reverse('unsubscribed'))
52     else:
53         form = UnsubscribeForm()
54     return render(request, 'newsletter/unsubscribe_form.html', {
55         'page_title': _(u'Unsubscribe'),
56         'form': form,
57     })
58
59
60 def unsubscribed(request):
61     return render(request, 'newsletter/unsubscribed.html', {
62         'page_title': _(u'Unsubscribed'),
63     })