From: Stephen Finucane Date: Mon, 9 Jan 2017 12:06:14 +0000 (+0000) Subject: views: Make 'patch_to_mbox' return a string X-Git-Tag: v2.0.0-rc1~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=051f0d8c96b9bcf6fb079ec9c4f02f2ed2f66e6c;p=thirdparty%2Fpatchwork.git views: Make 'patch_to_mbox' return a string The 'patch_to_mbox' function returns an object which is coverted to a string in all places where this call occurs. The string conversion differs between Python 2 and 3 and while it has been updated in one place, it was missed in two others. Resolve these issues and ensure they don't happen again by returning strings from 'patch_to_mbox' instead. Signed-off-by: Stephen Finucane Reviewed-by: Daniel Axtens --- diff --git a/patchwork/views/__init__.py b/patchwork/views/__init__.py index 7354f703..db53cdf4 100644 --- a/patchwork/views/__init__.py +++ b/patchwork/views/__init__.py @@ -29,6 +29,7 @@ import re from django.contrib import messages from django.shortcuts import get_object_or_404 +from django.utils import six from patchwork.filters import Filters from patchwork.forms import MultiplePatchForm @@ -400,4 +401,10 @@ def patch_to_mbox(patch): if 'Date' not in mail: mail['Date'] = email.utils.formatdate(utc_timestamp) + # NOTE(stephenfin) http://stackoverflow.com/a/28584090/613428 + if six.PY3: + mail = mail.as_bytes(True).decode() + else: + mail = mail.as_string(True) + return mail diff --git a/patchwork/views/bundle.py b/patchwork/views/bundle.py index b1038393..09798473 100644 --- a/patchwork/views/bundle.py +++ b/patchwork/views/bundle.py @@ -142,14 +142,12 @@ def bundle_mbox(request, username, bundlename): (basic_auth and basic_auth.authenticate(request))): return HttpResponseNotFound() - mbox = '\n'.join([patch_to_mbox(p).as_string(True) - for p in bundle.ordered_patches()]) - response = HttpResponse(content_type='text/plain') response['Content-Disposition'] = \ 'attachment; filename=bundle-%d-%s.mbox' % (bundle.id, bundle.name) + response.write('\n'.join( + [patch_to_mbox(p) for p in bundle.ordered_patches()])) - response.write(mbox) return response diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py index 469a53a8..705359f7 100644 --- a/patchwork/views/patch.py +++ b/patchwork/views/patch.py @@ -26,7 +26,6 @@ from django.http import HttpResponse from django.http import HttpResponseForbidden from django.http import HttpResponseRedirect from django.shortcuts import render, get_object_or_404 -from django.utils import six from patchwork.forms import PatchForm, CreateBundleForm from patchwork.models import Patch, Project, Bundle, Submission @@ -121,17 +120,15 @@ def patch_raw(request, patch_id): response.write(patch.diff) response['Content-Disposition'] = 'attachment; filename=' + \ patch.filename.replace(';', '').replace('\n', '') + return response def patch_mbox(request, patch_id): patch = get_object_or_404(Patch, id=patch_id) response = HttpResponse(content_type="text/plain") - # NOTE(stephenfin) http://stackoverflow.com/a/28584090/613428 - if six.PY3: - response.write(patch_to_mbox(patch).as_bytes(True).decode()) - else: - response.write(patch_to_mbox(patch).as_string(True)) + response.write(patch_to_mbox(patch)) response['Content-Disposition'] = 'attachment; filename=' + \ patch.filename.replace(';', '').replace('\n', '') + return response diff --git a/patchwork/views/xmlrpc.py b/patchwork/views/xmlrpc.py index bb8b7299..80e96b2f 100644 --- a/patchwork/views/xmlrpc.py +++ b/patchwork/views/xmlrpc.py @@ -694,7 +694,7 @@ def patch_get_mbox(patch_id): """ try: patch = Patch.objects.get(id=patch_id) - return patch_to_mbox(patch).as_string(True) + return patch_to_mbox(patch) except Patch.DoesNotExist: return ''