53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
import os
|
|
from unidecode import unidecode # Používám pro získání ID odkazu
|
|
|
|
from django.utils.text import get_valid_filename
|
|
from django.utils import timezone
|
|
|
|
|
|
def aux_generate_filename(_, filename):
|
|
"""Pomocná funkce generující ošetřený název souboru v adresáři s datem"""
|
|
clean = get_valid_filename(
|
|
unidecode(filename.replace('/', '-').replace('\0', ''))
|
|
)
|
|
datedir = timezone.now().strftime('%Y-%m')
|
|
fname = "{}/{}".format(
|
|
timezone.now().strftime('%Y-%m-%d-%H:%M'),
|
|
clean)
|
|
return os.path.join(datedir, fname)
|
|
|
|
|
|
|
|
bez_diakritiky = ({}
|
|
# FIXME: funguje jen pro český a slovenský text, jinak jsou špatně
|
|
# transliterace. Potenciální řešení:
|
|
# https://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-normalize-in-a-python-unicode-string
|
|
# (ale přidává to další závislosti…)
|
|
|
|
# Tisknutelné ASCII
|
|
| {chr(a): chr(a) for a in range(32, 126+1)}
|
|
|
|
# České, slovenské a blízké diakritiky a divnoznaky
|
|
| { x: 'a' for x in 'áÁäÄ'}
|
|
| { x: 'c' for x in 'čČ'}
|
|
| { x: 'd' for x in 'ďĎ'}
|
|
| { x: 'e' for x in 'éÉěĚëË'}
|
|
| { x: 'i' for x in 'íÍ'}
|
|
| { x: 'l' for x in 'ľĽĺĹ'}
|
|
| { x: 'n' for x in 'ňŇ'}
|
|
| { x: 'o' for x in 'óÓöÖôÔ'}
|
|
| { x: 'r' for x in 'řŘŕŔ'}
|
|
| { x: 's' for x in 'šŠßẞ'}
|
|
| { x: 't' for x in 'ťŤ'}
|
|
| { x: 'u' for x in 'úÚůŮ'}
|
|
| { x: 'y' for x in 'ýÝ'}
|
|
| { x: 'z' for x in 'žŽ'}
|
|
)
|
|
|
|
# Tabulka pro str.translate
|
|
class _bez_diakritiky_translate:
|
|
def __getitem__(self, it):
|
|
return ord(bez_diakritiky.get(chr(it), None))
|
|
bez_diakritiky_translate = _bez_diakritiky_translate()
|
|
|
|
# TODO: testy?
|