Created
March 18, 2012 20:03
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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