From 52662150470140cdea9bf1166265fb36ff4cd7c1 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 27 Oct 2010 11:20:22 -0500 Subject: Add tag for inserting CDN prefix on media URLs This tag will just be placed before any media reference that is available on the CDN; if the CDN is enabled and CDN_PATH/CDN_PATH_SECURE are defined the correct URL will be generated. Signed-off-by: Dan McGee --- main/templatetags/cdn.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'main') diff --git a/main/templatetags/cdn.py b/main/templatetags/cdn.py index 96181c1..43a7f4b 100644 --- a/main/templatetags/cdn.py +++ b/main/templatetags/cdn.py @@ -9,8 +9,7 @@ def jquery(parser, token): class JQueryNode(template.Node): def render(self, context): - # if we have the request in context, we can check if it is secure and - # serve content from HTTPS instead. + # serve content from HTTPS if we know this request is secure secure = 'secure' in context and context['secure'] prefixes = { False: 'http', True: 'https' } version = '1.4.3' @@ -22,4 +21,21 @@ class JQueryNode(template.Node): jquery = '/media/jquery-%s.min.js' % version return '' % jquery +@register.tag +def cdnprefix(parser, token): + return CDNPrefixNode() + +class CDNPrefixNode(template.Node): + def render(self, context): + oncdn = getattr(settings, 'CDN_ENABLED', True) + if not oncdn: + return '' + secure = 'secure' in context and context['secure'] + # if left undefined, same behavior as if CDN is turned off + paths = { + False: getattr(settings, 'CDN_PATH', ''), + True: getattr(settings, 'CDN_PATH_SECURE', ''), + } + return paths[secure] + # vim: set ts=4 sw=4 et: -- cgit v1.2.3-24-g4f1b