]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/user.py
Merge branch 'notifications'
[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, \
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     return render_to_response('patchwork/registration-confirm.html')
86
87 @login_required
88 def profile(request):
89     context = PatchworkRequestContext(request)
90
91     if request.method == 'POST':
92         form = UserProfileForm(instance = request.user.get_profile(),
93                 data = request.POST)
94         if form.is_valid():
95             form.save()
96     else:
97         form = UserProfileForm(instance = request.user.get_profile())
98
99     context.project = request.user.get_profile().primary_project
100     context['bundles'] = Bundle.objects.filter(owner = request.user)
101     context['profileform'] = form
102
103     optout_query = '%s.%s IN (SELECT %s FROM %s)' % (
104                         Person._meta.db_table,
105                         Person._meta.get_field('email').column,
106                         EmailOptout._meta.get_field('email').column,
107                         EmailOptout._meta.db_table)
108     people = Person.objects.filter(user = request.user) \
109              .extra(select = {'is_optout': optout_query})
110     context['linked_emails'] = people
111     context['linkform'] = UserPersonLinkForm()
112
113     return render_to_response('patchwork/profile.html', context)
114
115 @login_required
116 def link(request):
117     context = PatchworkRequestContext(request)
118
119     if request.method == 'POST':
120         form = UserPersonLinkForm(request.POST)
121         if form.is_valid():
122             conf = EmailConfirmation(type = 'userperson',
123                     user = request.user,
124                     email = form.cleaned_data['email'])
125             conf.save()
126             context['confirmation'] = conf
127
128             try:
129                 send_mail('Patchwork email address confirmation',
130                             render_to_string('patchwork/user-link.mail',
131                                 context),
132                             settings.DEFAULT_FROM_EMAIL,
133                             [form.cleaned_data['email']])
134             except Exception:
135                 context['confirmation'] = None
136                 context['error'] = 'An error occurred during confirmation. ' + \
137                                    'Please try again later'
138     else:
139         form = UserPersonLinkForm()
140     context['linkform'] = form
141
142     return render_to_response('patchwork/user-link.html', context)
143
144 @login_required
145 def link_confirm(request, conf):
146     context = PatchworkRequestContext(request)
147
148     try:
149         person = Person.objects.get(email__iexact = conf.email)
150     except Person.DoesNotExist:
151         person = Person(email = conf.email)
152
153     person.link_to_user(conf.user)
154     person.save()
155     conf.deactivate()
156
157     context['person'] = person
158
159     return render_to_response('patchwork/user-link-confirm.html', context)
160
161 @login_required
162 def unlink(request, person_id):
163     person = get_object_or_404(Person, id = person_id)
164
165     if request.method == 'POST':
166         if person.email != request.user.email:
167             person.user = None
168             person.save()
169
170     url = django.core.urlresolvers.reverse('patchwork.views.user.profile')
171     return HttpResponseRedirect(url)
172
173
174 @login_required
175 def todo_lists(request):
176     todo_lists = []
177
178     for project in Project.objects.all():
179         patches = request.user.get_profile().todo_patches(project = project)
180         if not patches.count():
181             continue
182
183         todo_lists.append({'project': project, 'n_patches': patches.count()})
184
185     if len(todo_lists) == 1:
186         return todo_list(request, todo_lists[0]['project'].linkname)
187
188     context = PatchworkRequestContext(request)
189     context['todo_lists'] = todo_lists
190     context.project = request.user.get_profile().primary_project
191     return render_to_response('patchwork/todo-lists.html', context)
192
193 @login_required
194 def todo_list(request, project_id):
195     project = get_object_or_404(Project, linkname = project_id)
196     patches = request.user.get_profile().todo_patches(project = project)
197     filter_settings = [(DelegateFilter,
198             {'delegate': request.user})]
199
200     context = generic_list(request, project,
201             'patchwork.views.user.todo_list',
202             view_args = {'project_id': project.linkname},
203             filter_settings = filter_settings,
204             patches = patches)
205
206     context['action_required_states'] = \
207         State.objects.filter(action_required = True).all()
208     return render_to_response('patchwork/todo-list.html', context)