]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/user.py
3d28f4b09b81efc1c80bfc8116487caa5bb2fe4a
[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.contrib.sites.models import Site
26 from django.http import HttpResponseRedirect
27 from patchwork.models import Project, Bundle, Person, EmailConfirmation, State
28 from patchwork.forms import UserProfileForm, UserPersonLinkForm, \
29          RegistrationForm
30 from patchwork.filters import DelegateFilter
31 from patchwork.views import generic_list
32 from django.template.loader import render_to_string
33 from django.conf import settings
34 from django.core.mail import send_mail
35 import django.core.urlresolvers
36
37 def register(request):
38     context = PatchworkRequestContext(request)
39     if request.method == 'POST':
40         form = RegistrationForm(request.POST)
41         if form.is_valid():
42             data = form.cleaned_data
43             # create inactive user
44             user = auth.models.User.objects.create_user(data['username'],
45                                                         data['email'],
46                                                         data['password'])
47             user.is_active = False;
48             user.first_name = data.get('first_name', '')
49             user.last_name = data.get('last_name', '')
50             user.save()
51
52             # create confirmation
53             conf = EmailConfirmation(type = 'registration', user = user,
54                                      email = user.email)
55             conf.save()
56
57             # send email
58             mail_ctx = {'site': Site.objects.get_current(),
59                         'confirmation': conf}
60
61             subject = render_to_string('patchwork/activation_email_subject.txt',
62                                 mail_ctx).replace('\n', ' ').strip()
63             
64             message = render_to_string('patchwork/activation_email.txt',
65                                     mail_ctx)
66             
67             send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
68                             [conf.email])
69
70             # setting 'confirmation' in the template indicates success
71             context['confirmation'] = conf
72
73     else:
74         form = RegistrationForm()
75     
76     return render_to_response('patchwork/registration_form.html',
77                               { 'form': form },
78                               context_instance=context)
79
80 def register_confirm(request, conf):
81     conf.user.is_active = True
82     conf.user.save()
83     conf.deactivate()
84     return render_to_response('patchwork/registration-confirm.html')
85
86 @login_required
87 def profile(request):
88     context = PatchworkRequestContext(request)
89
90     if request.method == 'POST':
91         form = UserProfileForm(instance = request.user.get_profile(),
92                 data = request.POST)
93         if form.is_valid():
94             form.save()
95     else:
96         form = UserProfileForm(instance = request.user.get_profile())
97
98     context.project = request.user.get_profile().primary_project
99     context['bundles'] = Bundle.objects.filter(owner = request.user)
100     context['profileform'] = form
101
102     people = Person.objects.filter(user = request.user)
103     context['linked_emails'] = people
104     context['linkform'] = UserPersonLinkForm()
105
106     return render_to_response('patchwork/profile.html', context)
107
108 @login_required
109 def link(request):
110     context = PatchworkRequestContext(request)
111
112     if request.method == 'POST':
113         form = UserPersonLinkForm(request.POST)
114         if form.is_valid():
115             conf = EmailConfirmation(type = 'userperson',
116                     user = request.user,
117                     email = form.cleaned_data['email'])
118             conf.save()
119             context['confirmation'] = conf
120
121             try:
122                 send_mail('Patchwork email address confirmation',
123                             render_to_string('patchwork/user-link.mail',
124                                 context),
125                             settings.DEFAULT_FROM_EMAIL,
126                             [form.cleaned_data['email']])
127             except Exception:
128                 context['confirmation'] = None
129                 context['error'] = 'An error occurred during confirmation. ' + \
130                                    'Please try again later'
131     else:
132         form = UserPersonLinkForm()
133     context['linkform'] = form
134
135     return render_to_response('patchwork/user-link.html', context)
136
137 @login_required
138 def link_confirm(request, conf):
139     context = PatchworkRequestContext(request)
140
141     try:
142         person = Person.objects.get(email__iexact = conf.email)
143     except Person.DoesNotExist:
144         person = Person(email = conf.email)
145
146     person.link_to_user(conf.user)
147     person.save()
148     conf.deactivate()
149
150     context['person'] = person
151
152     return render_to_response('patchwork/user-link-confirm.html', context)
153
154 @login_required
155 def unlink(request, person_id):
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)