views:

185

answers:

1

Hi,

I've got a Pylons controller with an action called serialize returning content_type=text/csv. I'd like the response of the action to be named based on the input patameter, i.e. for the following route, produced csv file should be named {id}.csv : /app/PROD/serialize => PROD.csv (so a user can open the file in Excel with a proper name directly via a webbrowser)

map.connect('/app/{id}/serialize',controller = 'csvproducer',action='serialize')

I've tried to set different HTTP headers and properties of the webob's response object with no luck. However, I figured out a workaround by simply adding a new action to the controller and dynamically redirecting the original action to that new action, i.e.:

map.connect('/app/{id}/serialize',controller = 'csvproducer',action='serialize')
map.connect('/app/csv/{foo}',controller = 'csvproducer', action='tocsv')

The controller's snippet:

def serialize(self,id):
  try:
    session['key'] = self.service.serialize(id) #produces csv content
    session.save()
    redirect_to(str("/app/csv/%s.csv" % id))
  except Exception,e:
    log.error(e)
    abort(503)

def tocsv(self):
  try:
    csv = session.pop("rfa.enviornment.serialize")
  except Exception,e:
    log.error(e)
    abort(503)
  if csv:
    response.content_type='text/csv'
    response.status_int=200
    response.write(csv)
  else:
    abort(404)

The above setup works perfectly fine, however, is there a better/slicker/neater way of doing it? Ideally I wouldn't like to redirect the request; instead I'd like to either rename location or set content-disposition: attachment; filename='XXX.csv' [ unsuccessfully tried both :( ]

Am I missing something obvious here?

Cheers

UPDATE: Thanks to ebo I've managed to do fix content-disposition. Should better read W3C specs next time ;)

+1  A: 

You should be able to set the content-disposition header on a response object.

If you have already tried that, it may not have worked because the http standard says that the quotes should be done by double-quote marks.

ebo
Cheers, spot on, that was it! Missing double quotes. Darn! I spent too much time coding Python >:)
Dave