Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Created March 18, 2012 20:03
Show Gist options
  • Save douglasmiranda/2080665 to your computer and use it in GitHub Desktop.
Save douglasmiranda/2080665 to your computer and use it in GitHub Desktop.
Django Filebrowser: Modifing the convert_filename function, to create a file name free of undesired chars.
def convert_filename(value):
"""
Convert Filename.
"""
def newname(text, encoding=None):
from unicodedata import normalize
if isinstance(text, str):
text = text.decode(encoding or 'ascii')
clean_text = text.strip().replace(' ', '-')
while '--' in clean_text:
clean_text = clean_text.replace('--', '-')
ascii_text = normalize('NFKD', clean_text).encode('ascii', 'ignore')
return ascii_text.lower()
if CONVERT_FILENAME:
return newname(value)
else:
return value
def convert_filename(value):
"""
Convert Filename.
"""
def newname(name, encoding=None):
from uuid import uuid4
return ''.join(str(uuid4()).split('-')) + '.' + name.split('.')[-1].lower()
if CONVERT_FILENAME:
return newname(value)
else:
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment