]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
models: Centralize generation of filenames
authorStephen Finucane <stephen@that.guru>
Tue, 30 May 2017 20:33:58 +0000 (21:33 +0100)
committerStephen Finucane <stephen@that.guru>
Tue, 30 May 2017 20:40:11 +0000 (21:40 +0100)
Move filename generation to a mixin. This allows us to reuse the code
for other items like cover letters. Some unncessary 'strip' calls are
removed as their unnecessary.

This allows us to change the file extension for diffs to 'diff', which
is a little more accurate.

Signed-off-by: Stephen Finucane <stephen@that.guru>
patchwork/models.py
patchwork/views/patch.py
patchwork/views/series.py
releasenotes/notes/patch-download-diff-extension-88c9963eac6ba4aa.yaml [new file with mode: 0644]

index ee66ace92b74461c3f75c56c7f1e00add9e6ba95..8678373cf185a3226d9af1548bca124bcc0f32f3 100644 (file)
@@ -315,8 +315,18 @@ 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(EmailMixin, models.Model):
+class Submission(FilenameMixin, EmailMixin, models.Model):
     # parent
 
     project = models.ForeignKey(Project, on_delete=models.CASCADE)
@@ -436,12 +446,6 @@ class Patch(SeriesMixin, Submission):
 
         return self.project.is_editable(user)
 
-    @property
-    def filename(self):
-        fname_re = re.compile(r'[^-_A-Za-z0-9\.]+')
-        str = fname_re.sub('-', self.name)
-        return str.strip('-') + '.patch'
-
     @property
     def combined_check_state(self):
         """Return the combined state for all checks.
@@ -570,7 +574,7 @@ class Comment(EmailMixin, models.Model):
 
 
 @python_2_unicode_compatible
-class Series(models.Model):
+class Series(FilenameMixin, models.Model):
     """An collection of patches."""
 
     # parent
@@ -605,12 +609,6 @@ class Series(models.Model):
     def received_all(self):
         return self.total <= self.received_total
 
-    @property
-    def filename(self):
-        fname_re = re.compile(r'[^-_A-Za-z0-9\.]+')
-        fname = fname_re.sub('-', str(self))
-        return fname.strip('-') + '.patch'
-
     def add_cover_letter(self, cover):
         """Add a cover letter to the series.
 
index 09705ef4b518defe3d7962c87b2788dc01dece7b..c447b1c85774d018b5cbb6cc5fcd33b85c782e45 100644 (file)
@@ -123,8 +123,8 @@ def patch_raw(request, patch_id):
 
     response = HttpResponse(content_type="text/x-patch")
     response.write(patch.diff)
-    response['Content-Disposition'] = 'attachment; filename=' + \
-        patch.filename.replace(';', '').replace('\n', '')
+    response['Content-Disposition'] = 'attachment; filename=%s.diff' % (
+        patch.filename)
 
     return response
 
@@ -138,7 +138,7 @@ def patch_mbox(request, patch_id):
         response.write(series_patch_to_mbox(patch, series_id))
     else:
         response.write(patch_to_mbox(patch))
-    response['Content-Disposition'] = 'attachment; filename=' + \
-        patch.filename.replace(';', '').replace('\n', '')
+    response['Content-Disposition'] = 'attachment; filename=%s.patch' % (
+        patch.filename)
 
     return response
index a59bffdd235aaf791abb8c552ceb9f85759fa5fd..ce528530e752b9d366e47e87c451fbcf32eeb195 100644 (file)
@@ -29,7 +29,7 @@ def series_mbox(request, series_id):
 
     response = HttpResponse(content_type='text/plain')
     response.write(series_to_mbox(series))
-    response['Content-Disposition'] = 'attachment; filename=' + \
-        series.filename.replace(';', '').replace('\n', '')
+    response['Content-Disposition'] = 'attachment; filename=%s.patch' % (
+        series.filename)
 
     return response
diff --git a/releasenotes/notes/patch-download-diff-extension-88c9963eac6ba4aa.yaml b/releasenotes/notes/patch-download-diff-extension-88c9963eac6ba4aa.yaml
new file mode 100644 (file)
index 0000000..fc3c38a
--- /dev/null
@@ -0,0 +1,4 @@
+---
+other:
+  - |
+    Patch diffs now download with a ``diff`` extension.