From e8c4d1dbefac49f9d08e705c555f63eea8772edd Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Mon, 23 Apr 2012 11:42:42 -0400 Subject: [PATCH] Replace links to static in problems with links to fingerprinted data --- lib/static_replace.py | 19 +++++++++++++++++++ rakefile | 2 +- templates/main.html | 6 ++++-- templates/problem.html | 3 ++- templates/static_content.html | 2 ++ urls.py | 2 -- 6 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 lib/static_replace.py diff --git a/lib/static_replace.py b/lib/static_replace.py new file mode 100644 index 0000000000..d820990740 --- /dev/null +++ b/lib/static_replace.py @@ -0,0 +1,19 @@ +from staticfiles.storage import staticfiles_storage +import re + +PREFIX = '/static/' +STATIC_PATTERN = re.compile(r""" +(?P['"]) # the opening quotes +{prefix} # the prefix +.*? # everything else in the url +(?P=quote) # the first matching closing quote +""".format(prefix=PREFIX), re.VERBOSE) +PREFIX_LEN = len(PREFIX) + +def replace(static_url): + quote = static_url[0] + url = staticfiles_storage.url(static_url[1+PREFIX_LEN:-1]) + return "".join([quote, url, quote]) + +def replace_urls(text): + return STATIC_PATTERN.sub(replace, text) diff --git a/rakefile b/rakefile index fa2b3e946c..fcfe49776a 100644 --- a/rakefile +++ b/rakefile @@ -40,7 +40,7 @@ task :package do set -x chown -R makeitso:makeitso #{INSTALL_DIR_PATH} - + service gunicorn stop rm -f #{LINK_PATH} ln -s #{INSTALL_DIR_PATH} #{LINK_PATH} diff --git a/templates/main.html b/templates/main.html index bf80160580..10532e601e 100644 --- a/templates/main.html +++ b/templates/main.html @@ -23,8 +23,10 @@ <%block name="headextra"/> - - + + "> diff --git a/templates/problem.html b/templates/problem.html index cd332f1b74..750fe7f0cc 100644 --- a/templates/problem.html +++ b/templates/problem.html @@ -1,3 +1,4 @@ +<%namespace name='static' file='static_content.html'/>

${ problem['name'] } % if problem['weight']: : ${ problem['weight'] } points @@ -5,7 +6,7 @@

- ${ problem['html'] } + ${ static.replace_urls(problem['html']) }
diff --git a/templates/static_content.html b/templates/static_content.html index cdb005d24e..00c2c4dff4 100644 --- a/templates/static_content.html +++ b/templates/static_content.html @@ -1,8 +1,10 @@ <%! from staticfiles.storage import staticfiles_storage from pipeline_mako import compressed_css, compressed_js +from static_replace import replace_urls %> <%def name='url(file)'>${staticfiles_storage.url(file)} <%def name='css(group)'>${compressed_css(group)} <%def name='js(group)'>${compressed_js(group)} +<%def name='replace_urls(text)'>${replace_urls(text)} diff --git a/urls.py b/urls.py index 329d500ea0..279fd75231 100644 --- a/urls.py +++ b/urls.py @@ -80,5 +80,3 @@ urlpatterns = patterns(*urlpatterns) if settings.DEBUG: urlpatterns += staticfiles_urlpatterns() - -