views:

100

answers:

0

We have a TurboGears 2.1 application where we have added several renderers, but I'm having trouble getting them to set the correct MIME type.

In config/app_cfg.py we enable the renderers:

class LITSAdminConfig(AppConfig):
    def setup_csv_renderer(self):
        self.render_functions.csv = render.render_csv
    def setup_pdf_renderer(self):
        self.render_functions.pdf = render.render_pdf
    def setup_xls_renderer(self):
        self.render_functions.xls = render.render_xls

base_config = LITSAdminConfig()
base_config.renderers = []
base_config.renderers.append('csv')
base_config.renderers.append('pdf')
base_config.renderers.append('xls')
base_config.mimetype_lookup = {
    '.csv': 'text/csv',
    '.pdf': 'application/pdf',
    '.xls': 'application/vnd.ms-excel',
}

Then in the controller we expose them like so:

@expose('pdf:compreg.print_department_computers')
@validate(validators={"dept": ValidDepartment()})
def print_department_computers(self, dept):

This happily spits out a PDF at the correct URL (both with and without a .pdf extension before the arguments), but with the MIME type set to text/html. I have currently worked around it by explicitly setting the MIME type in the renderer:

template_vars.response.headers['Content-Type'] = "application/pdf"
template_vars.response.headers["Content-Disposition"] = "attachment; filename=%s" % filename

What is the correct way to do it?