]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/user.py
0e145494ab023a6c3888b7e3efdb21f828f17650
[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
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
39
40 def register(request):
41     context = PatchworkRequestContext(request)
42     template = 'patchwork/register.html'
43
44     if request.method != 'POST':
45         form = RegisterForm()
46         context['form'] = form
47         return render_to_response(template, context)
48
49     reg_req = RegistrationRequest()
50     form = RegisterForm(instance = reg_req, data = request.POST)
51
52     if form.is_valid():
53         form.save()
54         try:
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']])
60
61         except Exception, ex:
62             context['request'] = None
63             context['error'] = 'An error occurred during registration. ' + \
64                                'Please try again later'
65
66     context['form'] = form
67
68     return render_to_response(template, context)
69
70 def register_confirm(request, key):
71     context = PatchworkRequestContext(request)
72     req = get_object_or_404(RegistrationRequest, key = key)
73     req.create_user()
74     user = auth.authenticate(username = req.username, password = req.password)
75     auth.login(request, user)
76
77     return render_to_response('patchwork/register-confirm.html', context)
78
79 def login(request):
80     context = PatchworkRequestContext(request)
81     template = 'patchwork/login.html'
82     error = None
83
84     if request.method == 'POST':
85         form = LoginForm(request.POST)
86         context['form'] = form
87
88         if not form.is_valid():
89             return render_to_response(template, context)
90
91         data = form.cleaned_data
92         user = auth.authenticate(username = data['username'],
93                 password = data['password'])
94
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)
101
102         context['error'] = 'Invalid username or password'
103
104     else:
105         context['form'] = LoginForm()
106
107     return render_to_response(template, context)
108
109 def logout(request):
110     auth.logout(request)
111     return render_to_response('patchwork/logout.html')
112
113 @login_required
114 def profile(request):
115     context = PatchworkRequestContext(request)
116
117     if request.method == 'POST':
118         form = UserProfileForm(instance = request.user.get_profile(),
119                 data = request.POST)
120         if form.is_valid():
121             form.save()
122     else:
123         form = UserProfileForm(instance = request.user.get_profile())
124
125     context.project = request.user.get_profile().primary_project
126     context['bundles'] = Bundle.objects.filter(owner = request.user)
127     context['profileform'] = form
128
129     people = Person.objects.filter(user = request.user)
130     context['linked_emails'] = people
131     context['linkform'] = UserPersonLinkForm()
132
133     return render_to_response('patchwork/profile.html', context)
134
135 @login_required
136 def link(request):
137     context = PatchworkRequestContext(request)
138
139     form = UserPersonLinkForm(request.POST)
140     if request.method == 'POST':
141         form = UserPersonLinkForm(request.POST)
142         if form.is_valid():
143             conf = UserPersonConfirmation(user = request.user,
144                     email = form.cleaned_data['email'])
145             context['confirmation'] = conf
146
147             try:
148                 send_mail('Patchwork email address confirmation',
149                             render_to_string('patchwork/user-link.mail',
150                                 context),
151                             settings.PATCHWORK_FROM_EMAIL,
152                             [form.cleaned_data['email']])
153                 conf.save()
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
159
160     return render_to_response('patchwork/user-link.html', context)
161
162 @login_required
163 def link_confirm(request, key):
164     context = PatchworkRequestContext(request)
165     confirmation = get_object_or_404(UserPersonConfirmation, key = key)
166
167     errors = confirmation.confirm()
168     if errors:
169         context['errors'] = errors
170     else:
171         context['person'] = Person.objects.get(email = confirmation.email)
172
173     return render_to_response('patchwork/user-link-confirm.html', context)
174
175 @login_required
176 def unlink(request, person_id):
177     context = PatchworkRequestContext(request)
178     person = get_object_or_404(Person, id = person_id)
179
180     if request.method == 'POST':
181         if person.email != request.user.email:
182             person.user = None
183             person.save()
184
185     url = django.core.urlresolvers.reverse('patchwork.views.user.profile')
186     return HttpResponseRedirect(url)
187
188
189 @login_required
190 def todo_lists(request):
191     todo_lists = []
192
193     for project in Project.objects.all():
194         patches = request.user.get_profile().todo_patches(project = project)
195         if not patches.count():
196             continue
197
198         todo_lists.append({'project': project, 'n_patches': patches.count()})
199
200     if len(todo_lists) == 1:
201         return todo_list(request, todo_lists[0]['project'].linkname)
202
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)
207
208 @login_required
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})]
214
215     context = generic_list(request, project,
216             'patchwork.views.user.todo_list',
217             view_args = {'project_id': project.linkname},
218             filter_settings = filter_settings,
219             patches = patches)
220
221     context['action_required_states'] = \
222         State.objects.filter(action_required = True).all()
223     return render_to_response('patchwork/todo-list.html', context)