diff --git a/tailbone/views/core.py b/tailbone/views/core.py index 40ff7de4..4f989b2d 100644 --- a/tailbone/views/core.py +++ b/tailbone/views/core.py @@ -1,8 +1,8 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8; -*- ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2016 Lance Edgar +# Copyright © 2010-2017 Lance Edgar # # This file is part of Rattail. # @@ -26,10 +26,13 @@ Base View Class from __future__ import unicode_literals, absolute_import +import os + from rattail.db import model from pyramid import httpexceptions from pyramid.renderers import render_to_response +from pyramid.response import FileResponse from tailbone.db import Session @@ -77,6 +80,18 @@ class View(object): """ return render_to_response('/progress.mako', kwargs, request=self.request) + def file_response(self, path): + """ + Returns a generic FileResponse from the given path + """ + if not os.path.exists(path): + return self.notfound() + response = FileResponse(path, request=self.request) + response.headers[b'Content-Length'] = str(os.path.getsize(path)) + filename = os.path.basename(path).encode('ascii', 'replace') + response.headers[b'Content-Disposition'] = b'attachment; filename="{}"'.format(filename) + return response + def fake_error(request): """ diff --git a/tailbone/views/reports.py b/tailbone/views/reports.py index 82b7a4f5..fc432c4f 100644 --- a/tailbone/views/reports.py +++ b/tailbone/views/reports.py @@ -196,9 +196,13 @@ class InventoryWorksheet(View): class ReportOutputView(ExportMasterView): + """ + Master view for report output + """ model_class = model.ReportOutput route_prefix = 'report_output' url_prefix = '/reports/generated' + downloadable = True def configure_grid(self, g): g.configure( @@ -229,6 +233,11 @@ class ReportOutputView(ExportMasterView): fs.created_by, ]) + def download(self): + report = self.get_instance() + path = report.filepath(self.rattail_config) + return self.file_response(path) + def add_routes(config): config.add_route('reports.ordering', '/reports/ordering')