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