lemoncurry/lemonshort/short_url.py

17 lines
498 B
Python

from django.apps import apps
from django.conf import settings
from typing import Any, Dict, Type
from .convert import AbcIdConverter
prefixes = {} # type: Dict[Type[Any], str]
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)] + AbcIdConverter().to_url(entity.id)