1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 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
21 from django.contrib.auth.decorators import login_required
22 from patchwork.requestcontext import PatchworkRequestContext
23 from django.shortcuts import render_to_response, get_object_or_404
24 from django.contrib import auth
25 from django.contrib.sites.models import Site
26 from django.http import HttpResponseRedirect
27 from patchwork.models import Project, Bundle, Person, EmailConfirmation, \
29 from patchwork.forms import UserProfileForm, UserPersonLinkForm, \
31 from patchwork.filters import DelegateFilter
32 from patchwork.views import generic_list
33 from django.template.loader import render_to_string
34 from django.conf import settings
35 from django.core.mail import send_mail
36 import django.core.urlresolvers
38 def register(request):
39 context = PatchworkRequestContext(request)
40 if request.method == 'POST':
41 form = RegistrationForm(request.POST)
43 data = form.cleaned_data
44 # create inactive user
45 user = auth.models.User.objects.create_user(data['username'],
48 user.is_active = False;
49 user.first_name = data.get('first_name', '')
50 user.last_name = data.get('last_name', '')
54 conf = EmailConfirmation(type = 'registration', user = user,
59 mail_ctx = {'site': Site.objects.get_current(),
62 subject = render_to_string('patchwork/activation_email_subject.txt',
63 mail_ctx).replace('\n', ' ').strip()
65 message = render_to_string('patchwork/activation_email.txt',
68 send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
71 # setting 'confirmation' in the template indicates success
72 context['confirmation'] = conf
75 form = RegistrationForm()
77 return render_to_response('patchwork/registration_form.html',
79 context_instance=context)
81 def register_confirm(request, conf):
82 conf.user.is_active = True
86 person = Person.objects.get(email__iexact = conf.user.email)
87 except Person.DoesNotExist:
88 person = Person(email = conf.user.email,
89 name = conf.user.profile.name())
90 person.user = conf.user
93 return render_to_response('patchwork/registration-confirm.html')
97 context = PatchworkRequestContext(request)
99 if request.method == 'POST':
100 form = UserProfileForm(instance = request.user.profile,
105 form = UserProfileForm(instance = request.user.profile)
107 context.project = request.user.profile.primary_project
108 context['bundles'] = Bundle.objects.filter(owner = request.user)
109 context['profileform'] = form
111 optout_query = '%s.%s IN (SELECT %s FROM %s)' % (
112 Person._meta.db_table,
113 Person._meta.get_field('email').column,
114 EmailOptout._meta.get_field('email').column,
115 EmailOptout._meta.db_table)
116 people = Person.objects.filter(user = request.user) \
117 .extra(select = {'is_optout': optout_query})
118 context['linked_emails'] = people
119 context['linkform'] = UserPersonLinkForm()
121 return render_to_response('patchwork/profile.html', context)
125 context = PatchworkRequestContext(request)
127 if request.method == 'POST':
128 form = UserPersonLinkForm(request.POST)
130 conf = EmailConfirmation(type = 'userperson',
132 email = form.cleaned_data['email'])
134 context['confirmation'] = conf
137 send_mail('Patchwork email address confirmation',
138 render_to_string('patchwork/user-link.mail',
140 settings.DEFAULT_FROM_EMAIL,
141 [form.cleaned_data['email']])
143 context['confirmation'] = None
144 context['error'] = 'An error occurred during confirmation. ' + \
145 'Please try again later'
147 form = UserPersonLinkForm()
148 context['linkform'] = form
150 return render_to_response('patchwork/user-link.html', context)
153 def link_confirm(request, conf):
154 context = PatchworkRequestContext(request)
157 person = Person.objects.get(email__iexact = conf.email)
158 except Person.DoesNotExist:
159 person = Person(email = conf.email)
161 person.link_to_user(conf.user)
165 context['person'] = person
167 return render_to_response('patchwork/user-link-confirm.html', context)
170 def unlink(request, person_id):
171 person = get_object_or_404(Person, id = person_id)
173 if request.method == 'POST':
174 if person.email != request.user.email:
178 url = django.core.urlresolvers.reverse('patchwork.views.user.profile')
179 return HttpResponseRedirect(url)
183 def todo_lists(request):
186 for project in Project.objects.all():
187 patches = request.user.profile.todo_patches(project = project)
188 if not patches.count():
191 todo_lists.append({'project': project, 'n_patches': patches.count()})
193 if len(todo_lists) == 1:
194 return todo_list(request, todo_lists[0]['project'].linkname)
196 context = PatchworkRequestContext(request)
197 context['todo_lists'] = todo_lists
198 context.project = request.user.profile.primary_project
199 return render_to_response('patchwork/todo-lists.html', context)
202 def todo_list(request, project_id):
203 project = get_object_or_404(Project, linkname = project_id)
204 patches = request.user.profile.todo_patches(project = project)
205 filter_settings = [(DelegateFilter,
206 {'delegate': request.user})]
208 context = generic_list(request, project,
209 'patchwork.views.user.todo_list',
210 view_args = {'project_id': project.linkname},
211 filter_settings = filter_settings,
214 context['action_required_states'] = \
215 State.objects.filter(action_required = True).all()
216 return render_to_response('patchwork/todo-list.html', context)