2005-10-15 06:22:12 +08:00
|
|
|
# Wrapper for loading templates from eggs via pkg_resources.resource_string.
|
2012-08-14 18:09:45 +08:00
|
|
|
from __future__ import unicode_literals
|
2005-10-15 06:22:12 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
from pkg_resources import resource_string
|
|
|
|
except ImportError:
|
|
|
|
resource_string = None
|
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2012-08-14 18:09:45 +08:00
|
|
|
from django.conf import settings
|
2010-11-27 13:47:30 +08:00
|
|
|
from django.template.base import TemplateDoesNotExist
|
2012-08-14 18:09:45 +08:00
|
|
|
from django.utils import six
|
2005-10-15 06:22:12 +08:00
|
|
|
|
2014-11-16 01:35:02 +08:00
|
|
|
from .base import Loader as BaseLoader
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2009-12-14 20:08:23 +08:00
|
|
|
class Loader(BaseLoader):
|
|
|
|
is_usable = resource_string is not None
|
|
|
|
|
|
|
|
def load_template_source(self, template_name, template_dirs=None):
|
|
|
|
"""
|
|
|
|
Loads templates from Python eggs via pkg_resource.resource_string.
|
|
|
|
|
|
|
|
For every installed app, it tries to get the resource (app, template_name).
|
|
|
|
"""
|
|
|
|
if resource_string is not None:
|
|
|
|
pkg_name = 'templates/' + template_name
|
2013-12-24 19:25:17 +08:00
|
|
|
for app_config in apps.get_app_configs():
|
2009-12-14 20:08:23 +08:00
|
|
|
try:
|
2013-12-19 22:57:23 +08:00
|
|
|
resource = resource_string(app_config.name, pkg_name)
|
2012-08-14 18:09:45 +08:00
|
|
|
except Exception:
|
|
|
|
continue
|
2013-09-02 18:06:32 +08:00
|
|
|
if six.PY2:
|
2012-08-14 18:09:45 +08:00
|
|
|
resource = resource.decode(settings.FILE_CHARSET)
|
2013-12-19 22:57:23 +08:00
|
|
|
return (resource, 'egg:%s:%s' % (app_config.name, pkg_name))
|
2010-01-11 02:36:20 +08:00
|
|
|
raise TemplateDoesNotExist(template_name)
|