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.http import HttpResponse, HttpResponseRedirect
26 from patchwork.models import Project, Patch, Bundle, Person, \
27 RegistrationRequest, UserProfile, UserPersonConfirmation, State
28 from patchwork.forms import RegisterForm, LoginForm, MultiplePatchForm, \
29 UserProfileForm, UserPersonLinkForm
30 from patchwork.utils import Order, get_patch_ids
31 from patchwork.filters import DelegateFilter
32 from patchwork.paginator import Paginator
33 from patchwork.views import generic_list
34 from django.template.loader import render_to_string
35 from django.template import Context
36 from django.conf import settings
37 from django.core.mail import send_mail
38 import django.core.urlresolvers
40 def register(request):
41 context = PatchworkRequestContext(request)
42 template = 'patchwork/register.html'
44 if request.method != 'POST':
46 context['form'] = form
47 return render_to_response(template, context)
49 reg_req = RegistrationRequest()
50 form = RegisterForm(instance = reg_req, data = request.POST)
55 context['request'] = reg_req
56 send_mail('Patchwork account confirmation',
57 render_to_string('patchwork/register.mail', context),
58 settings.PATCHWORK_FROM_EMAIL,
59 [form.cleaned_data['email']])
62 context['request'] = None
63 context['error'] = 'An error occurred during registration. ' + \
64 'Please try again later'
66 context['form'] = form
68 return render_to_response(template, context)
70 def register_confirm(request, key):
71 context = PatchworkRequestContext(request)
72 req = get_object_or_404(RegistrationRequest, key = key)
74 user = auth.authenticate(username = req.username, password = req.password)
75 auth.login(request, user)
77 return render_to_response('patchwork/register-confirm.html', context)
80 context = PatchworkRequestContext(request)
81 template = 'patchwork/login.html'
84 if request.method == 'POST':
85 form = LoginForm(request.POST)
86 context['form'] = form
88 if not form.is_valid():
89 return render_to_response(template, context)
91 data = form.cleaned_data
92 user = auth.authenticate(username = data['username'],
93 password = data['password'])
95 if user is not None and user.is_active:
96 auth.login(request, user)
97 url = request.POST.get('next', None) or \
98 django.core.urlresolvers.reverse( \
99 'patchwork.views.user.profile')
100 return HttpResponseRedirect(url)
102 context['error'] = 'Invalid username or password'
105 context['form'] = LoginForm()
107 return render_to_response(template, context)
111 return render_to_response('patchwork/logout.html')
114 def profile(request):
115 context = PatchworkRequestContext(request)
117 if request.method == 'POST':
118 form = UserProfileForm(instance = request.user.get_profile(),
123 form = UserProfileForm(instance = request.user.get_profile())
125 context.project = request.user.get_profile().primary_project
126 context['bundles'] = Bundle.objects.filter(owner = request.user)
127 context['profileform'] = form
129 people = Person.objects.filter(user = request.user)
130 context['linked_emails'] = people
131 context['linkform'] = UserPersonLinkForm()
133 return render_to_response('patchwork/profile.html', context)
137 context = PatchworkRequestContext(request)
139 form = UserPersonLinkForm(request.POST)
140 if request.method == 'POST':
141 form = UserPersonLinkForm(request.POST)
143 conf = UserPersonConfirmation(user = request.user,
144 email = form.cleaned_data['email'])
145 context['confirmation'] = conf
148 send_mail('Patchwork email address confirmation',
149 render_to_string('patchwork/user-link.mail',
151 settings.PATCHWORK_FROM_EMAIL,
152 [form.cleaned_data['email']])
154 except Exception, ex:
155 context['confirmation'] = None
156 context['error'] = 'An error occurred during confirmation. ' + \
157 'Please try again later'
158 context['linkform'] = form
160 return render_to_response('patchwork/user-link.html', context)
163 def link_confirm(request, key):
164 context = PatchworkRequestContext(request)
165 confirmation = get_object_or_404(UserPersonConfirmation, key = key)
167 errors = confirmation.confirm()
169 context['errors'] = errors
171 context['person'] = Person.objects.get(email = confirmation.email)
173 return render_to_response('patchwork/user-link-confirm.html', context)
176 def unlink(request, person_id):
177 context = PatchworkRequestContext(request)
178 person = get_object_or_404(Person, id = person_id)
180 if request.method == 'POST':
181 if person.email != request.user.email:
185 url = django.core.urlresolvers.reverse('patchwork.views.user.profile')
186 return HttpResponseRedirect(url)
190 def todo_lists(request):
193 for project in Project.objects.all():
194 patches = request.user.get_profile().todo_patches(project = project)
195 if not patches.count():
198 todo_lists.append({'project': project, 'n_patches': patches.count()})
200 if len(todo_lists) == 1:
201 return todo_list(request, todo_lists[0]['project'].linkname)
203 context = PatchworkRequestContext(request)
204 context['todo_lists'] = todo_lists
205 context.project = request.user.get_profile().primary_project
206 return render_to_response('patchwork/todo-lists.html', context)
209 def todo_list(request, project_id):
210 project = get_object_or_404(Project, linkname = project_id)
211 patches = request.user.get_profile().todo_patches(project = project)
212 filter_settings = [(DelegateFilter,
213 {'delegate': request.user})]
215 context = generic_list(request, project,
216 'patchwork.views.user.todo_list',
217 view_args = {'project_id': project.linkname},
218 filter_settings = filter_settings,
221 context['action_required_states'] = \
222 State.objects.filter(action_required = True).all()
223 return render_to_response('patchwork/todo-list.html', context)