Tweaks for export views, to make more generic
This commit is contained in:
parent
96185d17bd
commit
cfa9c95814
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue