コード例 #1
0
ファイル: loader.py プロジェクト: schuyler1d/djangobars
def select_template(template_name_list):
    "Given a list of template names, returns the first that can be loaded."
    if not template_name_list:
        raise TemplateDoesNotExist("No template names provided")
    not_found = []
    for template_name in template_name_list:
        try:
            return get_template(template_name)
        except TemplateDoesNotExist as e:
            if e.args[0] not in not_found:
                not_found.append(e.args[0])
            continue
    # If we get here, none of the templates could be loaded
    raise TemplateDoesNotExist(', '.join(not_found))
コード例 #2
0
    def load_template(self, template_name, template_dirs=None):
        if hasattr(template_name, 'rsplit'):
            exclude_apps = getattr(settings, 'COFINGO_EXCLUDE_APPS', [
                'debug_toolbar',
                'admin'
            ])

            app = template_name.rsplit('/')[0]
            if app in exclude_apps:
                raise TemplateDoesNotExist(template_name)
        try:
            template = env.get_template(template_name)
            return template, template.filename
        except jinja2.TemplateNotFound:
            raise TemplateDoesNotExist(template_name)
コード例 #3
0
 def find_template(self, name, dirs=None):
     """
     Helper method. Lookup the template :param name: in all the configured loaders
     """
     key = self.cache_key(name, dirs)
     try:
         result = self.find_template_cache[key]
     except KeyError:
         result = None
         for loader in self.loaders:
             try:
                 template, display_name = loader(name, dirs)
             except TemplateDoesNotExist:
                 pass
             else:
                 origin = self.engine.make_origin(display_name, loader,
                                                  name, dirs)
                 result = template, origin
                 break
     self.find_template_cache[key] = result
     if result:
         return result
     else:
         self.template_cache[key] = TemplateDoesNotExist
         raise TemplateDoesNotExist(name)
コード例 #4
0
ファイル: loader.py プロジェクト: rjshaver/MarkupHiveSDK
 def load_template_source(self, template_name, template_dirs=None):
     for loader in self.loaders:
         try:
             return loader.load_template_source(template_name,template_dirs)
         except TemplateDoesNotExist:
             pass
     raise TemplateDoesNotExist(template_name)
コード例 #5
0
 def find_template(self, name, dirs=None):
     """
     RemovedInDjango21Warning: An internal method to lookup the template
     name in all the configured loaders.
     """
     key = self.cache_key(name, dirs)
     try:
         result = self.find_template_cache[key]
     except KeyError:
         result = None
         for loader in self.loaders:
             try:
                 template, display_name = loader(name, dirs)
             except TemplateDoesNotExist:
                 pass
             else:
                 origin = Origin(
                     name=display_name,
                     template_name=name,
                     loader=loader,
                 )
                 result = template, origin
                 break
     self.find_template_cache[key] = result
     if result:
         return result
     else:
         self.template_cache[key] = TemplateDoesNotExist
         raise TemplateDoesNotExist(name)
コード例 #6
0
    def get_template(self, template_name, template_dirs=None, skip=None):
        """
        Calls self.get_template_sources() and returns a Template object for
        the first template matching template_name. If skip is provided,
        template origins in skip are ignored. This is used to avoid recursion
        during template extending.
        """
        tried = []

        args = [template_name]
        # RemovedInDjango21Warning: Add template_dirs for compatibility with
        # old loaders
        if 'template_dirs' in getargspec(self.get_template_sources)[0]:
            args.append(template_dirs)

        for origin in self.get_template_sources(*args):
            if skip is not None and origin in skip:
                tried.append((origin, 'Skipped'))
                continue

            try:
                contents = self.get_contents(origin)
            except TemplateDoesNotExist:
                tried.append((origin, 'Source does not exist'))
                continue
            else:
                return Template(
                    contents,
                    origin,
                    origin.template_name,
                    self.engine,
                )

        raise TemplateDoesNotExist(template_name, tried=tried)
コード例 #7
0
 def load_template_source(self, template_name, template_dirs=None):
     if template_name == 'cms_mock_template.html':
         return '<div></div>', 'template.html'
     elif template_name == '404.html':
         return "404 Not Found", "404.html"
     else:
         raise TemplateDoesNotExist()
コード例 #8
0
 def get_contents(self, origin):
     try:
         with io.open(origin.name, encoding=self.engine.file_charset) as fp:
             return fp.read()
     except IOError as e:
         if e.errno == errno.ENOENT:
             raise TemplateDoesNotExist(origin)
         raise
コード例 #9
0
    def __init__(self, *args, **kwargs):
        super(MapEntityDocumentOdt, self).__init__(*args, **kwargs)

        suffix = suffix_for(self.template_name_suffix, "", "odt")
        self.template_name = smart_get_template(self.model, suffix)
        if not self.template_name:
            raise TemplateDoesNotExist(name_for(self.model._meta.app_label,
                                                self.model._meta.object_name.lower(), suffix))
コード例 #10
0
 def find_template(self, name, dirs=None):
     for loader in self.loaders:
         try:
             template, display_name = loader(name, dirs)
             return (template, make_origin(display_name, loader, name, dirs))
         except TemplateDoesNotExist:
             pass
     raise TemplateDoesNotExist(name)
コード例 #11
0
    def load_template_source(self, template_name, template_dirs=None):
        if ":" not in template_name:
            raise TemplateDoesNotExist()

        app_name, template_name = template_name.split(":", 1)
        try:
            app = get_app(app_name)
        except ImproperlyConfigured:
            raise TemplateDoesNotExist()
        else:
            app_dir = path.dirname(app.__file__)
            app_templ_dir = path.join(app_dir, 'templates')
            if not path.isdir(app_templ_dir):
                raise TemplateDoesNotExist()

            return FileSystemLoader.load_template_source(
                self, template_name, template_dirs=[app_templ_dir])
コード例 #12
0
def find_template(name, dirs=None):
    for loader in get_template_loaders():
        try:
            source, display_name = loader(name, dirs)
            return (source, make_origin(display_name, loader, name, dirs))
        except TemplateDoesNotExist:
            pass
    raise TemplateDoesNotExist(name)
コード例 #13
0
 def load_template_source(self, template_name, template_dirs=None):
     template_id = (self.chunk_model_name, template_name)
     try:
         chunk = self.chunk_model.objects.get(slug=template_name)
         return (chunk.content, "chunk:%s:%s" % template_id)
     except self.chunk_model.DoesNotExist:
         error_msg = "Couldn't find a %s-chunk named %s" % template_id
         raise TemplateDoesNotExist(error_msg)
コード例 #14
0
 def find_form(self, name):
     try:
         source, display_name = self._forms_loader(name, self._forms_dirs)
         origin = self.make_origin(display_name, self._forms_loader, name,
                                   self._forms_dirs)
         return source, origin
     except TemplateDoesNotExist:
         raise TemplateDoesNotExist(name)
コード例 #15
0
 def load_template_source(self, template_name, template_dirs=None):
     for path in self.get_template_sources(template_name):
         try:
             with io.open(path, encoding=settings.FILE_CHARSET) as file:
                 return (file.read(), path)
         except IOError:
             pass
     raise TemplateDoesNotExist(template_name)
コード例 #16
0
 def load_template_source(self, template_name, template_dirs=None):
     for filepath in self.get_template_sources(template_name, template_dirs):
         try:
             with open(filepath, 'rb') as fp:
                 return (fp.read().decode(settings.FILE_CHARSET), filepath)
         except IOError:
             pass
     raise TemplateDoesNotExist(template_name)
コード例 #17
0
ファイル: loader.py プロジェクト: tedjt/personalSite
def select_template(template_name_list):
    "Given a list of template names, returns the first that can be loaded."
    for template_name in template_name_list:
        try:
            return get_template(template_name)
        except TemplateDoesNotExist:
            continue
    # If we get here, none of the templates could be loaded
    raise TemplateDoesNotExist(', '.join(template_name_list))
コード例 #18
0
ファイル: template.py プロジェクト: yflau/django-uwsgi
 def load_template_source(self, template_name, template_dirs=None):
     filename = 'templates_' + self.symbolize(template_name)
     for app in settings.INSTALLED_APPS:
         try:
             symbol = '%s_%s' % (self.symbolize(app), filename)
             return (uwsgi.embedded_data(symbol), 'sym://%s' % symbol)
         except:
             pass
     raise TemplateDoesNotExist(template_name)
コード例 #19
0
 def load_template_source(self, template_name, template_dirs=None):
     for filepath in self.get_template_sources(template_name,
                                               template_dirs):
         try:
             with io.open(filepath,
                          encoding=self.engine.file_charset) as fp:
                 return fp.read(), filepath
         except IOError:
             pass
     raise TemplateDoesNotExist(template_name)
コード例 #20
0
    def load_template_source(self, template_name, template_dirs=None):
        # Only applies to things like view:project:show
        if ':' not in template_name or '/' in template_name:
            raise TemplateDoesNotExist(template_name)

        # Extract the current request from the thread
        request = utils.get_request()
        keys = '/'.join(template_name.split(':'))
        tpl_names = []
        # Use the theme from the request if we got ahold of the request
        if request and request.theme:
            tpl_names.append("themes/%s/%s.html" % (request.theme, keys))
        tpl_names.append("%s.html" % keys)
        for tpl_name in tpl_names:
            try:
                return self.find_template_source(tpl_name, template_dirs)
            except TemplateDoesNotExist:
                pass
        raise TemplateDoesNotExist(template_name)
