From: Stephen Finucane Date: Mon, 9 Jan 2017 12:23:47 +0000 (+0000) Subject: views: Add 'series' parameter to '/mbox' endpoint X-Git-Tag: v2.0.0-rc1~52 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e2dfd490d1250ae7cba58377903b2bcba082e75d;p=thirdparty%2Fpatchwork.git views: Add 'series' parameter to '/mbox' endpoint 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 Reviewed-by: Daniel Axtens --- diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py index a4cea2fe..3c8cb256 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 900480b5..f936ed8e 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)