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