]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
views: Add 'series' parameter to '/mbox' endpoint
authorStephen Finucane <stephen@that.guru>
Mon, 9 Jan 2017 12:23:47 +0000 (12:23 +0000)
committerStephen Finucane <stephen@that.guru>
Tue, 4 Apr 2017 15:24:07 +0000 (16:24 +0100)
This allows a user to download dependencies for a patch without having
to do it manually. This is primarily aimed at users testing patches.

Signed-off-by: Stephen Finucane <stephen@that.guru>
Reviewed-by: Daniel Axtens <dja@axtens.net>
patchwork/views/patch.py
patchwork/views/utils.py

index a4cea2fe60192e33373ca92d6391d3485ec0e922..3c8cb2566071117659efeeda4a8de3da5157620f 100644 (file)
@@ -34,6 +34,7 @@ from patchwork.models import Project
 from patchwork.models import Submission
 from patchwork.views import generic_list
 from patchwork.views.utils import patch_to_mbox
+from patchwork.views.utils import series_patch_to_mbox
 
 
 def patch_list(request, project_id):
@@ -120,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=' + \
@@ -130,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', '')
 
index 900480b58049f21b8e363bf2b3fddffd9a3c38a1..f936ed8e72dab73cd4b4edfacef882f169c07a79 100644 (file)
@@ -26,9 +26,11 @@ from email.parser import HeaderParser
 import email.utils
 import re
 
+from django.http import Http404
 from django.utils import six
 
 from patchwork.models import Comment
+from patchwork.models import Series
 
 
 class PatchMbox(MIMENonMultipart):
@@ -116,3 +118,36 @@ def bundle_to_mbox(bundle):
         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:
+        raise Http404('Expected integer series value. Received: %r' %
+                      series_num)
+
+    try:
+        series = patch.series.get(id=series_num)
+    except Series.DoesNotExist:
+        raise Http404('Patch does not belong to series %d' % series_num)
+
+    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)