]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
views: Make 'patch_to_mbox' return a string
authorStephen Finucane <stephen@that.guru>
Mon, 9 Jan 2017 12:06:14 +0000 (12:06 +0000)
committerStephen Finucane <stephen@that.guru>
Tue, 4 Apr 2017 14:11:34 +0000 (15:11 +0100)
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 <stephen@that.guru>
Reviewed-by: Daniel Axtens <dja@axtens.net>
patchwork/views/__init__.py
patchwork/views/bundle.py
patchwork/views/patch.py
patchwork/views/xmlrpc.py

index 7354f70334002f133ba172175f9fde9f4cdf037e..db53cdf47a493dd98dbb770be266a2a4643fffa1 100644 (file)
@@ -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
index b10383933e4adebb4c59983101672f8bb462dbf9..09798473538fc1be318d9180f97cdd00a14be679 100644 (file)
@@ -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
 
 
index 469a53a80ec0b1c401ae992f20e8b8724ee98fab..705359f73eabc809a45727f16f4c8bd0d63ecfa8 100644 (file)
@@ -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
index bb8b7299a96eb812c47c7e5a878fe5b83bdefd7c..80e96b2f43019ca4e3b855b436ce3d8ee56ffe81 100644 (file)
@@ -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 ''