slug filename

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import os

def slug_filename(path):
    """ Returns a callable that can set upload_to to 'path/slug.ext'
        i is a (model) instance (with a field called 'slug'), f is a filename.

        >>> upload_to = slug_filename(u"/my/path/")
        >>> test_model_instance = type("",(),dict(slug=u"slugy"))()
        >>> upload_to(test_model_instance, u"myfilename.jpg")
        u"/my/path/slugy.jpg"
    """
    return lambda i,f: os.path.join(path, u''.join((i.slug, os.path.splitext(f)[1])))

More like this

  1. Unique naming for file uploads by mindcruzer 9 months ago
  2. Use the primary key in FileField and ImageField filenames by exogen 4 years, 8 months ago
  3. ImageWithThumbsField by zenx 4 years, 5 months ago
  4. Convert CamelCase to lowercase_with_underscores by jdriscoll 5 years, 4 months ago
  5. Dynamic thumbnail generator by semente 5 years, 3 months ago

Comments

(Forgotten your password?)