# Path: templation/settings.py
# DEBUG = getattr(settings, 'TEMPLATION_DEBUG',
#                 not bool(getattr(settings, 'DEBUG', True)))
#     DAV_ROOT = settings.TEMPLATION_DAV_ROOT
#     DAV_STATIC_URL = settings.TEMPLATION_DAV_STATIC_URL
#     RESOURCE_MODEL = settings.TEMPLATION_RESOURCE_MODEL
# PROVIDER_NAME = getattr(settings, 'TEMPLATION_PROVIDER_NAME', 'templation')
# BOILERPLATE_FOLDER = getattr(settings, 'TEMPLATION_BOILERPLATE_FOLDER', None)
# BOILERPLATE_INITIALIZER = getattr(settings,
#                                   'TEMPLATION_BOILERPLATE_INITIALIZER',
#                                   'templation.models.copy_boilerplate_folder')
# DUMP_EXCEPTIONS = getattr(settings, 'TEMPLATION_DUMP_EXCEPTION', (
#     'TemplateDoesNotExist',
#     'TemplateSyntaxError',
# ))
# SECRET_KEY = getattr(settings, 'TEMPLATION_SECRET_KEY', settings.SECRET_KEY)
# SANDBOX = getattr(settings, 'TEMPLATION_SANDBOX', False)
# DEFAULT_WHITELIST_TAGS = [
#     'comment', 'csrf_token', 'cycle', 'filter', 'firstof', 'for', 'if',
#     'ifchanged', 'now', 'regroup', 'spaceless', 'templatetag', 'url',
#     'widthratio', 'with', 'extends', 'include', 'block'
# ]
# WHITELIST_TAGS = getattr(settings, 'TEMPLATION_WHITELIST_TAGS',
#                          DEFAULT_WHITELIST_TAGS)
# DEFAULT_WHITELIST_FILTERS = [
#     'add', 'addslashes', 'capfirst', 'center', 'cut', 'date', 'default',
#     'default_if_none', 'dictsort', 'dictsortreversed', 'divisibleby', 'escape',
#     'escapejs', 'filesizeformat', 'first', 'fix_ampersands', 'floatformat',
#     'force_escape', 'get_digit', 'iriencode', 'join', 'last', 'length',
#     'length_is', 'linebreaks', 'linebreaksbr', 'linenumbers', 'ljust', 'lower',
#     'make_list', 'phone2numeric', 'pluralize', 'pprint', 'random',
#     'removetags', 'rjust', 'safe', 'safeseq', 'slice', 'slugify',
#     'stringformat', 'striptags', 'time', 'timesince', 'timeuntil', 'title',
#     'truncatewords', 'truncatewords_html', 'unordered_list', 'upper',
#     'urlencode', 'urlize', 'urlizetrunc', 'wordcount', 'wordwrap', 'yesno'
# ]
# WHITELIST_FILTERS = getattr(settings, 'TEMPLATION_WHITELIST_FILTERS',
#                             DEFAULT_WHITELIST_FILTERS)
# DEFAULT_EXTRA_LIBRARIES = [
#     'templation.templatetags.templation_tags',
# ]
# EXTRA_LIBRARIES = getattr(settings, 'TEMPLATION_EXTRA_LIBRARIES',
#                           DEFAULT_EXTRA_LIBRARIES)
# def import_from_path(import_string):
# 
# Path: templation/utils.py
# def will_override():
#     """
#     Test the necessary conditions for a template to be overwritten
# 
#     :returns: (bool, resource_access)
#     """
#     _access = get_resource_access_model()
#     try:
#         resource_access = _access.objects.filter(
#             resource_pointer__resource=thread_locals.resource)[:1][0]
#     except IndexError:
#         resource_access = None
#     ret_value = (
#         resource_access and (
#             resource_access.resource_pointer.is_validated or (
#                 resource_access.validate_access_token(thread_locals.token)
#             ) or (
#                 thread_locals.user.is_staff and _access.objects.filter(
#                     user=thread_locals.user,
#                     resource_pointer__resource=thread_locals.resource)
#             )
#         )
#     )
#     return (ret_value, resource_access)
# 
# def use_safe_templates(tags=None, filters=None, extra=None):
#     """
#     Cleans the builtin template libraries before running the function
#     (restoring the builtins afterwards).
# 
#     Removes any builtin tags and filters that are not enumerated in `tags` and
#     `filters`, and adds the extra library modules in `extra` to the builtins.
# 
#     Does not catch any template exceptions (notably, TemplateSyntaxError and
#     TemplateDoesNotExist may be raised).
#     """
#     def decorate(func):
#         @wraps(func)
#         def wrapped(*args, **kwargs):
#             # Clean out default libraries
#             # Note: template.builtins is merely a convenience import,
#             # we have to work with template.base.builtins for this to
#             # work right.
#             template.base.builtins, default_libs = [], template.base.builtins
#             try:
#                 # Construct new builtin with only whitelisted tags/filters
#                 whitelist = template.Library()
#                 for lib in default_libs:
#                     for tag in lib.tags:
#                         if tag in tags:
#                             whitelist.tags[tag] = lib.tags[tag]
#                     for filter in lib.filters:
#                         if filter in filters:
#                             whitelist.filters[filter] = lib.filters[filter]
# 
#                 # Install whitelist library and extras as builtins
#                 template.base.builtins.append(whitelist)
#                 [template.add_to_builtins(e) for e in extra]
# 
#                 return func(*args, **kwargs)
#             finally:
#                 # Restore the builtins to their former defaults
#                 template.base.builtins = default_libs
#         return wrapped
# 
#     # @use_safe_templates
#     func = tags
#     tags = WHITELIST_TAGS
#     filters = WHITELIST_FILTERS
#     extra = EXTRA_LIBRARIES
#     return decorate(func)


