]> git.ozlabs.org Git - patchwork/blob - patchwork/views/__init__.py
b64f6041eca006bb800f190c0b2183f5e9b3c3ac
[patchwork] / patchwork / views / __init__.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 base import *
22 from patchwork.utils import Order, get_patch_ids, bundle_actions, set_bundle
23 from patchwork.paginator import Paginator
24 from patchwork.forms import MultiplePatchForm
25 from patchwork.models import Comment
26 import re
27 import datetime
28
29 try:
30     from email.mime.nonmultipart import MIMENonMultipart
31     from email.encoders import encode_7or8bit
32     from email.parser import HeaderParser
33     from email.header import Header
34     import email.utils
35 except ImportError:
36     # Python 2.4 compatibility
37     from email.MIMENonMultipart import MIMENonMultipart
38     from email.Encoders import encode_7or8bit
39     from email.Parser import HeaderParser
40     from email.Header import Header
41     import email.Utils
42     email.utils = email.Utils
43
44 def generic_list(request, project, view,
45         view_args = {}, filter_settings = [], patches = None,
46         editable_order = False):
47
48     context = PatchworkRequestContext(request,
49             list_view = view,
50             list_view_params = view_args)
51
52     context.project = project
53     data = {}
54     if request.method == 'GET':
55         data = request.GET
56     elif request.method == 'POST':
57         data = request.POST
58     order = Order(data.get('order'), editable=editable_order)
59
60     # Explicitly set data to None because request.POST will be an empty dict
61     # when the form is not submitted, but passing a non-None data argument to
62     # a forms.Form will make it bound and we don't want that to happen unless
63     # there's been a form submission.
64     if request.method != 'POST':
65         data = None
66     user = request.user
67     properties_form = None
68     if project.is_editable(user):
69
70         # we only pass the post data to the MultiplePatchForm if that was
71         # the actual form submitted
72         data_tmp = None
73         if data and data.get('form', '') == 'patchlistform':
74             data_tmp = data
75
76         properties_form = MultiplePatchForm(project, data = data_tmp)
77
78     if request.method == 'POST' and data.get('form') == 'patchlistform':
79         action = data.get('action', '').lower()
80
81         # special case: the user may have hit enter in the 'create bundle'
82         # text field, so if non-empty, assume the create action:
83         if data.get('bundle_name', False):
84             action = 'create'
85
86         ps = Patch.objects.filter(id__in = get_patch_ids(data))
87
88         if action in bundle_actions:
89             errors = set_bundle(user, project, action, data, ps, context)
90
91         elif properties_form and action == properties_form.action:
92             errors = process_multiplepatch_form(properties_form, user,
93                                                 action, ps, context)
94         else:
95             errors = []
96
97         if errors:
98             context['errors'] = errors
99
100     for (filterclass, setting) in filter_settings:
101         if isinstance(setting, dict):
102             context.filters.set_status(filterclass, **setting)
103         elif isinstance(setting, list):
104             context.filters.set_status(filterclass, *setting)
105         else:
106             context.filters.set_status(filterclass, setting)
107
108     if patches is None:
109         patches = Patch.objects.filter(project=project)
110
111     # annotate with tag counts
112     patches = patches.with_tag_counts(project)
113
114     patches = context.filters.apply(patches)
115     if not editable_order:
116         patches = order.apply(patches)
117
118     # we don't need the content or headers for a list; they're text fields
119     # that can potentially contain a lot of data
120     patches = patches.defer('content', 'headers')
121
122     # but we will need to follow the state and submitter relations for
123     # rendering the list template
124     patches = patches.select_related('state', 'submitter', 'delegate')
125
126     paginator = Paginator(request, patches)
127
128     context.update({
129             'page':             paginator.current_page,
130             'patchform':        properties_form,
131             'project':          project,
132             'order':            order,
133             })
134
135     return context
136
137
138 def process_multiplepatch_form(form, user, action, patches, context):
139     errors = []
140     if not form.is_valid() or action != form.action:
141         return ['The submitted form data was invalid']
142
143     if len(patches) == 0:
144         context.add_message("No patches selected; nothing updated")
145         return errors
146
147     changed_patches = 0
148     for patch in patches:
149         if not patch.is_editable(user):
150             errors.append("You don't have permissions to edit patch '%s'"
151                             % patch.name)
152             continue
153
154         changed_patches += 1
155         form.save(patch)
156
157     if changed_patches == 1:
158         context.add_message("1 patch updated")
159     elif changed_patches > 1:
160         context.add_message("%d patches updated" % changed_patches)
161     else:
162         context.add_message("No patches updated")
163
164     return errors
165
166 class PatchMbox(MIMENonMultipart):
167     patch_charset = 'utf-8'
168     def __init__(self, _text):
169         MIMENonMultipart.__init__(self, 'text', 'plain',
170                         **{'charset': self.patch_charset})
171         self.set_payload(_text.encode(self.patch_charset))
172         encode_7or8bit(self)
173
174 def patch_to_mbox(patch):
175     postscript_re = re.compile('\n-{2,3} ?\n')
176
177     comment = None
178     try:
179         comment = Comment.objects.get(patch = patch, msgid = patch.msgid)
180     except Exception:
181         pass
182
183     body = ''
184     if comment:
185         body = comment.content.strip() + "\n"
186
187     parts = postscript_re.split(body, 1)
188     if len(parts) == 2:
189         (body, postscript) = parts
190         body = body.strip() + "\n"
191         postscript = postscript.rstrip()
192     else:
193         postscript = ''
194
195     for comment in Comment.objects.filter(patch = patch) \
196             .exclude(msgid = patch.msgid):
197         body += comment.patch_responses()
198
199     if postscript:
200         body += '---\n' + postscript + '\n'
201
202     if patch.content:
203         body += '\n' + patch.content
204
205     delta = patch.date - datetime.datetime.utcfromtimestamp(0)
206     utc_timestamp = delta.seconds + delta.days*24*3600
207
208     mail = PatchMbox(body)
209     mail['Subject'] = patch.name
210     mail['From'] = email.utils.formataddr((
211                     str(Header(patch.submitter.name, mail.patch_charset)),
212                     patch.submitter.email))
213     mail['X-Patchwork-Id'] = str(patch.id)
214     mail['Message-Id'] = patch.msgid
215     mail.set_unixfrom('From patchwork ' + patch.date.ctime())
216
217
218     copied_headers = ['To', 'Cc', 'Date']
219     orig_headers = HeaderParser().parsestr(str(patch.headers))
220     for header in copied_headers:
221         if header in orig_headers:
222             mail[header] = orig_headers[header]
223
224     if 'Date' not in mail:
225         mail['Date'] = email.utils.formatdate(utc_timestamp)
226
227     return mail