]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/user.py
223cfc6bcb4cd99f61b75fdbcac5e0ca9455e35a
[patchwork] / apps / patchwork / views / user.py
1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
3 #
4 # This file is part of the Patchwork package.
5 #
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.
10 #
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.
15 #
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
19
20
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
35
36 def register(request):
37     context = PatchworkRequestContext(request)
38     template = 'patchwork/register.html'
39
40     if request.method != 'POST':
41         form = RegisterForm()
42         context['form'] = form
43         return render_to_response(template, context)
44
45     reg_req = RegistrationRequest()
46     form = RegisterForm(instance = reg_req, data = request.POST)
47
48     if form.is_valid():
49         form.save()
50         context['request'] = reg_req
51     else:
52         context['form'] = form
53
54     return render_to_response(template, context)
55
56 def register_confirm(request, key):
57     context = PatchworkRequestContext(request)
58     req = get_object_or_404(RegistrationRequest, key = key)
59     req.create_user()
60     user = auth.authenticate(username = req.username, password = req.password)
61     auth.login(request, user)
62
63     return render_to_response('patchwork/register-confirm.html', context)
64
65 def login(request):
66     context = PatchworkRequestContext(request)
67     template = 'patchwork/login.html'
68     error = None
69
70     if request.method == 'POST':
71         form = LoginForm(request.POST)
72         context['form'] = form
73
74         if not form.is_valid():
75             return render_to_response(template, context)
76
77         data = form.cleaned_data
78         user = auth.authenticate(username = data['username'],
79                 password = data['password'])
80
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)
87
88         context['error'] = 'Invalid username or password'
89
90     else:
91         context['form'] = LoginForm()
92
93     return render_to_response(template, context)
94
95 def logout(request):
96     auth.logout(request)
97     return render_to_response('patchwork/logout.html')
98
99 @login_required
100 def profile(request):
101     context = PatchworkRequestContext(request)
102
103     if request.method == 'POST':
104         form = UserProfileForm(instance = request.user.get_profile(),
105                 data = request.POST)
106         if form.is_valid():
107             form.save()
108     else:
109         form = UserProfileForm(instance = request.user.get_profile())
110
111     context.project = request.user.get_profile().primary_project
112     context['bundles'] = Bundle.objects.filter(owner = request.user)
113     context['profileform'] = form
114
115     people = Person.objects.filter(user = request.user)
116     context['linked_emails'] = people
117     context['linkform'] = UserPersonLinkForm()
118
119     return render_to_response('patchwork/profile.html', context)
120
121 @login_required
122 def link(request):
123     context = PatchworkRequestContext(request)
124
125     form = UserPersonLinkForm(request.POST)
126     if request.method == 'POST':
127         form = UserPersonLinkForm(request.POST)
128         if form.is_valid():
129             conf = UserPersonConfirmation(user = request.user,
130                     email = form.cleaned_data['email'])
131             conf.save()
132             context['confirmation'] = conf
133
134     context['linkform'] = form
135
136     return render_to_response('patchwork/user-link.html', context)
137
138 @login_required
139 def link_confirm(request, key):
140     context = PatchworkRequestContext(request)
141     confirmation = get_object_or_404(UserPersonConfirmation, key = key)
142
143     errors = confirmation.confirm()
144     if errors:
145         context['errors'] = errors
146     else:
147         context['person'] = Person.objects.get(email = confirmation.email)
148
149     confirmation.delete()
150
151     return render_to_response('patchwork/user-link-confirm.html', context)
152
153 @login_required
154 def unlink(request, person_id):
155     context = PatchworkRequestContext(request)
156     person = get_object_or_404(Person, id = person_id)
157
158     if request.method == 'POST':
159         if person.email != request.user.email:
160             person.user = None
161             person.save()
162
163     url = django.core.urlresolvers.reverse('patchwork.views.user.profile')
164     return HttpResponseRedirect(url)
165
166
167 @login_required
168 def todo_lists(request):
169     todo_lists = []
170
171     for project in Project.objects.all():
172         patches = request.user.get_profile().todo_patches(project = project)
173         if not patches.count():
174             continue
175
176         todo_lists.append({'project': project, 'n_patches': patches.count()})
177
178     if len(todo_lists) == 1:
179         return todo_list(request, todo_lists[0]['project'].linkname)
180
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)
185
186 @login_required
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})]
192
193     context = generic_list(request, project,
194             'patchwork.views.user.todo_list',
195             view_args = {'project_id': project.linkname},
196             filter_settings = filter_settings,
197             patches = patches)
198
199     context['action_required_states'] = \
200         State.objects.filter(action_required = True).all()
201     return render_to_response('patchwork/todo-list.html', context)