1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
4 # This file is part of the Patchwork package.
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.
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.
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
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
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
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
42 email.utils = email.Utils
44 def generic_list(request, project, view,
45 view_args = {}, filter_settings = [], patches = None,
46 editable_order = False):
48 context = PatchworkRequestContext(request,
50 list_view_params = view_args)
52 context.project = project
53 order = Order(request.REQUEST.get('order'), editable = editable_order)
55 # Explicitly set data to None because request.POST will be an empty dict
56 # when the form is not submitted, but passing a non-None data argument to
57 # a forms.Form will make it bound and we don't want that to happen unless
58 # there's been a form submission.
60 if request.method == 'POST':
63 properties_form = None
64 if project.is_editable(user):
66 # we only pass the post data to the MultiplePatchForm if that was
67 # the actual form submitted
69 if data and data.get('form', '') == 'patchlistform':
72 properties_form = MultiplePatchForm(project, data = data_tmp)
74 if request.method == 'POST' and data.get('form') == 'patchlistform':
75 action = data.get('action', '').lower()
77 # special case: the user may have hit enter in the 'create bundle'
78 # text field, so if non-empty, assume the create action:
79 if data.get('bundle_name', False):
82 ps = Patch.objects.filter(id__in = get_patch_ids(data))
84 if action in bundle_actions:
85 errors = set_bundle(user, project, action, data, ps, context)
87 elif properties_form and action == properties_form.action:
88 errors = process_multiplepatch_form(properties_form, user,
94 context['errors'] = errors
96 for (filterclass, setting) in filter_settings:
97 if isinstance(setting, dict):
98 context.filters.set_status(filterclass, **setting)
99 elif isinstance(setting, list):
100 context.filters.set_status(filterclass, *setting)
102 context.filters.set_status(filterclass, setting)
105 patches = Patch.objects.filter(project=project)
107 patches = context.filters.apply(patches)
108 if not editable_order:
109 patches = order.apply(patches)
111 paginator = Paginator(request, patches)
114 'page': paginator.current_page,
115 'patchform': properties_form,
123 def process_multiplepatch_form(form, user, action, patches, context):
125 if not form.is_valid() or action != form.action:
126 return ['The submitted form data was invalid']
128 if len(patches) == 0:
129 context.add_message("No patches selected; nothing updated")
133 for patch in patches:
134 if not patch.is_editable(user):
135 errors.append("You don't have permissions to edit patch '%s'"
142 if changed_patches == 1:
143 context.add_message("1 patch updated")
144 elif changed_patches > 1:
145 context.add_message("%d patches updated" % changed_patches)
147 context.add_message("No patches updated")
151 class PatchMbox(MIMENonMultipart):
152 patch_charset = 'utf-8'
153 def __init__(self, _text):
154 MIMENonMultipart.__init__(self, 'text', 'plain',
155 **{'charset': self.patch_charset})
156 self.set_payload(_text.encode(self.patch_charset))
159 def patch_to_mbox(patch):
160 postscript_re = re.compile('\n-{2,3} ?\n')
164 comment = Comment.objects.get(patch = patch, msgid = patch.msgid)
170 body = comment.content.strip() + "\n"
172 parts = postscript_re.split(body, 1)
174 (body, postscript) = parts
175 body = body.strip() + "\n"
176 postscript = postscript.strip() + "\n"
180 for comment in Comment.objects.filter(patch = patch) \
181 .exclude(msgid = patch.msgid):
182 body += comment.patch_responses()
188 body += '---\n' + postscript.strip() + '\n'
191 body += '\n' + patch.content
193 delta = patch.date - datetime.datetime.utcfromtimestamp(0)
194 utc_timestamp = delta.seconds + delta.days*24*3600
196 mail = PatchMbox(body)
197 mail['Subject'] = patch.name
198 mail['From'] = email.utils.formataddr((
199 str(Header(patch.submitter.name, mail.patch_charset)),
200 patch.submitter.email))
201 mail['X-Patchwork-Id'] = str(patch.id)
202 mail['Message-Id'] = patch.msgid
203 mail.set_unixfrom('From patchwork ' + patch.date.ctime())
206 copied_headers = ['To', 'Cc', 'Date']
207 orig_headers = HeaderParser().parsestr(str(patch.headers))
208 for header in copied_headers:
209 if header in orig_headers:
210 mail[header] = orig_headers[header]
212 if 'Date' not in mail:
213 mail['Date'] = email.utils.formatdate(utc_timestamp)