From: Stephen Finucane Date: Tue, 30 May 2017 20:33:58 +0000 (+0100) Subject: models: Centralize generation of filenames X-Git-Tag: v2.0.0-rc3~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=35d696ea6c709cd154bc9910838d2e11b5c2652d;p=thirdparty%2Fpatchwork.git models: Centralize generation of filenames 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 --- diff --git a/patchwork/models.py b/patchwork/models.py index ee66ace9..8678373c 100644 --- a/patchwork/models.py +++ b/patchwork/models.py @@ -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. diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py index 09705ef4..c447b1c8 100644 --- a/patchwork/views/patch.py +++ b/patchwork/views/patch.py @@ -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 diff --git a/patchwork/views/series.py b/patchwork/views/series.py index a59bffdd..ce528530 100644 --- a/patchwork/views/series.py +++ b/patchwork/views/series.py @@ -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 index 00000000..fc3c38a9 --- /dev/null +++ b/releasenotes/notes/patch-download-diff-extension-88c9963eac6ba4aa.yaml @@ -0,0 +1,4 @@ +--- +other: + - | + Patch diffs now download with a ``diff`` extension.