]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/mail.py
Add email opt-out system
[patchwork] / apps / patchwork / views / mail.py
1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2010 Jeremy Kerr <jk@ozlabs.org>
3 #
4 # This file is part of the Patchwork package.
5 #
6 # Patchwork is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # Patchwork is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Patchwork; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 from patchwork.requestcontext import PatchworkRequestContext
21 from patchwork.models import EmailOptout, EmailConfirmation
22 from patchwork.forms import OptinoutRequestForm, EmailForm
23 from django.shortcuts import render_to_response
24 from django.template.loader import render_to_string
25 from django.conf import settings as conf_settings
26 from django.core.mail import send_mail
27 from django.core.urlresolvers import reverse
28 from django.http import HttpResponseRedirect
29
30 def settings(request):
31     context = PatchworkRequestContext(request)
32     if request.method == 'POST':
33         form = EmailForm(data = request.POST)
34         if form.is_valid():
35             email = form.cleaned_data['email']
36             is_optout = EmailOptout.objects.filter(email = email).count() > 0
37             context.update({
38                 'email': email,
39                 'is_optout': is_optout,
40             })
41             return render_to_response('patchwork/mail-settings.html', context)
42
43     else:
44         form = EmailForm()
45     context['form'] = form
46     return render_to_response('patchwork/mail-form.html', context)
47
48 def optout_confirm(request, conf):
49     context = PatchworkRequestContext(request)
50
51     email = conf.email.strip().lower()
52     # silently ignore duplicated optouts
53     if EmailOptout.objects.filter(email = email).count() == 0:
54         optout = EmailOptout(email = email)
55         optout.save()
56
57     conf.deactivate()
58     context['email'] = conf.email
59
60     return render_to_response('patchwork/optout.html', context)
61
62 def optin_confirm(request, conf):
63     context = PatchworkRequestContext(request)
64
65     email = conf.email.strip().lower()
66     EmailOptout.objects.filter(email = email).delete()
67
68     conf.deactivate()
69     context['email'] = conf.email
70
71     return render_to_response('patchwork/optin.html', context)
72
73 def optinout(request, action, description):
74     context = PatchworkRequestContext(request)
75
76     mail_template = 'patchwork/%s-request.mail' % action
77     html_template = 'patchwork/%s-request.html' % action
78
79     if request.method != 'POST':
80         return HttpResponseRedirect(reverse(settings))
81
82     form = OptinoutRequestForm(data = request.POST)
83     if not form.is_valid():
84         context['error'] = ('There was an error in the %s form. ' +
85                            'Please review the form and re-submit.') % \
86                             description
87         context['form'] = form
88         return render_to_response(html_template, context)
89
90     email = form.cleaned_data['email']
91     if action == 'optin' and \
92             EmailOptout.objects.filter(email = email).count() == 0:
93         context['error'] = ('The email address %s is not on the ' +
94                             'patchwork opt-out list, so you don\'t ' +
95                             'need to opt back in') % email
96         context['form'] = form
97         return render_to_response(html_template, context)
98
99     conf = EmailConfirmation(type = action, email = email)
100     conf.save()
101     context['confirmation'] = conf
102     mail = render_to_string(mail_template, context)
103     try:
104         send_mail('Patchwork %s confirmation' % description, mail,
105                     conf_settings.DEFAULT_FROM_EMAIL, [email])
106         context['email'] = mail
107         context['email_sent'] = True
108     except Exception, ex:
109         context['error'] = 'An error occurred during confirmation . ' + \
110                            'Please try again later.'
111         context['admins'] = conf_settings.ADMINS
112
113     return render_to_response(html_template, context)
114
115 def optout(request):
116     return optinout(request, 'optout', 'opt-out')
117
118 def optin(request):
119     return optinout(request, 'optin', 'opt-in')