[Grok-dev] tramline hints

Martijn Faassen faassen at startifact.com
Fri Feb 5 14:04:08 EST 2010

Hi there,

I heard people were asking about how to use tramline. It's been some 
time since I wrote it and hurry.file, but here are some hints.

Here's the tramline README.txt and INSTALL.txt



You set up Apache as in INSTALL.txt. You can tell it there where to 
store the files on the filesystem.

Tramline will magically turn all incoming file uploads in form submits 
into small ids. These you can just store in the ZODB as if they were a 
file, or a string, or whatever. Do make sure that after the upload you 
add a 'tramline_ok' header to confirm things were uploaded all right and 
accepted by the app server; if you don't (or if it never even gets there 
due to security checks, etc) it won't be stored on the filesystem 
either. But as long as you send out tramline_ok in the response header, 
any file upload will always end up on the filesystem then.

The trick is now to use Tramline to turn those ids back into files. To 
do that, you just serve the id, for instance by doing this:

class MyFileDownload(grok.View):
     def render(self):
          self.response.addHeader('tramline_file', 'ok')
          return self.context.the_stored_file_that_is_really_the_id

the response header will signal tramline to look up that id in the 

Now hurry.file has machinery to help automate some of this.

If you use hurry.file, you need to make sure you define a utility that 
configures how to actually get the underlying file from the filesystem. 
For instance:

class TramlineFileRetrieval(TramlineFileRetrievalBase,


      def getTramlinePath(self):
           return ...

where it returns a path to where the tramline file structure is stored. 
This offers some functionality to be able to mess with files stored by 
tramline directly from the application.

More info on this is here:


and in the interfaces.py of hurry.file should you wish to use 
IFileRetrieval directly.

Once you use the hurry.file widget in your form, and you have tramline 
configured as a front end, it'll now automatically store fake file 
objects in the ZODB for you. You can still use these to access the data 
on the file system, as if it were a Python object. It'll also send 
tramline_ok. You'll still have to do tramline_file yourself when you 
send out the file directly.

Note that these days it might be easier to use a WSGI middleware to 
accomplish the same effect as tramline, though I do suspect Tramline 
probably offers quite a bit of performance.



More information about the Grok-dev mailing list