learningequality/ka-lite — pull request #2075 by cpauya (2014-05-12 08:26:39+00:00)
Title: Feature/1161 staticfiles after update

This is in reference to #1161 - "Static files not properly reloaded by all browsers after update".

**Summary of Updates**

* Create a `static` template tag to append the BUILD/SHA256 hash as Get parameter to the static file's url.
* Change the `BUILD_ID` context variable implementation into use of the `static` template tag.
* Refactor templates that were using the `BUILD_ID` context variable above.
* Some minor fixes on affected templates re incorrect html tags.

**The extended `static` template tag.**

I created a template tag at `distributed/templatetags/kalite_staticfiles.py`. I'm not sure about it's location but just followed the app where the context processor was from. Was having second thoughts to put it at `python-packages/fle_utils/django_utils/templatetags/` as @bcipolli suggested.

This generally appends a BUILD/SHA256 hash as Get parameter to the file's url.

This calls the `static` template tag of the `staticfiles` app so we don't have to modify templates that were already using the `static` tag.

It uses the "BUILD_ID" code by @jamalex from the `distributed/custom_context_processors.py` (which I already removed in this changeset) as Option 1.

It will attempt to use the SHA256 hash of the file's contents (as Option 2) - but falls back to Option 1 if this doesn't work for some reason.

**To Use**

1. Load or replace `staticfiles` with `distributed/templatetags/kalite_staticfiles.py`.
2. Use the `static` template tag. Pass a second argument as string if you want it to append the BUILD/SHA256 hash into the static file's url.

Example:

...
{% load kalite_staticfiles %}
...

...

Output:

[12/May/2014 16:20:55] "GET /static/css/distributed/khan-lite.css?8ec19612553d2008ea71d1b9001cc64d6b3e49c39a3df37cb280584904b9c2ea HTTP/1.1" 200 9449

or

[12/May/2014 16:21:36] "GET /static/css/distributed/khan-lite.css?8ec19612553d2008ea71d1b9001cc64d6b3e49c39a3df37cb280584904b9c2ea HTTP/1.1" 304 0

To test it, I monitor the Terminal for Django server's status codes:
* 200 - OK, media was served
* 304 - Not Modified, media from cache

I have only tested this on these browsers:

* Google Chrome v34.0.x.x
* Mozilla Firefox v28.0
* Safari v7.0.3

References:

* [What are the best practices for versioning CSS and JS files?](https://www.quora.com/What-are-the-best-practices-for-versioning-CSS-and-JS-files)
* [Generating a MD5 checksum of a file?](http://stackoverflow.com/questions/3431825/generating-a-md5-checksum-of-a-file)