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, set_patches
31 from patchwork.filters import DelegateFilter
32 from patchwork.paginator import Paginator
33 from patchwork.views import generic_list
34 import django.core.urlresolvers
36 def register(request):
37 context = PatchworkRequestContext(request)
38 template = 'patchwork/register.html'
40 if request.method != 'POST':
42 context['form'] = form
43 return render_to_response(template, context)
45 reg_req = RegistrationRequest()
46 form = RegisterForm(instance = reg_req, data = request.POST)
50 context['request'] = reg_req
52 context['form'] = form
54 return render_to_response(template, context)
56 def register_confirm(request, key):
57 context = PatchworkRequestContext(request)
58 req = get_object_or_404(RegistrationRequest, key = key)
60 user = auth.authenticate(username = req.username, password = req.password)
61 auth.login(request, user)
63 return render_to_response('patchwork/register-confirm.html', context)
66 context = PatchworkRequestContext(request)
67 template = 'patchwork/login.html'
70 if request.method == 'POST':
71 form = LoginForm(request.POST)
72 context['form'] = form
74 if not form.is_valid():
75 return render_to_response(template, context)
77 data = form.cleaned_data
78 user = auth.authenticate(username = data['username'],
79 password = data['password'])
81 if user is not None and user.is_active:
82 auth.login(request, user)
83 url = request.POST.get('next', None) or \
84 django.core.urlresolvers.reverse( \
85 'patchwork.views.user.profile')
86 return HttpResponseRedirect(url)
88 context['error'] = 'Invalid username or password'
91 context['form'] = LoginForm()
93 return render_to_response(template, context)
97 return render_to_response('patchwork/logout.html')
100 def profile(request):
101 context = PatchworkRequestContext(request)
103 if request.method == 'POST':
104 form = UserProfileForm(instance = request.user.get_profile(),
109 form = UserProfileForm(instance = request.user.get_profile())
111 context.project = request.user.get_profile().primary_project
112 context['bundles'] = Bundle.objects.filter(owner = request.user)
113 context['profileform'] = form
115 people = Person.objects.filter(user = request.user)
116 context['linked_emails'] = people
117 context['linkform'] = UserPersonLinkForm()
119 return render_to_response('patchwork/profile.html', context)
123 context = PatchworkRequestContext(request)
125 form = UserPersonLinkForm(request.POST)
126 if request.method == 'POST':
127 form = UserPersonLinkForm(request.POST)
129 conf = UserPersonConfirmation(user = request.user,
130 email = form.cleaned_data['email'])
132 context['confirmation'] = conf
134 context['linkform'] = form
136 return render_to_response('patchwork/user-link.html', context)
139 def link_confirm(request, key):
140 context = PatchworkRequestContext(request)
141 confirmation = get_object_or_404(UserPersonConfirmation, key = key)
143 errors = confirmation.confirm()
145 context['errors'] = errors
147 context['person'] = Person.objects.get(email = confirmation.email)
149 confirmation.delete()
151 return render_to_response('patchwork/user-link-confirm.html', context)
154 def unlink(request, person_id):
155 context = PatchworkRequestContext(request)
156 person = get_object_or_404(Person, id = person_id)
158 if request.method == 'POST':
159 if person.email != request.user.email:
163 url = django.core.urlresolvers.reverse('patchwork.views.user.profile')
164 return HttpResponseRedirect(url)
168 def todo_lists(request):
171 for project in Project.objects.all():
172 patches = request.user.get_profile().todo_patches(project = project)
173 if not patches.count():
176 todo_lists.append({'project': project, 'n_patches': patches.count()})
178 if len(todo_lists) == 1:
179 return todo_list(request, todo_lists[0]['project'].linkname)
181 context = PatchworkRequestContext(request)
182 context['todo_lists'] = todo_lists
183 context.project = request.user.get_profile().primary_project
184 return render_to_response('patchwork/todo-lists.html', context)
187 def todo_list(request, project_id):
188 project = get_object_or_404(Project, linkname = project_id)
189 patches = request.user.get_profile().todo_patches(project = project)
190 filter_settings = [(DelegateFilter,
191 {'delegate': request.user})]
193 context = generic_list(request, project,
194 'patchwork.views.user.todo_list',
195 view_args = {'project_id': project.linkname},
196 filter_settings = filter_settings,
199 context['action_required_states'] = \
200 State.objects.filter(action_required = True).all()
201 return render_to_response('patchwork/todo-list.html', context)