On Fri, Jun 20, 2008 at 2:15 AM, Wichert Akkerman <[EMAIL PROTECTED]> wrote:

> I think the deployment discussion from a few days ago comes down to
> this: how can you use url_for to generate a link for a static resource.
>
> One person reported doing that but nobody seems to be able to reproduce
> that. Does anyone know if this is really possible?

You can define a regular route to a URL in your public directory, but
don't use _static=True because that only works with external routes.
Also, it can't take route variables.  In other words, _static is
fairly useless.  Routes 2, which is under development, will replace it
with true generation-only routes.

I have two applications that share a static directory pointing to a
large hierarchy of user-uploaded files.  In my first app I tried
_static and failed, didn't trust a regular route that might match
someday (and what controller & action do I point it to?), and ended up
building literal URLs the old-fashioned way.

In my second app, I brought the directory into Pylons as an action
using FileApp.  This requires a bit of unusual syntax.

from pylons import config
import os
from paste.fileapp import FileApp

def attachment(self, id1, id2, filename, environ, start_response)
    """id1, id2, and filename are the literal directory, subdirectory,
and filename of the desired attachment.

         environ and start_response are special features of Pylons,
given to any action which asks for them.
    """
    self._REQUIRE_PERM("view_attachment", id1=id1)   # Abort if user
doesn't have permission.
    attachments = config["attachments_dir"]
    path = os.path.join(attachments, str(id1), str(id2), filename)
    app = FileApp(path)
    return app(environ, start_response)

In other words, we create a WSGI application that serves that exact
file, and invoke it in the normal WSGI manner.  Pylons somehow
recognizes the return value and does the right thing.  FileApp takes
care of setting the MIME type based on the filename, and raises a 404
if it doesn't exist.

There are a couple other classes in Paste for looking in a static
directory, but i couldn't get them to work.

-- 
Mike Orr <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to