]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/user.py
759a6e392f1d6da1d8eb3d5d6ca14389d7bc7252
[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, EmailConfirmation, State
26 from patchwork.forms import UserProfileForm, UserPersonLinkForm
27 from patchwork.filters import DelegateFilter
28 from patchwork.views import generic_list
29 from django.template.loader import render_to_string
30 from django.conf import settings
31 from django.core.mail import send_mail
32 import django.core.urlresolvers
33
34 @login_required
35 def profile(request):
36     context = PatchworkRequestContext(request)
37
38     if request.method == 'POST':
39         form = UserProfileForm(instance = request.user.get_profile(),
40                 data = request.POST)
41         if form.is_valid():
42             form.save()
43     else:
44         form = UserProfileForm(instance = request.user.get_profile())
45
46     context.project = request.user.get_profile().primary_project
47     context['bundles'] = Bundle.objects.filter(owner = request.user)
48     context['profileform'] = form
49
50     people = Person.objects.filter(user = request.user)
51     context['linked_emails'] = people
52     context['linkform'] = UserPersonLinkForm()
53
54     return render_to_response('patchwork/profile.html', context)
55
56 @login_required
57 def link(request):
58     context = PatchworkRequestContext(request)
59
60     if request.method == 'POST':
61         form = UserPersonLinkForm(request.POST)
62         if form.is_valid():
63             conf = EmailConfirmation(type = 'userperson',
64                     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, conf):
87     context = PatchworkRequestContext(request)
88
89     try:
90         person = Person.objects.get(email__iexact = conf.email)
91     except Person.DoesNotExist:
92         person = Person(email = conf.email)
93
94     person.link_to_user(conf.user)
95     person.save()
96     conf.deactivate()
97
98     context['person'] = person
99
100     return render_to_response('patchwork/user-link-confirm.html', context)
101
102 @login_required
103 def unlink(request, person_id):
104     person = get_object_or_404(Person, id = person_id)
105
106     if request.method == 'POST':
107         if person.email != request.user.email:
108             person.user = None
109             person.save()
110
111     url = django.core.urlresolvers.reverse('patchwork.views.user.profile')
112     return HttpResponseRedirect(url)
113
114
115 @login_required
116 def todo_lists(request):
117     todo_lists = []
118
119     for project in Project.objects.all():
120         patches = request.user.get_profile().todo_patches(project = project)
121         if not patches.count():
122             continue
123
124         todo_lists.append({'project': project, 'n_patches': patches.count()})
125
126     if len(todo_lists) == 1:
127         return todo_list(request, todo_lists[0]['project'].linkname)
128
129     context = PatchworkRequestContext(request)
130     context['todo_lists'] = todo_lists
131     context.project = request.user.get_profile().primary_project
132     return render_to_response('patchwork/todo-lists.html', context)
133
134 @login_required
135 def todo_list(request, project_id):
136     project = get_object_or_404(Project, linkname = project_id)
137     patches = request.user.get_profile().todo_patches(project = project)
138     filter_settings = [(DelegateFilter,
139             {'delegate': request.user})]
140
141     context = generic_list(request, project,
142             'patchwork.views.user.todo_list',
143             view_args = {'project_id': project.linkname},
144             filter_settings = filter_settings,
145             patches = patches)
146
147     context['action_required_states'] = \
148         State.objects.filter(action_required = True).all()
149     return render_to_response('patchwork/todo-list.html', context)