"""
Compatibility layer for Django 1.7 and 1.8.

Django 1.8 introduced support for multiple templating engines, and
Ogmios needs to check each of the engines for the desired template.

In Django 1.7 Ogmios needs to check the loaders for the only engine
Django supports.
"""

from distutils.version import StrictVersion

import django
from django.template import Template, Context, TemplateDoesNotExist

def get_template_source_from_loader(template_loader, instance):
    """
    Return the source for a file from the template loader.
    """
    try:
        # This should work for templates that are not the cached template loader
        return template_loader.load_template_source(instance.filename)[0]
    except NotImplementedError:
        # This might be the cached template loader. Its load_template_source()
        # method is not implemented

        for temp in template_loader.loaders:
            try:
                return temp.load_template_source(instance.filename)[0]
            except TemplateDoesNotExist:
                # There may be many wrapped loaders.
                # We need to get through the whole list before
                # throwing an exception.
                pass

    raise TemplateDoesNotExist(instance.filename)

def get_template_source_from_engine(engine, instance):
    """
    Attempt to find a specific template in the given engine.
    """
    for template_loader in engine.engine.template_loaders:
        try:
            return get_template_source_from_loader(template_loader, instance)
        except TemplateDoesNotExist:
            # Go through all the template loaders before throwing
            # an exception.
            pass

    raise TemplateDoesNotExist(instance.filename)

def get_template_source_from_engine_module(engines, instance):
    """
    Check all configured engines for a specific template.
    """
    if instance.template_loader is None:
        engine_list = engines.all()
    else:
        engine_list = [engines[instance.template_loader]]

    for engine in engine_list:
        try:
            return get_template_source_from_engine(engine, instance)
        except TemplateDoesNotExist:
            # Go through all the template engines before raising
            # an exception
            pass

    raise TemplateDoesNotExist(instance.filename)

def get_template_source_from_loader_module(loader_module, instance):
    try:
        loader_module.find_template('') # Just to preload template_source_loader_module
    except TemplateDoesNotExist:
        pass

    for loader in loader_module.template_source_loaders:
        try:
            return get_template_source_from_loader(loader, instance)
        except TemplateDoesNotExist:
            pass

    raise TemplateDoesNotExist(instance.filename)

try:
    # Engines was introduced in Django 1.8
    from django.template import engines
    def get_template_source(instance):
        return get_template_source_from_engine_module(engines, instance)
except ImportError:
    # We have Django 1.7
    from django.template import loader as loader_module
    def get_template_source(instance):
        return get_template_source_from_loader_module(loader_module, instance)

@@ -1,271 +1,59 @@
 %22%22%22%0A
-Compatibility layer for Django 1.7 and 1.8.%0A%0ADjango 1.8 introduced support for multiple templating engines, and%0AOgmios needs to check each of the engines for the desired template.%0A%0AIn Django 1.7 Ogmios needs to check the loaders for the only engine%0ADjango support
+Module for finding templates from configured loader
 s.%0A%22
@@ -182,16 +182,25 @@
 NotExist
+, engines
 %0A%0Adef ge
@@ -1557,37 +1557,9 @@
 urce
-_from_engine_module(engines, 
+(
 inst
@@ -2099,882 +2099,4 @@
 me)%0A
-%0Adef get_template_source_from_loader_module(loader_module, instance):%0A    try:%0A        loader_module.find_template('') # Just to preload template_source_loader_module%0A    except TemplateDoesNotExist:%0A        pass%0A%0A    for loader in loader_module.template_source_loaders:%0A        try:%0A            return get_template_source_from_loader(loader, instance)%0A        except TemplateDoesNotExist:%0A            pass%0A%0A    raise TemplateDoesNotExist(instance.filename)%0A%0Atry:%0A    # Engines was introduced in Django 1.8%0A    from django.template import engines%0A    def get_template_source(instance):%0A        return get_template_source_from_engine_module(engines, instance)%0Aexcept ImportError:%0A    # We have Django 1.7%0A    from django.template import loader as loader_module%0A    def get_template_source(instance):%0A        return get_template_source_from_loader_module(loader_module, instance)%0A

Drop support for Django 1.7