[PATCH 4/6] views: Add 'series' parameter to '/mbox' endpoint

Stephen Finucane stephen at that.guru
Tue Jan 31 09:59:42 AEDT 2017


This allows a user to download dependencies for a patch without having
to do it manually.

Signed-off-by: Stephen Finucane <stephen at that.guru>
---
 patchwork/views/__init__.py | 50 +++++++++++++++++++++++++++++++++++++++++++++
 patchwork/views/patch.py    | 27 ++++++++++++++++--------
 2 files changed, 69 insertions(+), 8 deletions(-)

diff --git a/patchwork/views/__init__.py b/patchwork/views/__init__.py
index 298e0e2..e375f97 100644
--- a/patchwork/views/__init__.py
+++ b/patchwork/views/__init__.py
@@ -38,6 +38,7 @@ from patchwork.models import BundlePatch
 from patchwork.models import Comment
 from patchwork.models import Patch
 from patchwork.models import Project
+from patchwork.models import Series
 from patchwork.paginator import Paginator
 
 
@@ -354,6 +355,14 @@ class PatchMbox(MIMENonMultipart):
 
 
 def patch_to_mbox(patch):
+    """Get an mbox representation of a single patch.
+
+    Arguments:
+        patch: The Patch object to convert.
+
+    Returns:
+        A string for the mbox file.
+    """
     postscript_re = re.compile('\n-{2,3} ?\n')
     body = ''
 
@@ -411,4 +420,45 @@ def patch_to_mbox(patch):
 
 
 def bundle_to_mbox(bundle):
+    """Get an mbox representation of a bundle.
+
+    Arguments:
+        patch: The Bundle object to convert.
+
+    Returns:
+        A string for the mbox file.
+    """
     return '\n'.join([patch_to_mbox(p) for p in bundle.ordered_patches()])
+
+
+def series_patch_to_mbox(patch, series_num):
+    """Get an mbox representation of a patch with dependencies.
+
+    Arguments:
+        patch: The Patch object to convert.
+        series_num: The series number to retrieve dependencies from.
+
+    Returns:
+        A string for the mbox file.
+    """
+    try:
+        series_num = int(series_num)
+    except ValueError:
+        # TODO(stephenfin): Perhaps we should raise a HTTP code here?
+        return ''
+
+    try:
+        series = patch.series.get(id=series_num)
+    except Series.DoesNotExist:
+        return ''
+
+    mbox = []
+
+    # get the series-ified patch
+    number = series.seriespatch_set.get(patch=patch).number
+    for dep in series.seriespatch_set.filter(number__lt=number):
+        mbox.append(patch_to_mbox(dep.patch))
+
+    mbox.append(patch_to_mbox(patch))
+
+    return '\n'.join(mbox)
diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py
index 705359f..eda33b0 100644
--- a/patchwork/views/patch.py
+++ b/patchwork/views/patch.py
@@ -17,19 +17,24 @@
 # along with Patchwork; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-from __future__ import absolute_import
-
 from django.contrib import messages
 from django.core import urlresolvers
 from django.http import Http404
 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.shortcuts import get_object_or_404
+from django.shortcuts import render
 
-from patchwork.forms import PatchForm, CreateBundleForm
-from patchwork.models import Patch, Project, Bundle, Submission
-from patchwork.views import generic_list, patch_to_mbox
+from patchwork.forms import CreateBundleForm
+from patchwork.forms import PatchForm
+from patchwork.models import Bundle
+from patchwork.models import Patch
+from patchwork.models import Project
+from patchwork.models import Submission
+from patchwork.views import generic_list
+from patchwork.views import patch_to_mbox
+from patchwork.views import series_patch_to_mbox
 
 
 def patch_list(request, project_id):
@@ -116,6 +121,7 @@ def patch_detail(request, patch_id):
 
 def patch_raw(request, patch_id):
     patch = get_object_or_404(Patch, id=patch_id)
+
     response = HttpResponse(content_type="text/x-patch")
     response.write(patch.diff)
     response['Content-Disposition'] = 'attachment; filename=' + \
@@ -126,8 +132,13 @@ def patch_raw(request, patch_id):
 
 def patch_mbox(request, patch_id):
     patch = get_object_or_404(Patch, id=patch_id)
-    response = HttpResponse(content_type="text/plain")
-    response.write(patch_to_mbox(patch))
+    series_num = request.GET.get('series')
+
+    response = HttpResponse(content_type='text/plain')
+    if series_num:
+        response.write(series_patch_to_mbox(patch, series_num))
+    else:
+        response.write(patch_to_mbox(patch))
     response['Content-Disposition'] = 'attachment; filename=' + \
         patch.filename.replace(';', '').replace('\n', '')
 
-- 
2.9.3



More information about the Patchwork mailing list