2018-03-21 06:50:40 -04:00
|
|
|
from django.apps import apps
|
|
|
|
from django.conf import settings
|
2018-05-28 19:37:28 -04:00
|
|
|
from typing import Any, Dict, Type
|
2018-03-21 06:50:40 -04:00
|
|
|
|
|
|
|
from .convert import id_to_abc
|
|
|
|
|
2018-05-28 19:37:28 -04:00
|
|
|
prefixes = {} # type: Dict[Type[Any], str]
|
2018-03-21 06:50:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
def short_url(entity):
|
|
|
|
if not prefixes:
|
|
|
|
for k, m in settings.SHORTEN_MODELS.items():
|
|
|
|
prefixes[apps.get_model(m)] = k
|
|
|
|
base = '/'
|
|
|
|
if hasattr(settings, 'SHORT_BASE_URL'):
|
|
|
|
base = settings.SHORT_BASE_URL
|
|
|
|
return base + prefixes[type(entity)] + id_to_abc(entity.id)
|