From cfa9c95814bb1633d74c2f1ef54840ba52423869 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 19 Aug 2020 17:16:54 -0500 Subject: [PATCH] Tweaks for export views, to make more generic --- tailbone/views/exports.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tailbone/views/exports.py b/tailbone/views/exports.py index 793810f2..c136359a 100644 --- a/tailbone/views/exports.py +++ b/tailbone/views/exports.py @@ -27,6 +27,7 @@ Master class for generic export history views from __future__ import unicode_literals, absolute_import import os +import shutil import six @@ -46,6 +47,7 @@ class ExportMasterView(MasterView): creatable = False editable = False downloadable = False + delete_export_files = False grid_columns = [ 'id', @@ -65,8 +67,11 @@ class ExportMasterView(MasterView): if hasattr(self, 'export_key'): return self.export_key + cls = self.get_model_class() + return cls.export_key + def get_file_path(self, export, makedirs=False): - return self.rattail_config.export_filepath(self.export_key, + return self.rattail_config.export_filepath(self.get_export_key(), export.uuid, export.filename, makedirs=makedirs) @@ -89,6 +94,7 @@ class ExportMasterView(MasterView): g.set_label('created_by', "Created by") g.set_link('id') + g.set_link('filename') def render_id(self, export, field): return export.id_str @@ -129,6 +135,13 @@ class ExportMasterView(MasterView): else: f.set_readonly('record_count') + # filename + if self.editing: + f.remove_field('filename') + else: + f.set_readonly('filename') + f.set_renderer('filename', self.render_downloadable_file) + def objectify(self, form, data=None): obj = super(ExportMasterView, self).objectify(form, data=data) if self.creating: @@ -169,3 +182,17 @@ class ExportMasterView(MasterView): response.headers[b'Content-Length'] = six.binary_type(os.path.getsize(path)) response.headers[b'Content-Disposition'] = b'attachment; filename="{}"'.format(export.filename) return response + + def delete_instance(self, export): + """ + Delete the export's files as well as the export itself. + """ + # delete files for the export, if applicable + if self.delete_export_files: + path = self.get_file_path(export) + dirname = os.path.dirname(path) + if os.path.exists(dirname): + shutil.rmtree(dirname) + + # continue w/ normal deletion + super(ExportMasterView, self).delete_instance(export)