/
views.py
96 lines (80 loc) · 3.49 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
## Author: Adriano Monteiro Marques <adriano@umitproject.org>
##
## Copyright (C) 2011 S2S Network Consultoria e Tecnologia da Informacao LTDA
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU Affero General Public License as
## published by the Free Software Foundation, either version 3 of the
## License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Affero General Public License for more details.
##
## You should have received a copy of the GNU Affero General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from subscriptions.models import Subscription
from subscriptions.forms import SubscribeForm, UnsubscribeForm
from subscriptions.utils import send_subscribe_email
def index(request):
if 'success' in request.GET and request.GET['success']=='1':
success = True
else:
success = False
form = SubscribeForm()
return render_to_response('subscriptions/index.html', {
'form': form, 'success': success,
})
def add(request):
if request.method == 'POST':
form = SubscribeForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
# search if email was not registered yet
if Subscription.objects.filter(email=email):
return render_to_response('subscriptions/index.html', {
'form': form,
'error_message': 'Email already registered',
})
else:
sub = Subscription()
sub.email = email
sub.originating_ip = request.META['REMOTE_ADDR']
sub.save()
# Send email confirming and providing user with links to unsubscribe.
send_subscribe_email(sub)
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('subscriptions.views.index') + '?success=1')
else:
form = SubscribeForm()
return render_to_response('subscriptions/index.html', {
'form': form,
})
def remove(request):
if request.method == 'GET':
form = UnsubscribeForm(request.GET)
if form.is_valid():
email = form.cleaned_data['email']
id = form.cleaned_data['id']
# search if email is registered
subscription = Subscription.objects.filter(email=email, pk=id)
if subscription:
# This means user is registered. Let's unsubscribe him
sub = subscription[0]
sub.subscribed = False
sub.save()
return render_to_response('subscriptions/unsubscribed.html')
else:
# Doesn't exist... someone is trying to cheat on us. Let's pretend all is good in zion
return render_to_response('subscriptions/unsubscribed.html')
return index(request)