Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Made filename generator configurable #22

Merged
merged 3 commits into from

2 participants

@girasquid

We wanted our filenames to use something other than the timestamp.

@peterbe

Can you please change this to a getattr instead of the big try:except block. Reason being is that something else might raise an AttributeError by mistake and it gets confused with the AttributeError of fetching settings.DJANGO_STATIC_FILENAME_GENERATOR.

Please replace the try:except with:

  filename_generator = getattr(settings, 'DJANGO_STATIC_FILENAME_GENERATOR', None)
  if filename_generator:
      return filename_generator

   def default_filename_generator(apart, new_m_time):
        ...
   return default_filename_generator

"default" is better than "old" because that's what it is. It's only "old" to you :)

@peterbe
Owner

I can't merge this until you provide a unit test that checks it. If you need help writing one I'm more than happy to help out. Let me know.

Owner

By the way, I forgot to say; I love the idea!
Also, it would really help me accept this if you add a note in the README about being able to set DJANGO_STATIC_FILENAME_GENERATOR

Will do.

@girasquid

I'm a bit lost on how to write a test for this - can you help me out with that?

@peterbe
Owner

Sorry for late reply. Been busy. I'll try to see if I can send a pull request your way towards this when I find time. My september is being butchered by lots of other commitments.

@peterbe peterbe merged commit 070029a into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 4, 2011
  1. @girasquid
Commits on Oct 21, 2011
  1. @girasquid
  2. @girasquid

    Updated README

    girasquid authored
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 3 deletions.
  1. +18 −0 README.md
  2. +15 −3 django_static/templatetags/django_static.py
View
18 README.md
@@ -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
---------------------------
View
18 django_static/templatetags/django_static.py
@@ -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)
@@ -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)
Something went wrong with that request. Please try again.