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
85 return render_to_response('patchwork/registration-confirm.html')
89 context = PatchworkRequestContext(request)
91 if request.method == 'POST':
92 form = UserProfileForm(instance = request.user.get_profile(),
97 form = UserProfileForm(instance = request.user.get_profile())
99 context.project = request.user.get_profile().primary_project
100 context['bundles'] = Bundle.objects.filter(owner = request.user)
101 context['profileform'] = form
103 optout_query = '%s.%s IN (SELECT %s FROM %s)' % (
104 Person._meta.db_table,
105 Person._meta.get_field('email').column,
106 EmailOptout._meta.get_field('email').column,
107 EmailOptout._meta.db_table)
108 people = Person.objects.filter(user = request.user) \
109 .extra(select = {'is_optout': optout_query})
110 context['linked_emails'] = people
111 context['linkform'] = UserPersonLinkForm()
113 return render_to_response('patchwork/profile.html', context)
117 context = PatchworkRequestContext(request)
119 if request.method == 'POST':
120 form = UserPersonLinkForm(request.POST)
122 conf = EmailConfirmation(type = 'userperson',
124 email = form.cleaned_data['email'])
126 context['confirmation'] = conf
129 send_mail('Patchwork email address confirmation',
130 render_to_string('patchwork/user-link.mail',
132 settings.DEFAULT_FROM_EMAIL,
133 [form.cleaned_data['email']])
135 context['confirmation'] = None
136 context['error'] = 'An error occurred during confirmation. ' + \
137 'Please try again later'
139 form = UserPersonLinkForm()
140 context['linkform'] = form
142 return render_to_response('patchwork/user-link.html', context)
145 def link_confirm(request, conf):
146 context = PatchworkRequestContext(request)
149 person = Person.objects.get(email__iexact = conf.email)
150 except Person.DoesNotExist:
151 person = Person(email = conf.email)
153 person.link_to_user(conf.user)
157 context['person'] = person
159 return render_to_response('patchwork/user-link-confirm.html', context)
162 def unlink(request, person_id):
163 person = get_object_or_404(Person, id = person_id)
165 if request.method == 'POST':
166 if person.email != request.user.email:
170 url = django.core.urlresolvers.reverse('patchwork.views.user.profile')
171 return HttpResponseRedirect(url)
175 def todo_lists(request):
178 for project in Project.objects.all():
179 patches = request.user.get_profile().todo_patches(project = project)
180 if not patches.count():
183 todo_lists.append({'project': project, 'n_patches': patches.count()})
185 if len(todo_lists) == 1:
186 return todo_list(request, todo_lists[0]['project'].linkname)
188 context = PatchworkRequestContext(request)
189 context['todo_lists'] = todo_lists
190 context.project = request.user.get_profile().primary_project
191 return render_to_response('patchwork/todo-lists.html', context)
194 def todo_list(request, project_id):
195 project = get_object_or_404(Project, linkname = project_id)
196 patches = request.user.get_profile().todo_patches(project = project)
197 filter_settings = [(DelegateFilter,
198 {'delegate': request.user})]
200 context = generic_list(request, project,
201 'patchwork.views.user.todo_list',
202 view_args = {'project_id': project.linkname},
203 filter_settings = filter_settings,
206 context['action_required_states'] = \
207 State.objects.filter(action_required = True).all()
208 return render_to_response('patchwork/todo-list.html', context)