]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/user.py
1ae3c2dd3843cc37c33f32dc31e9b9262dc4b3df
[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.http import HttpResponseRedirect
25 from patchwork.models import Project, Bundle, Person, UserPersonConfirmation, \
26          State
27 from patchwork.forms import UserProfileForm, UserPersonLinkForm
28 from patchwork.filters import DelegateFilter
29 from patchwork.views import generic_list
30 from django.template.loader import render_to_string
31 from django.conf import settings
32 from django.core.mail import send_mail
33 import django.core.urlresolvers
34
35 @login_required
36 def profile(request):
37     context = PatchworkRequestContext(request)
38
39     if request.method == 'POST':
40         form = UserProfileForm(instance = request.user.get_profile(),
41                 data = request.POST)
42         if form.is_valid():
43             form.save()
44     else:
45         form = UserProfileForm(instance = request.user.get_profile())
46
47     context.project = request.user.get_profile().primary_project
48     context['bundles'] = Bundle.objects.filter(owner = request.user)
49     context['profileform'] = form
50
51     people = Person.objects.filter(user = request.user)
52     context['linked_emails'] = people
53     context['linkform'] = UserPersonLinkForm()
54
55     return render_to_response('patchwork/profile.html', context)
56
57 @login_required
58 def link(request):
59     context = PatchworkRequestContext(request)
60
61     if request.method == 'POST':
62         form = UserPersonLinkForm(request.POST)
63         if form.is_valid():
64             conf = UserPersonConfirmation(user = request.user,
65                     email = form.cleaned_data['email'])
66             conf.save()
67             context['confirmation'] = conf
68
69             try:
70                 send_mail('Patchwork email address confirmation',
71                             render_to_string('patchwork/user-link.mail',
72                                 context),
73                             settings.DEFAULT_FROM_EMAIL,
74                             [form.cleaned_data['email']])
75             except Exception:
76                 context['confirmation'] = None
77                 context['error'] = 'An error occurred during confirmation. ' + \
78                                    'Please try again later'
79     else:
80         form = UserPersonLinkForm()
81     context['linkform'] = form
82
83     return render_to_response('patchwork/user-link.html', context)
84
85 @login_required
86 def link_confirm(request, key):
87     context = PatchworkRequestContext(request)
88     confirmation = get_object_or_404(UserPersonConfirmation, key = key)
89
90     errors = confirmation.confirm()
91     if errors:
92         context['errors'] = errors
93     else:
94         context['person'] = Person.objects.get(email = confirmation.email)
95
96     return render_to_response('patchwork/user-link-confirm.html', context)
97
98 @login_required
99 def unlink(request, person_id):
100     person = get_object_or_404(Person, id = person_id)
101
102     if request.method == 'POST':
103         if person.email != request.user.email:
104             person.user = None
105             person.save()
106
107     url = django.core.urlresolvers.reverse('patchwork.views.user.profile')
108     return HttpResponseRedirect(url)
109
110
111 @login_required
112 def todo_lists(request):
113     todo_lists = []
114
115     for project in Project.objects.all():
116         patches = request.user.get_profile().todo_patches(project = project)
117         if not patches.count():
118             continue
119
120         todo_lists.append({'project': project, 'n_patches': patches.count()})
121
122     if len(todo_lists) == 1:
123         return todo_list(request, todo_lists[0]['project'].linkname)
124
125     context = PatchworkRequestContext(request)
126     context['todo_lists'] = todo_lists
127     context.project = request.user.get_profile().primary_project
128     return render_to_response('patchwork/todo-lists.html', context)
129
130 @login_required
131 def todo_list(request, project_id):
132     project = get_object_or_404(Project, linkname = project_id)
133     patches = request.user.get_profile().todo_patches(project = project)
134     filter_settings = [(DelegateFilter,
135             {'delegate': request.user})]
136
137     context = generic_list(request, project,
138             'patchwork.views.user.todo_list',
139             view_args = {'project_id': project.linkname},
140             filter_settings = filter_settings,
141             patches = patches)
142
143     context['action_required_states'] = \
144         State.objects.filter(action_required = True).all()
145     return render_to_response('patchwork/todo-list.html', context)