]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/views/user.py
Use DEFAULT_FROM_EMAIL rather than PATCHWORK_FROM_EMAIL
[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     form = UserPersonLinkForm(request.POST)
67     if request.method == 'POST':
68         form = UserPersonLinkForm(request.POST)
69         if form.is_valid():
70             conf = UserPersonConfirmation(user = request.user,
71                     email = form.cleaned_data['email'])
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                 conf.save()
81             except Exception, ex:
82                 context['confirmation'] = None
83                 context['error'] = 'An error occurred during confirmation. ' + \
84                                    'Please try again later'
85     context['linkform'] = form
86
87     return render_to_response('patchwork/user-link.html', context)
88
89 @login_required
90 def link_confirm(request, key):
91     context = PatchworkRequestContext(request)
92     confirmation = get_object_or_404(UserPersonConfirmation, key = key)
93
94     errors = confirmation.confirm()
95     if errors:
96         context['errors'] = errors
97     else:
98         context['person'] = Person.objects.get(email = confirmation.email)
99
100     return render_to_response('patchwork/user-link-confirm.html', context)
101
102 @login_required
103 def unlink(request, person_id):
104     context = PatchworkRequestContext(request)
105     person = get_object_or_404(Person, id = person_id)
106
107     if request.method == 'POST':
108         if person.email != request.user.email:
109             person.user = None
110             person.save()
111
112     url = django.core.urlresolvers.reverse('patchwork.views.user.profile')
113     return HttpResponseRedirect(url)
114
115
116 @login_required
117 def todo_lists(request):
118     todo_lists = []
119
120     for project in Project.objects.all():
121         patches = request.user.get_profile().todo_patches(project = project)
122         if not patches.count():
123             continue
124
125         todo_lists.append({'project': project, 'n_patches': patches.count()})
126
127     if len(todo_lists) == 1:
128         return todo_list(request, todo_lists[0]['project'].linkname)
129
130     context = PatchworkRequestContext(request)
131     context['todo_lists'] = todo_lists
132     context.project = request.user.get_profile().primary_project
133     return render_to_response('patchwork/todo-lists.html', context)
134
135 @login_required
136 def todo_list(request, project_id):
137     project = get_object_or_404(Project, linkname = project_id)
138     patches = request.user.get_profile().todo_patches(project = project)
139     filter_settings = [(DelegateFilter,
140             {'delegate': request.user})]
141
142     context = generic_list(request, project,
143             'patchwork.views.user.todo_list',
144             view_args = {'project_id': project.linkname},
145             filter_settings = filter_settings,
146             patches = patches)
147
148     context['action_required_states'] = \
149         State.objects.filter(action_required = True).all()
150     return render_to_response('patchwork/todo-list.html', context)