Add type hints to loader

This commit is contained in:
blag 2025-06-11 21:41:52 -06:00
parent f57ca2f769
commit 4e51b34b53
No known key found for this signature in database

View file

@ -1,3 +1,5 @@
from typing import Optional, Tuple
from django.contrib.sites.models import Site
from django.db import router
from django.template import Origin, TemplateDoesNotExist
@ -31,17 +33,17 @@ class Loader(BaseLoader):
loader=self,
)
def get_contents(self, origin):
def get_contents(self, origin: Origin) -> str:
content, _ = self._load_template_source(origin.template_name)
return content
def _load_and_store_template(self, template_name, cache_key, site, **params):
def _load_and_store_template(self, template_name: str, cache_key: str, site: Site, **params) -> Tuple[str, str]:
template = Template.objects.get(name__exact=template_name, **params)
db = router.db_for_read(Template, instance=template)
display_name = f"dbtemplates:{db}:{template_name}:{site.domain}"
return set_and_return(cache_key, template.content, display_name)
def _load_template_source(self, template_name, template_dirs=None):
def _load_template_source(self, template_name: str, template_dirs: Optional[str] = None) -> Tuple[str, str]:
# The logic should work like this:
# * Try to find the template in the cache. If found, return it.
# * Now check the cache if a lookup for the given template