import unicodedata
import re


def sanitize_filename(filename):
    """
    Adapted from Django's slugify functions.

    :param filename: The filename.
    """
    try:
        filename = filename.decode()
    except AttributeError:
        pass

    value = unicodedata.normalize('NFKD', filename).encode(
        'ascii', 'ignore').decode('ascii')
    # In constrast to django we allow dots and don't lowercase.
    value = re.sub(r'[^\w\.\s-]', '', value).strip()
    return re.sub(r'[-\s]+', '-', value)