Replace links to static in problems with links to fingerprinted data

This commit is contained in:
Calen Pennington
2012-04-23 11:42:42 -04:00
parent 5a4d9691d4
commit e8c4d1dbef
6 changed files with 28 additions and 6 deletions

19
lib/static_replace.py Normal file
View File

@@ -0,0 +1,19 @@
from staticfiles.storage import staticfiles_storage
import re
PREFIX = '/static/'
STATIC_PATTERN = re.compile(r"""
(?P<quote>['"]) # 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)

View File

@@ -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}

View File

@@ -23,8 +23,10 @@
</script>
<%block name="headextra"/>
<!-- This must appear after all mathjax-config blocks, so it is after the imports from the other templates -->
<script type="text/javascript" src="${static.url('js/mathjax-MathJax-c9db6ac/MathJax.js')}?config=TeX-AMS_HTML-full"></script>
<!-- This must appear after all mathjax-config blocks, so it is after the imports from the other templates.
It can't be run through static.url because MathJax uses crazy url introspection to do lazy loading of
MathJax extension libraries -->
<script type="text/javascript" src="/static/js/mathjax-MathJax-c9db6ac/MathJax.js?config=TeX-AMS_HTML-full"></script>
</head>
<body class="<%block name="bodyclass"/>">

View File

@@ -1,3 +1,4 @@
<%namespace name='static' file='static_content.html'/>
<h2 class="problem-header">${ problem['name'] }
% if problem['weight']:
: ${ problem['weight'] } points
@@ -5,7 +6,7 @@
</h2>
<section class="problem">
${ problem['html'] }
${ static.replace_urls(problem['html']) }
<section class="action">
<input type="hidden" name="problem_id" value="${ problem['name'] }">

View File

@@ -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>
<%def name='css(group)'>${compressed_css(group)}</%def>
<%def name='js(group)'>${compressed_js(group)}</%def>
<%def name='replace_urls(text)'>${replace_urls(text)}</%def>

View File

@@ -80,5 +80,3 @@ urlpatterns = patterns(*urlpatterns)
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()