lemoncurry/lemonshort/short_url.py

18 lines
498 B
Python
Raw Permalink Normal View History

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
2023-08-10 02:52:37 -04:00
base = "/"
if hasattr(settings, "SHORT_BASE_URL"):
base = settings.SHORT_BASE_URL
return base + prefixes[type(entity)] + AbcIdConverter().to_url(entity.id)