Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made filename generator configurable #22

Merged
merged 3 commits into from
Sep 20, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,24 @@ like this:

return on_my_cdn.get(uri, uri)

Advanced configuration with DJANGO_STATIC_FILENAME_GENERATOR
----------------------------------------------------

By default, django-static generates filenames for your combined files
using timestamps. You can use your own filename generating function
by setting it in settings, like so:

DJANGO_STATIC_FILENAME_GENERATOR = 'myapp.filename_generator'

This is expected to be the equivalent of this import statement:

from myapp import filename_generator

Where `myapp` is a python module, and `filename_generator` is a regular
python function. Here's the skeleton for that function:

def filename_generator(file_parts, new_m_time):
return ''.join([file_parts[0], '.%s' % new_m_time, file_parts[1]])

Compression Filters
---------------------------
Expand Down
18 changes: 15 additions & 3 deletions django_static/templatetags/django_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ def file_proxy_nothing(uri, *args, **kwargs):
return file_proxy_nothing
file_proxy = _load_file_proxy()

def _load_filename_generator():
filename_generator = getattr(settings, 'DJANGO_STATIC_FILENAME_GENERATOR', None)
if filename_generator:
from django.utils.importlib import import_module
_module_name, _function_name = filename_generator.rsplit('.', 1)
file_generator_module = import_module(_module_name)
return getattr(file_generator_module, _function_name)
def default_filename_generator(apart, new_m_time):
new_filename = ''.join([apart[0], '.%s' % new_m_time, apart[1]])
return new_filename
return default_filename_generator

_generate_filename = _load_filename_generator()

# this defines what keyword arguments you can always expect to get from in the
# file proxy function you've defined.
fp_default_kwargs = dict(new=False, changed=False, checked=False, notfound=False)
Expand Down Expand Up @@ -469,9 +483,7 @@ def wrap_up(filename):
if not m_time:
# We did not have the filename in the map OR it has changed
apart = os.path.splitext(filename)
new_filename = ''.join([apart[0],
'.%s' % new_m_time,
apart[1]])
new_filename = _generate_filename(apart, new_m_time)
fileinfo = (settings.DJANGO_STATIC_NAME_PREFIX + new_filename,
new_m_time)

Expand Down