diff mbox series

[1/5] Add option to get all project patches in one mbox

Message ID 20190628114314.10462-2-metepolat2000@gmail.com
State Superseded
Headers show
Series Update REST API: Add 'project patches as mbox' field | expand

Commit Message

Mete Polat June 28, 2019, 11:43 a.m. UTC
FilenameMixin.filename(self) returns a sanitized filename based on the str
representation of the object. In our case str(project) returns the project name.
Thus the output file will be <project_name_sanitized>.mbox

Signed-off-by: Mete Polat <metepolat2000@gmail.com>
---
patch_list_mbox() and project_patches_to_mbox() are not named project_mbox() and
project_to_mbox() in order to prevent confusion. While a project also consists
of cover letters, those are not intended to be included.

 patchwork/models.py      | 22 +++++++++++-----------
 patchwork/views/patch.py | 12 ++++++++++++
 patchwork/views/utils.py | 13 +++++++++++++
 3 files changed, 36 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/patchwork/models.py b/patchwork/models.py
index a7eee4d..36d6fad 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -55,8 +55,18 @@  class Person(models.Model):
         verbose_name_plural = 'People'
 
 
+class FilenameMixin(object):
+
+    @property
+    def filename(self):
+        """Return a sanitized filename without extension."""
+        fname_re = re.compile(r'[^-_A-Za-z0-9\.]+')
+        fname = fname_re.sub('-', str(self)).strip('-')
+        return fname
+
+
 @python_2_unicode_compatible
-class Project(models.Model):
+class Project(FilenameMixin, models.Model):
     # properties
 
     linkname = models.CharField(max_length=255, unique=True)
@@ -337,16 +347,6 @@  class EmailMixin(models.Model):
         abstract = True
 
 
-class FilenameMixin(object):
-
-    @property
-    def filename(self):
-        """Return a sanitized filename without extension."""
-        fname_re = re.compile(r'[^-_A-Za-z0-9\.]+')
-        fname = fname_re.sub('-', str(self)).strip('-')
-        return fname
-
-
 @python_2_unicode_compatible
 class Submission(FilenameMixin, EmailMixin, models.Model):
     # parent
diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py
index 277b281..cf494ad 100644
--- a/patchwork/views/patch.py
+++ b/patchwork/views/patch.py
@@ -20,6 +20,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 project_patches_to_mbox
 from patchwork.views.utils import series_patch_to_mbox
 
 
@@ -34,6 +35,17 @@  def patch_list(request, project_id):
     return render(request, 'patchwork/list.html', context)
 
 
+def patch_list_mbox(request, project_id):
+    project = get_object_or_404(Project, linkname=project_id)
+
+    response = HttpResponse(content_type='text/plain')
+    response.write(project_patches_to_mbox(project))
+    response['Content-Disposition'] = 'attachment; filename=%s.mbox' % (
+        project.filename)
+
+    return response
+
+
 def patch_detail(request, patch_id):
     # redirect to cover letters where necessary
     try:
diff --git a/patchwork/views/utils.py b/patchwork/views/utils.py
index 1da1aaa..1461525 100644
--- a/patchwork/views/utils.py
+++ b/patchwork/views/utils.py
@@ -110,6 +110,19 @@  patch_to_mbox = _submission_to_mbox
 cover_to_mbox = _submission_to_mbox
 
 
+def project_patches_to_mbox(project):
+    """Get an mbox representation of all patches in a project.
+
+    Arguments:
+        project: The project object to convert.
+
+    Returns:
+        A string for the mbox file.
+    """
+    patches = Patch.objects.filter(patch_project=project)
+    return '\n'.join([patch_to_mbox(p) for p in patches])
+
+
 def bundle_to_mbox(bundle):
     """Get an mbox representation of a bundle.