Tweaks for export views, to make more generic

This commit is contained in:
Lance Edgar 2020-08-19 17:16:54 -05:00
parent 96185d17bd
commit cfa9c95814

View file

@ -27,6 +27,7 @@ Master class for generic export history views
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
import os import os
import shutil
import six import six
@ -46,6 +47,7 @@ class ExportMasterView(MasterView):
creatable = False creatable = False
editable = False editable = False
downloadable = False downloadable = False
delete_export_files = False
grid_columns = [ grid_columns = [
'id', 'id',
@ -65,8 +67,11 @@ class ExportMasterView(MasterView):
if hasattr(self, 'export_key'): if hasattr(self, 'export_key'):
return self.export_key return self.export_key
cls = self.get_model_class()
return cls.export_key
def get_file_path(self, export, makedirs=False): 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.uuid,
export.filename, export.filename,
makedirs=makedirs) makedirs=makedirs)
@ -89,6 +94,7 @@ class ExportMasterView(MasterView):
g.set_label('created_by', "Created by") g.set_label('created_by', "Created by")
g.set_link('id') g.set_link('id')
g.set_link('filename')
def render_id(self, export, field): def render_id(self, export, field):
return export.id_str return export.id_str
@ -129,6 +135,13 @@ class ExportMasterView(MasterView):
else: else:
f.set_readonly('record_count') 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): def objectify(self, form, data=None):
obj = super(ExportMasterView, self).objectify(form, data=data) obj = super(ExportMasterView, self).objectify(form, data=data)
if self.creating: 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-Length'] = six.binary_type(os.path.getsize(path))
response.headers[b'Content-Disposition'] = b'attachment; filename="{}"'.format(export.filename) response.headers[b'Content-Disposition'] = b'attachment; filename="{}"'.format(export.filename)
return response 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)