Add file download support by default for report output

This commit is contained in:
Lance Edgar 2017-03-22 14:35:40 -05:00
parent 6adb99003d
commit 7975d177d6
2 changed files with 26 additions and 2 deletions

View file

@ -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):
"""

View file

@ -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')