コード例 #21
0
 def load_template_source(self, template_name, template_dirs=None):
     warnings.warn(
         'The load_template_sources() method is deprecated. Use '
         'get_template() or get_contents() instead.',
         RemovedInDjango21Warning,
     )
     try:
         return self.templates_dict[template_name], template_name
     except KeyError:
         raise TemplateDoesNotExist(template_name)
コード例 #22
0
    def get_contents(self, origin):
        try:
            source = resource_string(origin.app_name, origin.pkg_name)
        except:
            raise TemplateDoesNotExist(origin)

        if six.PY2:
            source = source.decode(self.engine.file_charset)

        return source
コード例 #23
0
ファイル: templateloader.py プロジェクト: sunlightlabs/tcamp
    def load_template(self, template_name, template_dirs=None):
        if isinstance(template_name, Template):
            return (template_name, '')
        # require that templates loaded via this loader start with 'pages:'
        if not template_name.startswith('pages:'):
            raise TemplateDoesNotExist(template_name)

        db_template_name = template_name.replace('pages:', '', 1)
        try:
            template = PageTemplate.objects.get(name__exact=db_template_name)
        except PageTemplate.DoesNotExist:
            raise TemplateDoesNotExist(template_name)

        if template.is_path:
            template = get_template(template.path)
        else:
            template = get_template_from_string(template.content)

        return (template, template_name)
コード例 #24
0
    def load_template(self, template_name, template_dirs=None):
        #~ source, origin = self.load_template_source(template_name, template_dirs)
        env = settings.SITE.plugins.jinja.renderer.jinja_env

        try:
            jt = env.get_template(template_name)
        except TemplateNotFound:
            raise TemplateDoesNotExist(template_name)
        template = DjangoJinjaTemplate(jt)
        return template, None
コード例 #25
0
    def __init__(self, *args, **kwargs):
        super(MapEntityDocumentWeasyprint, self).__init__(*args, **kwargs)

        suffix = suffix_for(self.template_name_suffix, "_pdf", "html")
        self.template_name = smart_get_template(self.model, suffix)
        if not self.template_name:
            raise TemplateDoesNotExist(name_for(self.model._meta.app_label,
                                                self.model._meta.object_name.lower(), suffix))
        self.template_attributes = smart_get_template(self.model, suffix_for(self.template_name_suffix,
                                                                             "_attributes", "html"))
        self.template_css = smart_get_template(self.model, suffix_for(self.template_name_suffix, "_pdf", "css"))
コード例 #26
0
 def load_template_source(self, template_name, template_dirs=None):
     for path in self.get_template_sources(template_name):
         try:
             file = open(path)
             try:
                 return (file.read().decode(settings.FILE_CHARSET), path)
             finally:
                 file.close()
         except IOError:
             pass
     raise TemplateDoesNotExist(template_name)
コード例 #27
0
 def load_template_source_inner(self, template_name, app, template_path):
     """
     Try to load 'template_path' in the templates directory of 'app'.
     """
     try:
         file_path = self.get_app_template_path(app, template_path)
         with open(file_path, 'rb') as fp:
             template = fp.read().decode(settings.FILE_CHARSET)
             return (template, 'app_namespace:%s:%s' % (app, file_path))
     except (IOError, KeyError, ValueError):
         raise TemplateDoesNotExist(template_name)
コード例 #28
0
 def load_template_source(self, template_name, template_dirs=None):
     warnings.warn(
         'The load_template_sources() method is deprecated. Use '
         'get_template() or get_contents() instead.',
         RemovedInDjango21Warning,
     )
     for origin in self.get_template_sources(template_name, template_dirs):
         try:
             return self.get_contents(origin), origin.name
         except TemplateDoesNotExist:
             pass
     raise TemplateDoesNotExist(template_name)
コード例 #29
0
ファイル: loader.py プロジェクト: dchang00/keekaa-back-end
def select_template(template_name_list):
    "Given a list of template names, returns the first that can be loaded."
    if not template_name_list:
        raise TemplateDoesNotExist("No template names provided")
    not_found = []
    for template_name in template_name_list:
        try:
            return get_template(template_name)
        except TemplateDoesNotExist, e:
            if e.args[0] not in not_found:
                not_found.append(e.args[0])
            continue
コード例 #30
0
 def load_template_source(self, template_name, template_dirs=None):
     tried = []
     for filepath in self.get_template_sources(template_name, template_dirs):
         try:
             with io.open(filepath, encoding=settings.FILE_CHARSET) as fp:
                 return fp.read(), filepath
         except IOError:
             tried.append(filepath)
     if tried:
         error_msg = "Tried %s" % tried
     else:
         error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory."
     raise TemplateDoesNotExist(error_msg)