straight55b / app-engine-patch

Automatically exported from code.google.com/p/app-engine-patch
0 stars 0 forks source link

media_url template tag #112

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Here is an implementation of a media_url template tag which handles the
case when you are running on your own domain, on the live server, and you
want your media to be served from the appspot domain (which allows the
browser to open more parallel connections, resulting in faster download).

Using a template tag rather than a variable is also more flexible, as in
the future you could for example transform the whole url into the md5 of
the content.

Here's the code. To enable it, add:

  CANONICAL_DOMAIN = 'www.example.com'

to settings.py, and use:

  {% media_url "foo/bar.gif" %}

in templates.

from django.template import Library, TextNode, TemplateSyntaxError
from django.conf import settings
from appenginepatcher import on_production_server
import os

register = Library()

APPLICATION_ID = os.environ.get('APPLICATION_ID')
APPSPOT_LIVE_DOMAIN = '%s.appspot.com' % APPLICATION_ID

try:
    CANONICAL_DOMAIN = settings.CANONICAL_DOMAIN
except:
    CANONICAL_DOMAIN = APPSPOT_LIVE_DOMAIN

@register.tag(name='media_url')
def compile_media_url(parser, token):
    try:
        tag_name, path = token.split_contents()
    except ValueError:
        raise TemplateSyntaxError("%r tag requires 1 argument" % tag_name)
    if not path.startswith('"') and not path.startswith("'"):
        raise TemplateSyntaxError("%r tag requires a string argument" %
tag_name)
    path = path[1:-1]

    if on_production_server and CANONICAL_DOMAIN != APPSPOT_LIVE_DOMAIN:
        url = 'http://' + APPSPOT_LIVE_DOMAIN + settings.MEDIA_URL
    else:
        url = settings.MEDIA_URL

    return TextNode(url + path.lstrip('/'))

Original issue reported on code.google.com by sdeasey on 21 Mar 2009 at 12:51

GoogleCodeExporter commented 9 years ago
Actually, you can achieve the same by setting your MEDIA_URL in your settings
depending on the value of on_production_server. Also, I'm not so happy about 
using
"appspot.com" (we use a different subdomain instead).

BTW, instead of using @register.tag you should try @register.simple_tag. That's 
much
easier.

Original comment by wkornew...@gmail.com on 22 Mar 2009 at 12:23

GoogleCodeExporter commented 9 years ago
> Also, I'm not so happy about using "appspot.com" (we use a different subdomain
instead).

You map the same app-engine application to two separate apps-for-your-domain 
domains?
I never thought of that, as the appspot domain isn't really visible for style 
sheets
etc. But that's a fair use case.

It isn't as easy as as just using on_production_server in settings.py to set
MEDIA_URL though. For example, if you access /login using https on the appspot
domain, then the page will generate warnings because the media is being served 
via a
non-ssl domain.

Similarly, if you access your app via it's staging URL at 
appid.version.appspot.com
you will get stale media from static.example.com, which will be an alias for
appid.appspot.com.

The media_url tag handles this.

> BTW, instead of using @register.tag you should try @register.simple_tag. 
That's
much easier.

They aren't quite the same thing.  @simple_tag does it's processing during the 
render
phase, whereas the media_url tag does it's processing during the compile phase 
and
generates a TextNode, which has the simplest possible render phase.

This doesn't make much difference at the moment for app-engine-patch as it is 
not
caching templates, but it should. Take a look at
google/appengine/ext/webapp/template.py for an example.  (This was going to be 
my
next patch, if your interested in adding this)

Original comment by sdeasey on 22 Mar 2009 at 1:33

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
> It isn't as easy as as just using on_production_server in settings.py to set
MEDIA_URL though. For example, if you access /login using https on the appspot
domain, then the page will generate warnings because the media is being served 
via a
non-ssl domain.

If your app runs on an appspot domain you don't need to change MEDIA_URL, 
anyway. ;)

Also, it's easy to set "http://..." for on_production_server.

The only real problem is indeed with staging URLs, but even your current 
media_url
implementation would act incorrectly (using stale version from 
appid.appspot.com).
You'd have to detect whether the app is accessed via a versioned appspot.com 
domain
and handle that case appropriately. But since MEDIA_URL also has to be 
available at
the JavaScript level (site_data.settings.MEDIA_URL) this can't work magically 
with a
media_url tag, anyway. You just have to set MEDIA_URL in your settings. At 
least, I
can't promote media_url if it's not compatible with the media generator's 
site_data
feature.

Regarding template caching, you should send the code to Django. It's definitely 
a
cool feature and a shame that Django doesn't have it.

Original comment by wkornew...@gmail.com on 22 Mar 2009 at 3:55