X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Fviews%2F__init__.py;h=cb35c1fda7f145e3c6854e7ad6916d265117b527;hb=67181f5c929018d5304732969f0811795c13ea37;hp=b5c19c2e26705ef8c031182194f9ab1f9cc7d39a;hpb=3c6909740c2b00c31a9b39b5ef8e0ce7f588f685;p=patchwork diff --git a/apps/patchwork/views/__init__.py b/apps/patchwork/views/__init__.py index b5c19c2..cb35c1f 100644 --- a/apps/patchwork/views/__init__.py +++ b/apps/patchwork/views/__init__.py @@ -19,50 +19,80 @@ from base import * -from patchwork.utils import Order, get_patch_ids, set_patches +from patchwork.utils import Order, get_patch_ids, bundle_actions, set_bundle from patchwork.paginator import Paginator from patchwork.forms import MultiplePatchForm +from patchwork.models import Comment +import re +import datetime + +try: + from email.mime.nonmultipart import MIMENonMultipart + from email.encoders import encode_7or8bit + from email.parser import HeaderParser + from email.header import Header + import email.utils +except ImportError: + # Python 2.4 compatibility + from email.MIMENonMultipart import MIMENonMultipart + from email.Encoders import encode_7or8bit + from email.Parser import HeaderParser + from email.Header import Header + import email.Utils + email.utils = email.Utils def generic_list(request, project, view, - view_args = {}, filter_settings = [], patches = None): + view_args = {}, filter_settings = [], patches = None, + editable_order = False): context = PatchworkRequestContext(request, list_view = view, list_view_params = view_args) context.project = project - order = Order(request.REQUEST.get('order')) - - form = MultiplePatchForm(project) - - if request.method == 'POST' and \ - request.POST.get('form') == 'patchlistform': - action = request.POST.get('action', None) - if action: - action = action.lower() + order = Order(request.REQUEST.get('order'), editable = editable_order) + + # Explicitly set data to None because request.POST will be an empty dict + # when the form is not submitted, but passing a non-None data argument to + # a forms.Form will make it bound and we don't want that to happen unless + # there's been a form submission. + data = None + if request.method == 'POST': + data = request.POST + user = request.user + properties_form = None + if project.is_editable(user): + + # we only pass the post data to the MultiplePatchForm if that was + # the actual form submitted + data_tmp = None + if data and data.get('form', '') == 'patchlistform': + data_tmp = data + + properties_form = MultiplePatchForm(project, data = data_tmp) + + if request.method == 'POST' and data.get('form') == 'patchlistform': + action = data.get('action', '').lower() # special case: the user may have hit enter in the 'create bundle' # text field, so if non-empty, assume the create action: - if request.POST.get('bundle_name', False): + if data.get('bundle_name', False): action = 'create' - ps = [] - for patch_id in get_patch_ids(request.POST): - try: - patch = Patch.objects.get(id = patch_id) - except Patch.DoesNotExist: - pass - ps.append(patch) + ps = Patch.objects.filter(id__in = get_patch_ids(data)) + + if action in bundle_actions: + errors = set_bundle(user, project, action, data, ps, context) + + elif properties_form and action == properties_form.action: + errors = process_multiplepatch_form(properties_form, user, + action, ps, context) + else: + errors = [] - (errors, form) = set_patches(request.user, project, action, \ - request.POST, ps, context) if errors: context['errors'] = errors - if not (request.user.is_authenticated() and \ - project in request.user.get_profile().maintainer_projects.all()): - form = None - for (filterclass, setting) in filter_settings: if isinstance(setting, dict): context.filters.set_status(filterclass, **setting) @@ -71,20 +101,113 @@ def generic_list(request, project, view, else: context.filters.set_status(filterclass, setting) - if not patches: + if patches is None: patches = Patch.objects.filter(project=project) patches = context.filters.apply(patches) - patches = patches.order_by(order.query()) + if not editable_order: + patches = patches.order_by(order.query()) paginator = Paginator(request, patches) context.update({ 'page': paginator.current_page, - 'patchform': form, + 'patchform': properties_form, 'project': project, 'order': order, }) return context + +def process_multiplepatch_form(form, user, action, patches, context): + errors = [] + if not form.is_valid() or action != form.action: + return ['The submitted form data was invalid'] + + if len(patches) == 0: + context.add_message("No patches selected; nothing updated") + return errors + + changed_patches = 0 + for patch in patches: + if not patch.is_editable(user): + errors.append("You don't have permissions to edit patch '%s'" + % patch.name) + continue + + changed_patches += 1 + form.save(patch) + + if changed_patches == 1: + context.add_message("1 patch updated") + elif changed_patches > 1: + context.add_message("%d patches updated" % changed_patches) + else: + context.add_message("No patches updated") + + return errors + +class PatchMbox(MIMENonMultipart): + patch_charset = 'utf-8' + def __init__(self, _text): + MIMENonMultipart.__init__(self, 'text', 'plain', + **{'charset': self.patch_charset}) + self.set_payload(_text.encode(self.patch_charset)) + encode_7or8bit(self) + +def patch_to_mbox(patch): + postscript_re = re.compile('\n-{2,3} ?\n') + + comment = None + try: + comment = Comment.objects.get(patch = patch, msgid = patch.msgid) + except Exception: + pass + + body = '' + if comment: + body = comment.content.strip() + "\n" + + parts = postscript_re.split(body, 1) + if len(parts) == 2: + (body, postscript) = parts + body = body.strip() + "\n" + postscript = postscript.strip() + "\n" + else: + postscript = '' + + for comment in Comment.objects.filter(patch = patch) \ + .exclude(msgid = patch.msgid): + body += comment.patch_responses() + + if body: + body += '\n' + + if postscript: + body += '---\n' + postscript.strip() + '\n' + + if patch.content: + body += '\n' + patch.content + + utc_timestamp = (patch.date - + datetime.datetime.utcfromtimestamp(0)).total_seconds() + + mail = PatchMbox(body) + mail['Subject'] = patch.name + mail['Date'] = email.utils.formatdate(utc_timestamp) + mail['From'] = email.utils.formataddr(( + str(Header(patch.submitter.name, mail.patch_charset)), + patch.submitter.email)) + mail['X-Patchwork-Id'] = str(patch.id) + mail['Message-Id'] = patch.msgid + mail.set_unixfrom('From patchwork ' + patch.date.ctime()) + + + copied_headers = ['To', 'Cc'] + orig_headers = HeaderParser().parsestr(str(patch.headers)) + for header in copied_headers: + if header in orig_headers: + mail[header] = orig_headers[header] + + return mail