]> git.ozlabs.org Git - patchwork/blob - patchwork/views/user.py
Move to a more recent django project structure
[patchwork] / 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, \
28          State, EmailOptout
29 from patchwork.forms import UserProfileForm, UserPersonLinkForm, \
30          RegistrationForm
31 from patchwork.filters import DelegateFilter
32 from patchwork.views import generic_list
33 from django.template.loader import render_to_string
34 from django.conf import settings
35 from django.core.mail import send_mail
36 import django.core.urlresolvers
37
38 def register(request):
39     context = PatchworkRequestContext(request)
40     if request.method == 'POST':
41         form = RegistrationForm(request.POST)
42         if form.is_valid():
43             data = form.cleaned_data
44             # create inactive user
45             user = auth.models.User.objects.create_user(data['username'],
46                                                         data['email'],
47                                                         data['password'])
48             user.is_active = False;
49             user.first_name = data.get('first_name', '')
50             user.last_name = data.get('last_name', '')
51             user.save()
52
53             # create confirmation
54             conf = EmailConfirmation(type = 'registration', user = user,
55                                      email = user.email)
56             conf.save()
57
58             # send email
59             mail_ctx = {'site': Site.objects.get_current(),
60                         'confirmation': conf}
61
62             subject = render_to_string('patchwork/activation_email_subject.txt',
63                                 mail_ctx).replace('\n', ' ').strip()
64             
65             message = render_to_string('patchwork/activation_email.txt',
66                                     mail_ctx)
67             
68             send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
69                             [conf.email])
70
71             # setting 'confirmation' in the template indicates success
72             context['confirmation'] = conf
73
74     else:
75         form = RegistrationForm()
76     
77     return render_to_response('patchwork/registration_form.html',
78                               { 'form': form },
79                               context_instance=context)
80
81 def register_confirm(request, conf):
82     conf.user.is_active = True
83     conf.user.save()
84     conf.deactivate()
85     try:
86         person = Person.objects.get(email__iexact = conf.user.email)
87     except Person.DoesNotExist:
88         person = Person(email = conf.user.email,
89                 name = conf.user.profile.name())
90     person.user = conf.user
91     person.save()
92
93     return render_to_response('patchwork/registration-confirm.html')
94
95 @login_required
96 def profile(request):
97     context = PatchworkRequestContext(request)
98
99     if request.method == 'POST':
100         form = UserProfileForm(instance = request.user.profile,
101                 data = request.POST)
102         if form.is_valid():
103             form.save()
104     else:
105         form = UserProfileForm(instance = request.user.profile)
106
107     context.project = request.user.profile.primary_project
108     context['bundles'] = Bundle.objects.filter(owner = request.user)
109     context['profileform'] = form
110
111     optout_query = '%s.%s IN (SELECT %s FROM %s)' % (
112                         Person._meta.db_table,
113                         Person._meta.get_field('email').column,
114                         EmailOptout._meta.get_field('email').column,
115                         EmailOptout._meta.db_table)
116     people = Person.objects.filter(user = request.user) \
117              .extra(select = {'is_optout': optout_query})
118     context['linked_emails'] = people
119     context['linkform'] = UserPersonLinkForm()
120
121     return render_to_response('patchwork/profile.html', context)
122
123 @login_required
124 def link(request):
125     context = PatchworkRequestContext(request)
126
127     if request.method == 'POST':
128         form = UserPersonLinkForm(request.POST)
129         if form.is_valid():
130             conf = EmailConfirmation(type = 'userperson',
131                     user = request.user,
132                     email = form.cleaned_data['email'])
133             conf.save()
134             context['confirmation'] = conf
135
136             try:
137                 send_mail('Patchwork email address confirmation',
138                             render_to_string('patchwork/user-link.mail',
139                                 context),
140                             settings.DEFAULT_FROM_EMAIL,
141                             [form.cleaned_data['email']])
142             except Exception:
143                 context['confirmation'] = None
144                 context['error'] = 'An error occurred during confirmation. ' + \
145                                    'Please try again later'
146     else:
147         form = UserPersonLinkForm()
148     context['linkform'] = form
149
150     return render_to_response('patchwork/user-link.html', context)
151
152 @login_required
153 def link_confirm(request, conf):
154     context = PatchworkRequestContext(request)
155
156     try:
157         person = Person.objects.get(email__iexact = conf.email)
158     except Person.DoesNotExist:
159         person = Person(email = conf.email)
160
161     person.link_to_user(conf.user)
162     person.save()
163     conf.deactivate()
164
165     context['person'] = person
166
167     return render_to_response('patchwork/user-link-confirm.html', context)
168
169 @login_required
170 def unlink(request, person_id):
171     person = get_object_or_404(Person, id = person_id)
172
173     if request.method == 'POST':
174         if person.email != request.user.email:
175             person.user = None
176             person.save()
177
178     url = django.core.urlresolvers.reverse('patchwork.views.user.profile')
179     return HttpResponseRedirect(url)
180
181
182 @login_required
183 def todo_lists(request):
184     todo_lists = []
185
186     for project in Project.objects.all():
187         patches = request.user.profile.todo_patches(project = project)
188         if not patches.count():
189             continue
190
191         todo_lists.append({'project': project, 'n_patches': patches.count()})
192
193     if len(todo_lists) == 1:
194         return todo_list(request, todo_lists[0]['project'].linkname)
195
196     context = PatchworkRequestContext(request)
197     context['todo_lists'] = todo_lists
198     context.project = request.user.profile.primary_project
199     return render_to_response('patchwork/todo-lists.html', context)
200
201 @login_required
202 def todo_list(request, project_id):
203     project = get_object_or_404(Project, linkname = project_id)
204     patches = request.user.profile.todo_patches(project = project)
205     filter_settings = [(DelegateFilter,
206             {'delegate': request.user})]
207
208     context = generic_list(request, project,
209             'patchwork.views.user.todo_list',
210             view_args = {'project_id': project.linkname},
211             filter_settings = filter_settings,
212             patches = patches)
213
214     context['action_required_states'] = \
215         State.objects.filter(action_required = True).all()
216     return render_to_response('patchwork/todo-list.html', context)