1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2010 Jeremy Kerr <jk@ozlabs.org>
4 # This file is part of the Patchwork package.
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.
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.
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
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
30 def settings(request):
31 context = PatchworkRequestContext(request)
32 if request.method == 'POST':
33 form = EmailForm(data = request.POST)
35 email = form.cleaned_data['email']
36 is_optout = EmailOptout.objects.filter(email = email).count() > 0
39 'is_optout': is_optout,
41 return render_to_response('patchwork/mail-settings.html', context)
45 context['form'] = form
46 return render_to_response('patchwork/mail-form.html', context)
48 def optout_confirm(request, conf):
49 context = PatchworkRequestContext(request)
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)
58 context['email'] = conf.email
60 return render_to_response('patchwork/optout.html', context)
62 def optin_confirm(request, conf):
63 context = PatchworkRequestContext(request)
65 email = conf.email.strip().lower()
66 EmailOptout.objects.filter(email = email).delete()
69 context['email'] = conf.email
71 return render_to_response('patchwork/optin.html', context)
73 def optinout(request, action, description):
74 context = PatchworkRequestContext(request)
76 mail_template = 'patchwork/%s-request.mail' % action
77 html_template = 'patchwork/%s-request.html' % action
79 if request.method != 'POST':
80 return HttpResponseRedirect(reverse(settings))
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.') % \
87 context['form'] = form
88 return render_to_response(html_template, context)
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)
99 conf = EmailConfirmation(type = action, email = email)
101 context['confirmation'] = conf
102 mail = render_to_string(mail_template, context)
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
113 return render_to_response(html_template, context)
116 return optinout(request, 'optout', 'opt-out')
119 return optinout(request, 'optin', 'opt-in')