src/p4l/templateloaders.py
changeset 145 7c6fe1dab213
equal deleted inserted replaced
144:8c32ea1310de 145:7c6fe1dab213
       
     1 # -*- coding: utf-8 -*-
       
     2 #
       
     3 # Copyright IRI (c) 2013
       
     4 #
       
     5 # contact@iri.centrepompidou.fr
       
     6 #
       
     7 # This software is governed by the CeCILL-B license under French law and
       
     8 # abiding by the rules of distribution of free software.  You can  use, 
       
     9 # modify and/ or redistribute the software under the terms of the CeCILL-B
       
    10 # license as circulated by CEA, CNRS and INRIA at the following URL
       
    11 # "http://www.cecill.info". 
       
    12 #
       
    13 # As a counterpart to the access to the source code and  rights to copy,
       
    14 # modify and redistribute granted by the license, users are provided only
       
    15 # with a limited warranty  and the software's author,  the holder of the
       
    16 # economic rights,  and the successive licensors  have only  limited
       
    17 # liability. 
       
    18 #
       
    19 # In this respect, the user's attention is drawn to the risks associated
       
    20 # with loading,  using,  modifying and/or developing or reproducing the
       
    21 # software by the user in light of its specific status of free software,
       
    22 # that may mean  that it is complicated to manipulate,  and  that  also
       
    23 # therefore means  that it is reserved for developers  and  experienced
       
    24 # professionals having in-depth computer knowledge. Users are therefore
       
    25 # encouraged to load and test the software's suitability as regards their
       
    26 # requirements in conditions enabling the security of their systems and/or 
       
    27 # data to be ensured and,  more generally, to use and operate it in the 
       
    28 # same conditions as regards security. 
       
    29 #
       
    30 # The fact that you are presently reading this means that you have had
       
    31 # knowledge of the CeCILL-B license and that you accept its terms.
       
    32 #
       
    33 
       
    34 '''
       
    35 Created on Oct 9, 2013
       
    36 
       
    37 From http://djangosnippets.org/snippets/1376/
       
    38 
       
    39 @author: ymh
       
    40 '''
       
    41 from os.path import dirname, join, abspath, isdir
       
    42 
       
    43 from django.core.exceptions import ImproperlyConfigured
       
    44 from django.db.models import get_app
       
    45 from django.template import TemplateDoesNotExist
       
    46 from django.template.loaders.filesystem import Loader as FilesystemLoader
       
    47 
       
    48 
       
    49 def get_template_vars(template_name):
       
    50     app_name, template_name = template_name.split(":", 1)
       
    51     try:
       
    52         template_dir = abspath(join(dirname(get_app(app_name).__file__), 'templates'))
       
    53     except ImproperlyConfigured:
       
    54         raise TemplateDoesNotExist()
       
    55     
       
    56     return template_name, template_dir
       
    57 
       
    58 class Loader(FilesystemLoader):
       
    59     
       
    60     is_usable = True
       
    61     
       
    62     def get_template_sources(self, template_name, template_dirs=None):
       
    63         if ":" not in template_name:
       
    64             raise TemplateDoesNotExist()
       
    65         template_name, template_dir = get_template_vars(template_name)
       
    66         
       
    67         if not isdir(template_dir):
       
    68             raise TemplateDoesNotExist()
       
    69 
       
    70         return [join(template_dir, template_name)]
       
    71