from django.template.base import Template
from django.template.loaders.app_directories import Loader
from .settings import get_resource_access_model, SANDBOX
from .utils import will_override, use_safe_templates
"""
Wrapper for loading templates from the filesystem.
"""

from __future__ import absolute_import


class TemplationLoader(Loader):
    def __init__(self):
        self._access = get_resource_access_model()

    def get_template_sources(self, template_name, template_dirs=None):
        """ Add the resource dir to the available dirs. """

# Path: templation/settings.py
# DEBUG = getattr(settings, 'TEMPLATION_DEBUG',
#                 not bool(getattr(settings, 'DEBUG', True)))
#     DAV_ROOT = settings.TEMPLATION_DAV_ROOT
#     DAV_STATIC_URL = settings.TEMPLATION_DAV_STATIC_URL
#     RESOURCE_MODEL = settings.TEMPLATION_RESOURCE_MODEL
# PROVIDER_NAME = getattr(settings, 'TEMPLATION_PROVIDER_NAME', 'templation')
# BOILERPLATE_FOLDER = getattr(settings, 'TEMPLATION_BOILERPLATE_FOLDER', None)
# BOILERPLATE_INITIALIZER = getattr(settings,
#                                   'TEMPLATION_BOILERPLATE_INITIALIZER',
#                                   'templation.models.copy_boilerplate_folder')
# DUMP_EXCEPTIONS = getattr(settings, 'TEMPLATION_DUMP_EXCEPTION', (
#     'TemplateDoesNotExist',
#     'TemplateSyntaxError',
# ))
# SECRET_KEY = getattr(settings, 'TEMPLATION_SECRET_KEY', settings.SECRET_KEY)
# SANDBOX = getattr(settings, 'TEMPLATION_SANDBOX', False)
# DEFAULT_WHITELIST_TAGS = [
#     'comment', 'csrf_token', 'cycle', 'filter', 'firstof', 'for', 'if',
#     'ifchanged', 'now', 'regroup', 'spaceless', 'templatetag', 'url',
#     'widthratio', 'with', 'extends', 'include', 'block'
# ]
# WHITELIST_TAGS = getattr(settings, 'TEMPLATION_WHITELIST_TAGS',
#                          DEFAULT_WHITELIST_TAGS)
# DEFAULT_WHITELIST_FILTERS = [
#     'add', 'addslashes', 'capfirst', 'center', 'cut', 'date', 'default',
#     'default_if_none', 'dictsort', 'dictsortreversed', 'divisibleby', 'escape',
#     'escapejs', 'filesizeformat', 'first', 'fix_ampersands', 'floatformat',
#     'force_escape', 'get_digit', 'iriencode', 'join', 'last', 'length',
#     'length_is', 'linebreaks', 'linebreaksbr', 'linenumbers', 'ljust', 'lower',
#     'make_list', 'phone2numeric', 'pluralize', 'pprint', 'random',
#     'removetags', 'rjust', 'safe', 'safeseq', 'slice', 'slugify',
#     'stringformat', 'striptags', 'time', 'timesince', 'timeuntil', 'title',
#     'truncatewords', 'truncatewords_html', 'unordered_list', 'upper',
#     'urlencode', 'urlize', 'urlizetrunc', 'wordcount', 'wordwrap', 'yesno'
# ]
# WHITELIST_FILTERS = getattr(settings, 'TEMPLATION_WHITELIST_FILTERS',
#                             DEFAULT_WHITELIST_FILTERS)
# DEFAULT_EXTRA_LIBRARIES = [
#     'templation.templatetags.templation_tags',
# ]
# EXTRA_LIBRARIES = getattr(settings, 'TEMPLATION_EXTRA_LIBRARIES',
#                           DEFAULT_EXTRA_LIBRARIES)
# def import_from_path(import_string):
# 
# Path: templation/utils.py
# def will_override():
#     """
#     Test the necessary conditions for a template to be overwritten
# 
#     :returns: (bool, resource_access)
#     """
#     _access = get_resource_access_model()
#     try:
#         resource_access = _access.objects.filter(
#             resource_pointer__resource=thread_locals.resource)[:1][0]
#     except IndexError:
#         resource_access = None
#     ret_value = (
#         resource_access and (
#             resource_access.resource_pointer.is_validated or (
#                 resource_access.validate_access_token(thread_locals.token)
#             ) or (
#                 thread_locals.user.is_staff and _access.objects.filter(
#                     user=thread_locals.user,
#                     resource_pointer__resource=thread_locals.resource)
#             )
#         )
#     )
#     return (ret_value, resource_access)
# 
# def use_safe_templates(tags=None, filters=None, extra=None):
#     """
#     Cleans the builtin template libraries before running the function
#     (restoring the builtins afterwards).
# 
#     Removes any builtin tags and filters that are not enumerated in `tags` and
#     `filters`, and adds the extra library modules in `extra` to the builtins.
# 
#     Does not catch any template exceptions (notably, TemplateSyntaxError and
#     TemplateDoesNotExist may be raised).
#     """
#     def decorate(func):
#         @wraps(func)
#         def wrapped(*args, **kwargs):
#             # Clean out default libraries
#             # Note: template.builtins is merely a convenience import,
#             # we have to work with template.base.builtins for this to
#             # work right.
#             template.base.builtins, default_libs = [], template.base.builtins
#             try:
#                 # Construct new builtin with only whitelisted tags/filters
#                 whitelist = template.Library()
#                 for lib in default_libs:
#                     for tag in lib.tags:
#                         if tag in tags:
#                             whitelist.tags[tag] = lib.tags[tag]
#                     for filter in lib.filters:
#                         if filter in filters:
#                             whitelist.filters[filter] = lib.filters[filter]
# 
#                 # Install whitelist library and extras as builtins
#                 template.base.builtins.append(whitelist)
#                 [template.add_to_builtins(e) for e in extra]
# 
#                 return func(*args, **kwargs)
#             finally:
#                 # Restore the builtins to their former defaults
#                 template.base.builtins = default_libs
#         return wrapped
# 
#     # @use_safe_templates
#     func = tags
#     tags = WHITELIST_TAGS
#     filters = WHITELIST_FILTERS
#     extra = EXTRA_LIBRARIES
#     return decorate(func)


# Path: templation/loaders.py
from django.template.base import Template
from django.template.loaders.app_directories import Loader
from .settings import get_resource_access_model, SANDBOX
from .utils import will_override, use_safe_templates

"""
Wrapper for loading templates from the filesystem.
"""

from __future__ import absolute_import


class TemplationLoader(Loader):
    def __init__(self):
        self._access = get_resource_access_model()

    def get_template_sources(self, template_name, template_dirs=None):
        """ Add the resource dir to the available dirs. """

        override, resource_access = will_override()