Web M&M
https://mam.matfyz.cz
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.4 KiB
83 lines
2.4 KiB
from django import views as DjangoViews
|
|
|
|
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?
|
|
|
|
# Pozor: zarovnáno velmi netradičně pro přehlednost
|
|
roman_numerals = zip((1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1),
|
|
('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'))
|
|
|
|
|
|
def roman(num):
|
|
res = ""
|
|
for i, n in roman_numerals:
|
|
res += n * (num // i)
|
|
num %= i
|
|
return res
|
|
|
|
|
|
def from_roman(rom):
|
|
if not rom:
|
|
return 0
|
|
for i, n in roman_numerals:
|
|
if rom.upper().startswith(n):
|
|
return i + from_roman(rom[len(n):])
|
|
raise Exception('Invalid roman numeral: "%s"', rom)
|
|
|
|
|
|
def viewMethodSwitch(get, post):
|
|
"""
|
|
Vrátí view, který zavolá různé jiné views podle toho, kterou metodou je zavolán.
|
|
|
|
Inspirováno https://docs.djangoproject.com/en/3.1/topics/class-based-views/mixins/#an-alternative-better-solution, jen jsem to udělal genericky.
|
|
|
|
Parametry:
|
|
post view pro metodu POST
|
|
get view pro metodu GET
|
|
|
|
V obou případech se míní už view jakožto funkce, takže u class-based views se už má použít .as_view()
|
|
|
|
TODO: Podpora i pro metodu HEAD? A možná i pro FILES?
|
|
"""
|
|
|
|
theGetView = get
|
|
thePostView = post
|
|
|
|
class NewView(DjangoViews.View):
|
|
def get(self, request, *args, **kwargs):
|
|
return theGetView(request, *args, **kwargs)
|
|
def post(self, request, *args, **kwargs):
|
|
return thePostView(request, *args, **kwargs)
|
|
|
|
return NewView.as_view()
|
|
|