[v3,6/9] views: Add 'series' parameter to '/mbox' endpoint

Submitted by Stephen Finucane on March 7, 2017, 7:46 p.m.

Details

Message ID 20170307194624.21582-7-stephen@that.guru
State Superseded
Headers show

Commit Message

Stephen Finucane March 7, 2017, 7:46 p.m.
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>
---
v2:
- Don't silently fail on invalid series
- Move unrelated changes to separate patches
---
 patchwork/views/patch.py | 11 +++++++++--
 patchwork/views/utils.py | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py
index a4cea2f..3c8cb25 100644
--- a/patchwork/views/patch.py
+++ b/patchwork/views/patch.py
@@ -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', '')
 
diff --git a/patchwork/views/utils.py b/patchwork/views/utils.py
index 900480b..f936ed8 100644
--- a/patchwork/views/utils.py
+++ b/patchwork/views/utils.py
@@ -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)