url_for for static resources

17 views
Skip to first unread message

Wichert Akkerman

unread,
Jun 20, 2008, 5:15:41 AM6/20/08
to pylons-...@googlegroups.com
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?

Wichert.

--
Wichert Akkerman <wic...@wiggy.net> It is simple to make things.
http://www.wiggy.net/ It is hard to make things simple.

Antonio Beamud Montero

unread,
Jun 20, 2008, 8:05:05 AM6/20/08
to pylons-...@googlegroups.com

El vie, 20-06-2008 a las 11:15 +0200, Wichert Akkerman escribió:
> 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?

Well, I think the problem is generating a link for a static resource in
a remote location...

My idea is something like define a url prefix for a static type, for
example:

public.css = http://mydomain.com/content/

And the url_for magically transform this resource type url to:
http://mydomain.com/content/css/main.css for example...

My 0.02€ :)

Greetings


> Wichert.
>

Devin Torres

unread,
Jun 20, 2008, 3:41:02 PM6/20/08
to pylons-...@googlegroups.com
You mean like map.connect('my_named_static_resource',
'http://google.com', _static=True) ?

-Devin

Wichert Akkerman

unread,
Jun 20, 2008, 4:55:22 PM6/20/08
to pylons-...@googlegroups.com
Previously Devin Torres wrote:
> You mean like map.connect('my_named_static_resource',
> 'http://google.com', _static=True) ?

Almost. I also want to be able to create a static route which points to
the root of the existing site. That's the bit that still escapes me.
That would allow me to easily switch from internally-hosted resources to
externally hosted resources.

Ben Bangert

unread,
Jun 20, 2008, 5:54:51 PM6/20/08
to pylons-...@googlegroups.com
On Jun 20, 2008, at 1:55 PM, Wichert Akkerman wrote:

> Previously Devin Torres wrote:
>> You mean like map.connect('my_named_static_resource',
>> 'http://google.com', _static=True) ?
>
> Almost. I also want to be able to create a static route which points
> to
> the root of the existing site. That's the bit that still escapes me.
> That would allow me to easily switch from internally-hosted
> resources to
> externally hosted resources.

So put something like:

static_resources = http://google.com

in your ini file (so you can change it on the fly, maybe to a CDN when
deployed). Then in your routing.py:

from pylons import config
from routes import Mapper

def make_map():
"""Create, configure and return the routes Mapper"""
map = Mapper(directory=config['pylons.paths']['controllers'],
always_scan=config['debug'])

map.connect('my_static_resource', config['static_resources'],
_static=True)

Routes static resources currently don't let you specify options
directly to them, which means this is the slightly hacky workaround:
map.connect('my_static_resource', '%s/%%s' %
config['static_resources'], _static=True)

Then in templates and such:
url_for('my_static_resource') % 'some/url/here'

This is what we do to switch to a CDN in the PylonsHQ site when
deployed.

Cheers,
Ben

Jonathan Vanasco

unread,
Jun 21, 2008, 10:53:39 AM6/21/08
to pylons-discuss
i had a similar situation -- i wanted to change url_for's output on
people who have a 'preview' cookie and those who don't

i basically did this

def url_for_custom( url ):
rval = url_for(url)
if logic_test():
rval = regex or stringsub or both
return rval

then quickly did a find/replace on the entire pylons app with url_for -
> url_for_custom

its ridiculously not messy, simple, and gets the job done.

Shannon -jj Behrens

unread,
Jun 21, 2008, 10:22:13 PM6/21/08
to pylons-...@googlegroups.com

url_for comes from webhelpers. Can't you just hijack it there and
reuse the same name?

-jj

--
It's a walled garden, but the flowers sure are lovely!
http://jjinux.blogspot.com/

Mike Orr

unread,
Jun 26, 2008, 1:24:35 PM6/26/08
to pylons-...@googlegroups.com
On Fri, Jun 20, 2008 at 2:15 AM, Wichert Akkerman <wic...@wiggy.net> 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 <slugg...@gmail.com>

Mike Orr

unread,
Jun 26, 2008, 1:43:14 PM6/26/08
to pylons-...@googlegroups.com
On Sat, Jun 21, 2008 at 7:22 PM, Shannon -jj Behrens <jji...@gmail.com> wrote:
>
> On Sat, Jun 21, 2008 at 7:53 AM, Jonathan Vanasco <jona...@findmeon.com> wrote:
>> i had a similar situation -- i wanted to change url_for's output on
>> people who have a 'preview' cookie and those who don't
>>
>> i basically did this
>>
>> def url_for_custom( url ):
>> rval = url_for(url)
>> if logic_test():
>> rval = regex or stringsub or both
>> return rval
>>
>> then quickly did a find/replace on the entire pylons app with url_for -
>>> url_for_custom
>>
>> its ridiculously not messy, simple, and gets the job done.
>
> url_for comes from webhelpers. Can't you just hijack it there and
> reuse the same name?

url_for comes from Routes. It's trivially imported into WebHelpers
but that was a mistake. For WebHelpers 0.6-dev, you should put "from
routes import url_for" in helpers.py if you want h.url_for.

As for the magic transformation of "content" to
"/stylesheets/content.css", h.stylesheet_link_tag("content") does
that. It also calls url_for to get the application prefix, and
generates the <link> tag. But that also is deprecated in WebHelpers
0.6 as too magical. The 0.6 version is:

h.stylesheet_link(h.url_for("/stylesheets/content.css")) #
Imported from webhelpers.html.tags

--
Mike Orr <slugg...@gmail.com>

Reply all
Reply to author
Forward
0 new messages