import unicodedata


def slugify(value, replace_spaces=False):
    """
    Normalizes string, converts to lowercase, removes non-alpha and "foreign"
    characters, removes forbidden characters, and converts spaces to underscores.
    Used for file and folder names.
    """
    replaced = ('/', '\\', ':', '$', '&', '!', '*', '~', '`', '"', '+', '>', '<', '?', '|', '¿', '~', '¡')
    for r in replaced:
      value = value.replace(r, '_')
    if replace_spaces:
      value = value.replace(' ', '_')
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
    return value.strip()