Ejemplo n.º 1
0
	def __init__(self, engine):
		from django.template.loaders import app_directories
		super(JinjaLoader, self).__init__(tuple(engine.dirs) + app_directories.get_app_template_dirs('templates'))
		from django.conf import settings
		auto_reload = {t['BACKEND']: t for t in settings.TEMPLATES}['template_dynamicloader.backend.Jinja2']["OPTIONS"].get('auto_reload', False)
		cache_enable = not auto_reload
		self.template_source_cache = {} if cache_enable else None
Ejemplo n.º 2
0
def get_django_template_dirs(loader_list=None):
    """Build a list of template directories based on configured loaders.
    """
    if not loader_list:
        try:
            from django.template import engines
        except ImportError:
            pass
        else:
            # Django >=1.8
            return uniq(
                sum((list(engines[e].template_dirs) for e in engines), []))

        # Django <1.8
        loader_list = settings.TEMPLATE_LOADERS

    template_dirs = []
    for loader in loader_list:
        if loader in FILESYSTEM_LOADERS:
            template_dirs.extend(settings.TEMPLATE_DIRS)
        if loader in APPDIR_LOADERS:
            from django.template.loaders import app_directories
            if hasattr(app_directories, 'app_template_dirs'):
                template_dirs.extend(app_directories.app_template_dirs)
            elif hasattr(app_directories, 'get_app_template_dirs'):
                template_dirs.extend(
                    app_directories.get_app_template_dirs('templates'))
        if isinstance(loader, (list, tuple)) and len(loader) >= 2:
            # The cached loader uses the tuple syntax, but simply search all
            # tuples for nested loaders; thus possibly support custom ones too.
            template_dirs.extend(get_django_template_dirs(loader[1]))

    return uniq(template_dirs)
Ejemplo n.º 3
0
    def handle(self, **options):
        self.verbosity = 2
        self.storage = staticfiles_storage
        extensions = options.get('extensions') or ['html']
        self.symlinks = options.get('symlinks')
        self.post_process = options['post_process']
        # self.post_processed_files = []

        self.extensions = handle_extensions(extensions)

        template_dirs = list(get_app_template_dirs('templates'))
        for config in settings.TEMPLATES:
            # only support vanilla Django templates
            if config['BACKEND'] != 'django.template.backends.django.DjangoTemplates':
                continue
            template_dirs += list(config['DIRS'])

        # find all template files

        all_files = []
        for template_dir in template_dirs:
            for dirpath, dirnames, filenames in os.walk(template_dir, topdown=True, followlinks=self.symlinks):
                for filename in filenames:
                    filepath = os.path.join(dirpath, filename)
                    file_ext = os.path.splitext(filename)[1]
                    if file_ext not in self.extensions:
                        continue
                    all_files.append(filepath)

        all_apps = []
        for fp in all_files:
            with io.open(fp, 'r', encoding=settings.FILE_CHARSET) as template_file:
                src_data = template_file.read()

            for t in Lexer(src_data, None).tokenize():
                if t.token_type == TOKEN_BLOCK:
                    imatch = SYSTEMJS_TAG_RE.match(t.contents)
                    if imatch:
                        all_apps.append(imatch.group('app'))

        bundled_files = OrderedDict()
        storage = FileSystemStorage(settings.STATIC_ROOT, base_url=settings.STATIC_URL)
        for app in all_apps:
            rel_path = System.bundle(app, force=True)
            bundled_files[rel_path] = (storage, rel_path)

        if self.post_process and hasattr(self.storage, 'post_process'):
            processor = self.storage.post_process(bundled_files, dry_run=False)
            for original_path, processed_path, processed in processor:
                if isinstance(processed, Exception):  # pragma: no cover
                    self.stderr.write("Post-processing '%s' failed!" % original_path)
                    # Add a blank line before the traceback, otherwise it's
                    # too easy to miss the relevant part of the error message.
                    self.stderr.write("")
                    raise processed
                if processed:  # pragma: no cover
                    self.log("Post-processed '%s' as '%s'" %
                             (original_path, processed_path), level=1)
                else:
                    self.log("Skipped post-processing '%s'" % original_path)  # pragma: no cover
Ejemplo n.º 4
0
def get_template_dirs():
    template_dirs = [
        d for engines in settings.TEMPLATES for d in engines.get("DIRS", [])
    ]
    template_app_dirs = get_app_template_dirs("templates")
    template_dirs += template_app_dirs
    return template_dirs
Ejemplo n.º 5
0
def get_django_template_dirs(loader_list=None):
    """Build a list of template directories based on configured loaders.
    """
    if not loader_list:
        try:
            from django.template.utils import EngineHandler
        except ImportError:
            pass
        else:
            # Django >=1.8
            engines = EngineHandler()
            return uniq(sum((list(engines[e].template_dirs) for e in engines), []))

        # Django <1.8
        loader_list = settings.TEMPLATE_LOADERS

    template_dirs = []
    for loader in loader_list:
        if loader in FILESYSTEM_LOADERS:
            template_dirs.extend(settings.TEMPLATE_DIRS)
        if loader in APPDIR_LOADERS:
            from django.template.loaders import app_directories
            if hasattr(app_directories, 'app_template_dirs'):
                template_dirs.extend(app_directories.app_template_dirs)
            elif hasattr(app_directories, 'get_app_template_dirs'):
                template_dirs.extend(app_directories.get_app_template_dirs('templates'))
        if isinstance(loader, (list, tuple)) and len(loader) >= 2:
            # The cached loader uses the tuple syntax, but simply search all
            # tuples for nested loaders; thus possibly support custom ones too.
            template_dirs.extend(get_django_template_dirs(loader[1]))

    return uniq(template_dirs)
    def get_template_sources(self, template_name, template_dirs=None):
        """This will append the site-prefix to every application template directory.  If
        SITE_ROOT is defined in settings it will scope to your application.  This is to enable
        the file finder to work correctly"""

        site_id = settings.SITE_ID
        site_root = getattr(settings, 'SITE_ROOT')
        site_prefixes = settings.SITE_FOLDERS.get(site_id, [])

        if not self.logged:
            msg = "Sources Site ID: %d  Site Root: %s Site-Prefixes: %r Template Name: %s Template Dirs: %r"
            log.debug(msg, site_id, site_root, site_prefixes, template_name, template_dirs)
            self.logged = True

        if not isinstance(site_prefixes, (list, tuple)):
            site_prefixes = [site_prefixes]

        _app_template_dirs = []
        if not template_dirs:
            try:
                # Post Django 1.8
                template_dirs = app_directories.get_app_template_dirs('templates')
            except AttributeError:
                # Pre Django 1.8
                template_dirs = app_directories.app_template_dirs

        for app_dir in template_dirs:
            if site_root and os.path.abspath(site_root) not in os.path.abspath(app_dir):
                continue
            for site_prefix in site_prefixes:
                _app_template_dirs.append(safe_join(app_dir, site_prefix))

        _app_template_dirs = tuple(_app_template_dirs) if len(_app_template_dirs) else None
        return super(AgileSiteAppDirectoriesFinder, self).get_template_sources(template_name, _app_template_dirs)
Ejemplo n.º 7
0
    def test_compress_offline(self):
        template_dir_list = []
        for template_dir in get_app_template_dirs('templates'):
            if settings.BASE_DIR in template_dir:
                template_dir_list.append(template_dir)

        template_list = []
        for template_dir in template_dir_list:
            for base_dir, dirnames, filenames in os.walk(template_dir):
                for filename in filenames:
                    template_list.append(os.path.join(base_dir, filename))

        # Filter lines that are not html and strip whitespace
        filenames = [name for name in [name.strip() for name in template_list] if name.endswith('.html')]

        for filename in filenames:
            with open(filename, 'r') as f:
                in_compress_block = False
                for line in f.readlines():
                    has_start_tag = COMPRESS_JS in line or COMPRESS_CSS in line
                    has_end_tag = ENDCOMPRESS in line

                    if has_start_tag and not has_end_tag:
                        in_compress_block = True
                        continue

                    if has_end_tag:
                        in_compress_block = False

                    if in_compress_block:
                        self._assert_valid_import(line, filename)
Ejemplo n.º 8
0
    def test_compress_offline(self):
        template_dir_list = []
        for template_dir in get_app_template_dirs('templates'):
            if settings.BASE_DIR in template_dir:
                template_dir_list.append(template_dir)

        template_list = []
        for template_dir in template_dir_list:
            for base_dir, dirnames, filenames in os.walk(template_dir):
                for filename in filenames:
                    template_list.append(os.path.join(base_dir, filename))

        # Filter lines that are not html and strip whitespace
        filenames = [
            name for name in [name.strip() for name in template_list]
            if name.endswith('.html')
        ]

        for filename in filenames:
            with open(filename, 'r') as f:
                in_compress_block = False
                for line in f.readlines():
                    has_start_tag = COMPRESS_JS in line or COMPRESS_CSS in line
                    has_end_tag = ENDCOMPRESS in line

                    if has_start_tag and not has_end_tag:
                        in_compress_block = True
                        continue

                    if has_end_tag:
                        in_compress_block = False

                    if in_compress_block:
                        self._assert_valid_import(line, filename)
Ejemplo n.º 9
0
def index(request):
    ctx = get_context(request)
    cname = os.environ["PORTAL_CNAME"]
    template_dir = get_app_template_dirs("templates/notebooks")[0]
    htmls = os.path.join(template_dir, cname, "*.html")
    ctx["notebooks"] = [
        p.split("/" + cname + "/")[-1].replace(".html", "")
        for p in glob(htmls)
    ]
    ctx["PORTAL_CNAME"] = cname
    ctx["landing_pages"] = []
    mask = ["project", "title", "authors", "is_public", "description", "urls"]
    client = load_client(
        headers=get_consumer(request))  # sets/returns global variable
    entries = client.projects.get_entries(_fields=mask).result()["data"]
    for entry in entries:
        authors = entry["authors"].strip().split(",", 1)
        if len(authors) > 1:
            authors[1] = authors[1].strip()
        entry["authors"] = authors
        entry["description"] = entry["description"].split(".", 1)[0] + "."
        ctx["landing_pages"].append(
            entry
        )  # visibility governed by is_public flag and X-Consumer-Groups header
    return render(request, "home.html", ctx.flatten())
Ejemplo n.º 10
0
def _ensure_setup() -> None:
    """
    Ensure templates are populated

    :return: None
    """
    # pylint: disable=global-statement
    global templates

    if templates:
        return

    # Collect all mail templates
    templates = defaultdict(lambda: defaultdict(list))
    regex = re.compile(
        r'.*/mail/(?P<name>.*)/(?P<locale>[a-z]+)\.(?P<subtype>[a-z]+)')
    for template_dir in (get_app_template_dirs("templates")):
        for directory, _, filenames in os.walk(template_dir):
            for filename in filenames:
                match = regex.match("{}/{}".format(directory, filename))
                if match:
                    d = match.groupdict()
                    templates[d['name']][d['locale']].append({
                        **d,
                        'file':
                        os.path.join(directory, filename),
                    })

    templates = {k: dict(v) for k, v in templates.items()}
Ejemplo n.º 11
0
def get_flatpage_templates():
    flatpages_dirs = (os.path.join(path, "flatpages") for path in chain(
        get_template_dirs(), get_app_template_dirs('templates'))
                      if os.path.exists(os.path.join(path, "flatpages")))

    return sorted(
        set(tmp for path in flatpages_dirs for tmp in os.listdir(path)
            if tmp.endswith('.html')))
    def find_template(self, name, context, peeking=False):
        """
        Replacement for Django's ``find_template`` that uses the current
        template context to keep track of which template directories it
        has used when finding a template. This allows multiple templates
        with the same relative name/path to be discovered, so that
        circular template inheritance can occur.
        """

        # These imports want settings, which aren't available when this
        # module is imported to ``add_to_builtins``, so do them here.
        import django.template.loaders.app_directories as app_directories

        from mezzanine.conf import settings

        # Store a dictionary in the template context mapping template
        # names to the lists of template directories available to
        # search for that template. Each time a template is loaded, its
        # origin directory is removed from its directories list.
        context_name = "OVEREXTENDS_DIRS"
        if context_name not in context:
            context[context_name] = {}
        if name not in context[context_name]:
            all_dirs = list(
                chain.from_iterable([
                    template_engine.get("DIRS", [])
                    for template_engine in settings.TEMPLATES
                ])) + list(app_directories.get_app_template_dirs("templates"))
            # os.path.abspath is needed under uWSGI, and also ensures we
            # have consistent path separators across different OSes.
            context[context_name][name] = list(map(os.path.abspath, all_dirs))

        # Build a list of template loaders to use. For loaders that wrap
        # other loaders like the ``cached`` template loader, unwind its
        # internal loaders and add those instead.
        loaders = []
        for loader in context.template.engine.template_loaders:
            loaders.extend(getattr(loader, "loaders", [loader]))

        # Go through the loaders and try to find the template. When
        # found, removed its absolute path from the context dict so
        # that it won't be used again when the same relative name/path
        # is requested.
        for loader in loaders:
            dirs = context[context_name][name]
            try:
                source, path = loader.load_template_source(name, dirs)
            except TemplateDoesNotExist:
                pass
            else:
                # Only remove the absolute path for the initial call in
                # get_parent, and not when we're peeking during the
                # second call.
                if not peeking:
                    remove_path = os.path.abspath(path[:-len(name) - 1])
                    context[context_name][name].remove(remove_path)
                return Template(source)
        raise TemplateDoesNotExist(name)
Ejemplo n.º 13
0
    def find_template(self, name, context, peeking=False):
        """
        Replacement for Django's ``find_template`` that uses the current
        template context to keep track of which template directories it
        has used when finding a template. This allows multiple templates
        with the same relative name/path to be discovered, so that
        circular template inheritance can occur.
        """

        # These imports want settings, which aren't available when this
        # module is imported to ``add_to_builtins``, so do them here.
        import django.template.loaders.app_directories as app_directories

        from mezzanine.conf import settings

        # Store a dictionary in the template context mapping template
        # names to the lists of template directories available to
        # search for that template. Each time a template is loaded, its
        # origin directory is removed from its directories list.
        context_name = "OVEREXTENDS_DIRS"
        if context_name not in context:
            context[context_name] = {}
        if name not in context[context_name]:
            all_dirs = (
                list(chain.from_iterable(
                    [template_engine.get('DIRS', [])
                     for template_engine in settings.TEMPLATES])) +
                list(app_directories.get_app_template_dirs('templates')))
            # os.path.abspath is needed under uWSGI, and also ensures we
            # have consistent path separators across different OSes.
            context[context_name][name] = list(map(os.path.abspath, all_dirs))

        # Build a list of template loaders to use. For loaders that wrap
        # other loaders like the ``cached`` template loader, unwind its
        # internal loaders and add those instead.
        loaders = []
        for loader in context.template.engine.template_loaders:
            loaders.extend(getattr(loader, "loaders", [loader]))

        # Go through the loaders and try to find the template. When
        # found, removed its absolute path from the context dict so
        # that it won't be used again when the same relative name/path
        # is requested.
        for loader in loaders:
            dirs = context[context_name][name]
            try:
                source, path = loader.load_template_source(name, dirs)
            except TemplateDoesNotExist:
                pass
            else:
                # Only remove the absolute path for the initial call in
                # get_parent, and not when we're peeking during the
                # second call.
                if not peeking:
                    remove_path = os.path.abspath(path[:-len(name) - 1])
                    context[context_name][name].remove(remove_path)
                return Template(source)
        raise TemplateDoesNotExist(name)
Ejemplo n.º 14
0
def work(request):
    ctx = get_context(request)
    template_dir = get_app_template_dirs("templates/notebooks")[0]
    htmls = os.path.join(template_dir, ctx["PORTAL_CNAME"], "*.html")
    ctx["notebooks"] = [
        p.split("/" + ctx["PORTAL_CNAME"] + "/")[-1].replace(".html", "")
        for p in glob(htmls)
    ]
    return render(request, "work.html", ctx.flatten())
Ejemplo n.º 15
0
 def _find_templates(location, add_to):
     """Helper method for shared search-templates-add-to-dict
     functionality"""
     search_path = os.path.join('templates', 'regulations', location)
     file_names = {file_name
                   for template_dir in get_app_template_dirs(search_path)
                   for file_name in os.listdir(template_dir)
                   if os.path.isfile(os.path.join(template_dir, file_name))}
     for file_name in file_names:
         ident, _ = os.path.splitext(file_name)
         add_to[ident] = 'regulations/{}/{}'.format(location, file_name)
Ejemplo n.º 16
0
def _app_template_dirs():
    from django.template.loaders import app_directories
    if hasattr( app_directories, 'app_template_dirs' ):
        # pre 1.8
        return app_directories.app_template_dirs
    elif hasattr( app_directories, 'get_app_template_dirs' ):
        # django 1.8
        return app_directories.get_app_template_dirs('templates')
    else:
        # django 1.9
        return app_directories.calculate_app_template_dirs()
Ejemplo n.º 17
0
def get_flatpage_templates():
    flatpages_dirs = (
        os.path.join(path, "flatpages")
        for path in chain(get_template_dirs(), get_app_template_dirs('templates'))
        if os.path.exists(os.path.join(path, "flatpages"))
    )

    return sorted(set(
        tmp
        for path in flatpages_dirs for tmp in os.listdir(path)
        if tmp.endswith('.html')
    ))
Ejemplo n.º 18
0
def template_wildcard(*args):
    """Return a list of all templates in dir"""
    templates = []
    for tmpl_dir in args:
        for app_templates in get_app_template_dirs('templates'):
            path = os.path.join(app_templates, tmpl_dir)
            if os.path.isdir(path):
                files = sorted(glob.glob(path + '*.html'))
                for file in files:
                    templates.append(os.path.relpath(file,
                                                     start=app_templates))
    return templates
Ejemplo n.º 19
0
 def _find_templates(location, add_to):
     """Helper method for shared search-templates-add-to-dict
     functionality"""
     search_path = os.path.join('templates', 'regulations', location)
     file_names = {
         file_name
         for template_dir in get_app_template_dirs(search_path)
         for file_name in os.listdir(template_dir)
         if os.path.isfile(os.path.join(template_dir, file_name))
     }
     for file_name in file_names:
         ident, _ = os.path.splitext(file_name)
         add_to[ident] = 'regulations/{}/{}'.format(location, file_name)
Ejemplo n.º 20
0
def get_templates_in_dir(dir_suffix):
    template_dirs = []
    for app_template_dir in app_directories.get_app_template_dirs('templates'):
        for root, _, _ in os.walk(app_template_dir):
            if root.endswith(dir_suffix):
                template_dirs.append(root)

    for template_dir in template_dirs:
        for root, _, files in os.walk(template_dir):
            public_files = [f for f in files if not f.startswith('.')]

            for f in sorted(public_files):
                yield os.path.join(root, f)
Ejemplo n.º 21
0
def story(label, story_dir):
    ctx = {'label': label}

    template_files = []
    for template_dir in (settings.TEMPLATE_DIRS + get_app_template_dirs('')):
        story_path = os.path.join(template_dir, story_dir)
        for dir, dirnames, filenames in os.walk(story_path):
            for filename in filenames:
                if filename.endswith('~'):
                    continue
                template_files.append(os.path.join(story_dir, filename))

    ctx['stories'] = template_files
    return ctx
def _template_names(proj_only=False):
    for engine in settings.TEMPLATES:
        if (engine['BACKEND'] ==
                'django.template.backends.django.DjangoTemplates'):
            for template_dir in chain(engine['DIRS'],
                                      get_app_template_dirs('templates')):
                if not proj_only or template_dir.startswith(settings.BASE_DIR):
                    for dirpath, dirnames, filenames in os.walk(template_dir):
                        for template_name in filenames:
                            yield os.path.join(dirpath[len(template_dir) + 1:],
                                               template_name)
        else:
            raise NotImplementedError(
                "Currently only supports default DjangoTemplates backend.")
Ejemplo n.º 23
0
 def handle(self, *args, **options):
     return_code = 0
     for template_dir in get_app_template_dirs('templates'):
         if template_dir.startswith(settings.BASE_DIR):
             for template_file in glob('%s/**/*html' % template_dir,
                                       recursive=True):
                 short_path = template_file.replace(settings.BASE_DIR, '')
                 try:
                     tmpl = Template(template_file)
                     tmpl.render(Context())
                     LOGGER.info('%s passed successfully!', short_path)
                 except TemplateSyntaxError as exc:
                     LOGGER.error('%s errored: %s', short_path, exc)
                     return_code = 1
     return return_code
Ejemplo n.º 24
0
 def get_templates(self):
     """
     Returns a list of templates (.html files) on every app
     """
     app_dir = settings.APPLICATION_DIR
     for template_dir in get_app_template_dirs('templates'):
         if not self.options[
                 'include-packages'] and not template_dir.startswith(
                     app_dir):  # noqa
             continue
         for base_dir, dirnames, filenames in os.walk(template_dir):
             for filename in filenames:
                 if filename.endswith(".html"):
                     yield (os.path.join(base_dir,
                                         filename).split('templates/')[-1])
Ejemplo n.º 25
0
def get_routes_from_templates():
    template_dir_list = []
    template_list = []

    for template_dir in get_app_template_dirs('templates'):
        if settings.ROOT_DIR in template_dir:
            template_dir_list.append(template_dir)

    for template_dir in template_dir_list:
        for base_dir, dirnames, filenames in os.walk(template_dir):
            if 'templates/styleguide' in base_dir:
                for filename in filenames:
                    template_list.append(filename.replace('.html', ''))

    return template_list
Ejemplo n.º 26
0
 def handle(self, *args, **options):
     template_dirs = get_app_template_dirs('templates')
     for template_dir in template_dirs:
         for dirname, dirnames, filenames in os.walk(template_dir):
             for filename in filenames:
                 if 'mail' in filename:
                     file_path = os.path.join(dirname, filename)
                     mail_template_name = file_path.split('templates/')[-1]
                     if os.path.exists(file_path):
                         MailTemplate.objects.get_or_create(
                             name=mail_template_name,
                             defaults={
                                 'html_template': open(file_path,
                                                       'r').read()
                             })
Ejemplo n.º 27
0
def story(label, story_dir):
    ctx = {
        'label': label
    }

    template_files = []
    for template_dir in (settings.TEMPLATE_DIRS + get_app_template_dirs('')):
        story_path = os.path.join(template_dir, story_dir)
        for dir, dirnames, filenames in os.walk(story_path):
            for filename in filenames:
                if filename.endswith('~'):
                    continue
                template_files.append(os.path.join(story_dir, filename))

    ctx['stories'] = template_files
    return ctx
Ejemplo n.º 28
0
 def handle(self, *args, **options):
     for template_dir in get_app_template_dirs('templates/sfc'):
         widgets = []
         for dir_, dirs, filenames in os.walk(template_dir):
             root, _, relative = dir_.partition('templates')
             tag_library = relative.lstrip(os.path.sep)
             tag_library_file = "_".join(tag_library.split(
                 os.path.sep)) + '.py'
             for filename in filenames:
                 widget_name = filename.replace('.html', '')
                 tpl = os.path.join(tag_library, filename)
                 widgets.append(dict(tpl=tpl, name=widget_name))
                 print(f"{widget_name} registered in {tag_library_file}")
             tag_dir = os.path.join(root, 'templatetags', tag_library_file)
             if not widgets:
                 continue
             with open(tag_dir, 'w+') as file:
                 print(f"{tag_library_file} written to {tag_dir}")
                 file.write(template.render(Context(dict(widgets=widgets))))
Ejemplo n.º 29
0
    def discover_templates(self):
        template_dirs = list(get_app_template_dirs('templates'))
        for config in settings.TEMPLATES:
            # only support vanilla Django templates
            if config['BACKEND'] != 'django.template.backends.django.DjangoTemplates':
                continue
            template_dirs += list(config['DIRS'])

        all_files = []
        for template_dir in template_dirs:
            for dirpath, dirnames, filenames in os.walk(template_dir, topdown=True, followlinks=self.symlinks):
                for filename in filenames:
                    filepath = os.path.join(dirpath, filename)
                    file_ext = os.path.splitext(filename)[1]
                    if file_ext not in self.extensions:
                        continue
                    template_name = os.path.relpath(filepath, template_dir)
                    all_files.append((template_name, filepath))

        return all_files
Ejemplo n.º 30
0
    def handle(self, *args, **options):
        directories = set([dir_ for dir_ in get_app_template_dirs('templates')])

        if len(settings.TEMPLATES) > 0:
            if 'DIRS' in settings.TEMPLATES[0].keys():
                direectories = directories.union(set(
                    settings.TEMPLATES[0]['DIRS']))

        extensions = self.default_extensions

        if hasattr(settings, 'ANUBIS_JS_EXTENSIONS'):
            extensions = settings.ANUBIS_JS_EXTENSIONS

        jsx_files = [os.path.join(dir_, fname)
                     for dir_ in directories
                     for _, __, fnames in os.walk(dir_)
                     for fname in fnames
                     if os.path.splitext(fname)[1] in extensions]

        working_path = os.path.join(*[os.path.dirname(anubis.__file__),
                                      "frontend"])

        node_path = os.path.join(working_path, "node_modules")

        presets = ",".join(self.presets)
        plugins = ",".join(self.plugins)
        options = "-s inline" if settings.DEBUG else "--minified"

        for source in jsx_files:
            root, _ = os.path.splitext(source)
            target = root + ".js"

            self.stdout.write("{} -> {}".format(source, target))

            shell('pwd', cwd=working_path)

            shell(("./node_modules/.bin/babel -o {} --presets {} --plugins {} "
                   "--compact true {} {}").format(target, presets, plugins,
                                                  options, source),
                  cwd=working_path,
                  env=dict(os.environ, NODE_PATH=node_path))
Ejemplo n.º 31
0
    def __init__(self):
        self.templates = []

        # Load all templates
        template_dir_list = []
        for template_dir in get_app_template_dirs('templates'):
            if settings.BASE_DIR in template_dir:
                template_dir_list.append(template_dir)

        for template_dir in (template_dir_list +
                             settings.TEMPLATES[0]['DIRS']):
            for base_dir, subdir, filenames in os.walk(template_dir):
                for filename in filenames:
                    path = os.path.join(base_dir, filename)
                    relative_path = path[len(template_dir) + 1:]
                    key = hashlib.md5(path.encode()).hexdigest()
                    template = Template(key=key,
                                        path=path,
                                        relative_path=relative_path,
                                        filename=filename)
                    self.add(template)
Ejemplo n.º 32
0
def story(label, story_dir):
    ctx = {
        'label': label
    }

    template_files = []
    if settings.TEMPLATES[0]['APP_DIRS']:
        directories = get_app_template_dirs('')
    else:
        directories = settings.TEMPLATES[0]['DIRS']

    for template_dir in directories:
        story_path = os.path.join(template_dir, 'templates', story_dir)
        for dir, dirnames, filenames in os.walk(story_path):
            for filename in filenames:
                if filename.endswith('~'):
                    continue
                template_files.append(os.path.join(story_dir, filename))

    ctx['stories'] = template_files
    return ctx
Ejemplo n.º 33
0
def get_templates():
    templates = []
    dirs_to_scan = []

    for dir in get_app_template_dirs('templates'):
        dirs = [os.path.join(dir, name) for name in os.listdir(dir) if os.path.isdir(os.path.join(dir, name)) and name == 'djangocms_easy_gallery']
        if dirs:
            dirs_to_scan.extend(dirs)

    for dir in settings.TEMPLATES[0]['DIRS']:
        dirs = [os.path.join(dir, name) for name in os.listdir(dir) if os.path.isdir(os.path.join(dir, name)) and name == 'djangocms_easy_gallery']
        if dirs:
            dirs_to_scan.extend(dirs)
                
    for dir in dirs_to_scan:
        files = glob.glob(os.path.join(dir, '*.html'))
        for file in files:
            dir, file = os.path.split(file)
            templates.append((file, file))

    return templates
Ejemplo n.º 34
0
    def discover_templates(self):
        template_dirs = list(get_app_template_dirs('templates'))
        for config in settings.TEMPLATES:
            # only support vanilla Django templates
            if config[
                    'BACKEND'] != 'django.template.backends.django.DjangoTemplates':
                continue
            template_dirs += list(config['DIRS'])

        all_files = []
        for template_dir in template_dirs:
            for dirpath, dirnames, filenames in os.walk(
                    template_dir, topdown=True, followlinks=self.symlinks):
                for filename in filenames:
                    filepath = os.path.join(dirpath, filename)
                    file_ext = os.path.splitext(filename)[1]
                    if file_ext not in self.extensions:
                        continue
                    template_name = os.path.relpath(filepath, template_dir)
                    all_files.append((template_name, filepath))

        return all_files
Ejemplo n.º 35
0
def get_django_template_dirs(loader_list=None):
    """Build a list of template directories based on configured loaders.
    """
    if not loader_list:
        loader_list = settings.TEMPLATE_LOADERS

    template_dirs = []
    for loader in loader_list:
        if loader in FILESYSTEM_LOADERS:
            template_dirs.extend(settings.TEMPLATE_DIRS)
        if loader in APPDIR_LOADERS:
            from django.template.loaders import app_directories
            if hasattr(app_directories, 'app_template_dirs'):
                template_dirs.extend(app_directories.app_template_dirs)
            elif hasattr(app_directories, 'get_app_template_dirs'):
                template_dirs.extend(app_directories.get_app_template_dirs('templates'))
        if isinstance(loader, (list, tuple)) and len(loader) >= 2:
            # The cached loader uses the tuple syntax, but simply search all
            # tuples for nested loaders; thus possibly support custom ones too.
            template_dirs.extend(get_django_template_dirs(loader[1]))

    return uniq(template_dirs)
Ejemplo n.º 36
0
    def get_template_sources(self, template_name, template_dirs=None):
        """This will append the site-prefix to every application template directory.  If
        SITE_ROOT is defined in settings it will scope to your application.  This is to enable
        the file finder to work correctly"""

        site_id = settings.SITE_ID
        site_root = getattr(settings, 'SITE_ROOT')
        site_prefixes = settings.SITE_FOLDERS.get(site_id, [])

        if not self.logged:
            log.debug("Getting sources = {0} ({1}) - {2} {3}".format(
                site_id, site_prefixes, template_name, template_dirs))
            self.logged = True

        if not isinstance(site_prefixes, (list, tuple)):
            site_prefixes = [site_prefixes]

        _app_template_dirs = []
        if not template_dirs:
            try:
                # Post Django 1.8
                template_dirs = app_directories.get_app_template_dirs(
                    'templates')
            except AttributeError:
                # Pre Django 1.8
                template_dirs = app_directories.app_template_dirs

        for app_dir in template_dirs:
            if site_root and site_root not in app_dir:
                continue
            for site_prefix in site_prefixes:
                _app_template_dirs.append(safe_join(app_dir, site_prefix))

        _app_template_dirs = tuple(_app_template_dirs) if len(
            _app_template_dirs) else None
        return super(AgileSiteAppDirectoriesFinder,
                     self).get_template_sources(template_name,
                                                _app_template_dirs)
Ejemplo n.º 37
0
from django.template.loaders import filesystem, app_directories
#Later versions of django seem to be fussy about get_library paths.
try:
    from django.template import import_library
except ImportError:
    try:
        from django.template.base import import_library
    except ImportError:
        import_library = get_library


try:
    from django.template.loaders.app_directories import app_template_dirs
except ImportError:
    from django.template.loaders.app_directories import get_app_template_dirs
    app_template_dirs = get_app_template_dirs('templates')

from django.conf import settings as mysettings

try:
    from django.template import get_templatetags_modules
except ImportError:
    #I've lifted this version from the django source
    try:
        from importlib import import_module
    except ImportError:
        from django.utils.importlib import import_module

    def get_templatetags_modules():
        """
        Return the list of all available template tag modules.
Ejemplo n.º 38
0
    def ready(self):
        global HANDLERS, CHANNELS, CHANNELS_NAMES, DEFAULT_HANDLER
        from django.conf import settings
        debug = getattr(settings, "INSTANT_DEBUG", False)
        from .utils import get_channels_for_roles
        from .conf import ENABLE_STAFF_CHANNEL, ENABLE_USERS_CHANNEL, \
            ENABLE_SUPERUSER_CHANNEL
        col = '\033[91m'
        endcol = '\033[0m'
        if ENABLE_STAFF_CHANNEL is True:
            print(col + "Warning from Django Instant" + endcol + 
                  ": the setting ENABLE_STAFF_CHANNEL "
                  "will be deprecated in version 0.8. "
                  "Please use declarative channels instead")
        if ENABLE_USERS_CHANNEL is True:
            print(col + "Warning from Django Instant" + endcol + 
                  ": the setting ENABLE_USERS_CHANNEL "
                  "will be deprecated in version 0.8. "
                  "Please use declarative channels instead")
        if ENABLE_SUPERUSER_CHANNEL is True:
            print(col + "Warning from Django Instant" + endcol + 
                  ": the setting ENABLE_SUPERUSER_CHANNEL "
                  "will be deprecated in version 0.8. "
                  "Please use declarative channels instead")

        CHANNELS, CHANNELS_NAMES = get_channels_for_roles()
        try:
            if debug is True:
                print("Django Instant registered channels:")
                print(json.dumps(CHANNELS, indent=2))
        except AttributeError:
            pass
        # get channels handlers
        project_template_dirpaths = []
        # get default templates paths
        tconf = getattr(settings, "TEMPLATES", None)
        if tconf is not None:
            if "DIRS" in tconf[0]:
                for dir_ in tconf[0]["DIRS"]:
                    dirpath = safe_join(settings.BASE_DIR, dir_)
                    project_template_dirpaths.append(dirpath)
        # get modules templates paths
        modules_templatepaths = list(get_app_template_dirs("templates"))
        template_dirpaths = project_template_dirpaths + modules_templatepaths
        # get handlers directoriess
        for dir_ in template_dirpaths:
            if "instant" in os.listdir(dir_):
                idir = dir_ + "/instant"
                if "handlers" in os.listdir(idir):
                    hdir = idir + "/handlers"
                    for handler in os.listdir(hdir):
                        chan_name = handler.split(
                            "/")[-1:][0].replace(".js", "")
                        if dir_ not in project_template_dirpaths:
                            # the default handler must be in project dir
                            if handler != "default.js":
                                HANDLERS[chan_name] = hdir + "/" + handler
                        else:
                            if handler == "default.js":
                                DEFAULT_HANDLER = hdir + "/" + handler
                            else:
                                HANDLERS[chan_name] = hdir + "/" + handler
        if debug is True:
            print("Instant channels handlers found:")
            for handler in HANDLERS:
                print(handler + ": " + HANDLERS[handler])
            if DEFAULT_HANDLER is None:
                print("No default handler found")
            else:
                print("Default handler: " + DEFAULT_HANDLER)
            print()
Ejemplo n.º 39
0
# show all templates
from django.template.loaders.app_directories import app_template_dirs
template_files = []
for template_dir in (settings.TEMPLATE_DIRS + app_template_dirs):
    for dir, dirnames, filenames in os.walk(template_dir):
        for filename in filenames:
        template_files.append(os.path.join(dir, filename))


# django>1.9
from django.conf import settings
from django.template.loaders.app_directories import get_app_template_dirs
import os

template_dir_list = []
for each in get_app_template_dirs('templates'):
    if settings.ROOT_DIR in each:
        template_dir_list.append(each)


template_list = []
for each in (template_dir_list + settings.TEMPLATES[0]['DIRS']):
    for dir, dirnames, filenames in os.walk(each):
        for filename in filenames:
            template_list.append(os.path.join(each, filename))





Ejemplo n.º 40
0
	def __init__(self, params):
		from django.template.loaders import app_directories

		options = params['OPTIONS']
		options['loader'] = JinjaLoader(tuple(params['DIRS']) + app_directories.get_app_template_dirs('templates'))
		super(Jinja2, self).__init__(params)
Ejemplo n.º 41
0
    def get_existing_styles(self, current_styles):
        """return list(tuple("key", "val"), ...)
        self -- current inline form instance.
        current_styles -- styles that are already present by default or
        obtained from other settings.

        If the settings are not present, it will return the list of already set
        styles, by default consisting only of [("tile", "Tile")].
        """
        try:
            setting = settings.COMPOSER
        except AttributeError:
            return current_styles

        if not setting.get("load-existing-styles"):
            return current_styles

        # Get a list of all the availible templates in the project.
        styles_settings = setting["load-existing-styles"]
        greedy = styles_settings.get("greedy", False)
        excludes = styles_settings.get("excludes")
        includes = styles_settings.get("includes")

        # If for some reason none of the actual values are supplied return an
        # empty list.
        if not greedy and (excludes is None) and (includes is None):
            return []

        # Get the app template directories and installed apps.
        template_dirs = app_directories.get_app_template_dirs("templates")
        installed_apps = apps.app_configs.keys()

        # Traverse all the directories within the template directories for all
        # available apps.
        template_dict = {}
        for app_dir in template_dirs:
            for path, dirnames, filenames in os.walk(app_dir):

                # Composer template tags expect the templates to live within
                # the app inclusion tags, so a good area to start ignoring any
                # templates that won't match.
                if filenames and "inclusion_tags" in path:
                    split = path.split("/")

                    # Ensures inclusion_tags is the current directory we are in.
                    if not split[len(split) - 1] == "inclusion_tags":
                        continue

                    # We already made sure inclusion_tags is in the path, so
                    # safe assumption is app_label should prefix it. In the
                    # event this is not a valid app name, these files will
                    # merely get ignored.
                    app_label = split[len(split) - 2]
                    for filename in filenames:

                        # The naming convention that the composer tags expect
                        # is modelname_style.html. If the template name has no
                        # underscores it won't ever be valid.
                        if len(filename.split("_")) > 1:
                            data = {
                                "path": os.path.join(path, filename),
                                "filename": filename
                            }

                            # There will more than likely be multiple templates
                            # with the same base key, so we add them to a list
                            # for iteration later on.
                            if template_dict.get(app_label, None) is None:
                                template_dict[app_label] = []
                            template_dict[app_label].append(data)

        # We compare against the actual installed apps and their models to try
        # and get rid of false positives.
        styles_dict = {}
        for model in apps.get_models():
            model_name = model._meta.model_name
            app_label = model._meta.app_label
            template_data = template_dict.get(app_label, None)
            if template_data is not None:

                if not greedy:
                    if excludes:
                        to_exclude = excludes.get(app_label, [])
                        if to_exclude == "__all__" or model_name in to_exclude:
                            continue
                    if includes:
                        to_include = includes.get(app_label, [])
                        if to_include != "__all__" and model_name not in to_include:
                            continue

                # Pattern to replace the modelname as well as the file
                # extension later on.
                pattern = re.compile((r"%s_|\.html" % (model_name)))

                for template in template_data:
                    filename = template["filename"]
                    path = template["path"]

                    # Further exclusions based on the composer template tag's
                    # expectations.
                    # <app_label>/inclusion_tags/<model_name>_<style>
                    if "%s/inclusion_tags/%s_" % (app_label,
                                                  model_name) in path:

                        # Remove the file extension and model name to get only
                        # the style.
                        style = pattern.sub("", filename)

                        # We add the styles to a dictionary, so we can easily
                        # make sure we don't have duplicate styles and convert
                        # it to a list of tuples.
                        if style not in styles_dict.keys():

                            # Try to make the values we display a bit more
                            # admin friendly.
                            styles_dict[style] = style.capitalize().replace(
                                "_", " ")

        # Return the list of tuples containing the found styles.
        new_styles = [(k, v) for k, v in styles_dict.iteritems()]

        # Make use of the built in set to remove exact duplicates then
        # parse back into a list of tuples.
        new_styles = set(current_styles + new_styles)
        return list(new_styles)
Ejemplo n.º 42
0
TEMPLATESADMIN_EDITHOOKS = tuple(_hooks)

_fixpath = lambda path: os.path.abspath(os.path.normpath(path))

try:
    additional_dirs = []
    for template_def in settings.TEMPLATES:
        additional_dirs.extend(template_def.get('DIRS', []))
except KeyError:
    additional_dirs = settings.TEMPLATE_DIRS

TEMPLATESADMIN_TEMPLATE_DIRS = getattr(
    settings,
    'TEMPLATESADMIN_TEMPLATE_DIRS', [
        d for d in additional_dirs + \
        list(get_app_template_dirs('templates')) if os.path.isdir(d)
    ]
)

TEMPLATESADMIN_TEMPLATE_DIRS = [_fixpath(dir) for dir in TEMPLATESADMIN_TEMPLATE_DIRS]

def user_in_templatesadmin_group(user):
    try:
        user.groups.get(name=TEMPLATESADMIN_GROUP)
        return True
    except ObjectDoesNotExist:
        return False

@never_cache
@user_passes_test(lambda u: user_in_templatesadmin_group(u))
@login_required
Ejemplo n.º 43
0
# show all templates
from django.template.loaders.app_directories import app_template_dirs
template_files = []
for template_dir in (settings.TEMPLATE_DIRS + app_template_dirs):
    for dir, dirnames, filenames in os.walk(template_dir):
        for filename in filenames:
        template_files.append(os.path.join(dir, filename))


# django>1.9
from django.conf import settings
from django.template.loaders.app_directories import get_app_template_dirs
import os

template_dir_list = []
for each in get_app_template_dirs('templates'):
    if settings.ROOT_DIR in each:
        template_dir_list.append(each)


template_list = []
for each in (template_dir_list + settings.TEMPLATES[0]['DIRS']):
    for dir, dirnames, filenames in os.walk(each):
        for filename in filenames:
            template_list.append(os.path.join(each, filename))





Ejemplo n.º 44
0
    def handle(self, *args, **options):
        template_dir_list = []
        for template_dir in get_app_template_dirs('templates'):
            if settings.BASE_DIR in template_dir:
                template_dir_list.append(template_dir)

        template_list = []
        for template_dir in (template_dir_list +
                             settings.TEMPLATES[0]['DIRS']):
            for base_dir, dirnames, filenames in os.walk(template_dir):
                for filename in filenames:
                    if ".html" in filename:
                        template_list.append(os.path.join(base_dir, filename))

        # using regex to grab the bundle tags content from html
        block_pattern = re.compile(
            r'({%\sbundle(.|\n)*?(?<={%\sendbundle\s%}))')
        open_pattern = re.compile(
            r'({%\s+bundle\s+(js|css|merge_js|merge_css)\s+?(file)?\s+?([^\s]*)?\s+?%})'
        )
        close_pattern = re.compile(r'({%\sendbundle\s%})')
        static_open_pattern = re.compile(r'({%\sstatic\s["|\'])')
        static_close_pattern = re.compile(r'(\s?%}(\"|\')?\s?\/?>)')

        # remove the previously bundled files
        for ext in ['js', 'scss', 'css']:
            rmdirs('assets', ext)
            rmdirs('static', ext)

        print('\nStart generating bundle files\n')

        # store unique entries for count
        rendered = dict()

        for template in template_list:
            try:
                f = open(('%s' % template).replace('/', os.sep),
                         'r',
                         encoding='utf8')

                t = f.read()
                if re.search(block_pattern, t) is not None:
                    for m in re.finditer(block_pattern, t):
                        block = m.group(0)
                        details = re.search(open_pattern, block)

                        # kind and name from the tag
                        kind = details.group(2)
                        name = details.group(4)

                        # remove open/close from the block
                        block = re.sub(open_pattern, '', block)
                        block = re.sub(close_pattern, '', block)

                        # clean static helper if we havent ran this through parse
                        block = re.sub(static_open_pattern, '', block)
                        block = re.sub(static_close_pattern, '>', block)

                        # render the template (producing a bundle file)
                        rendered[render(block, kind, 'file', name,
                                        True)] = True

            except Exception as e:
                print('-- X - failed to parse %s: %s' % (template, e))
                pass

        print('\nGenerated %s bundle files' % len(rendered))
Ejemplo n.º 45
0
def get_pattern_templates():
    templates = base_dict()
    template_dirs = get_app_template_dirs('templates')

    for lookup_dir in template_dirs:
        for root, dirs, files in os.walk(lookup_dir, topdown=True):
            # Ignore folders without files
            if not files:
                continue

            base_path = os.path.relpath(root, lookup_dir)
            section, path = section_for(base_path)

            # It has no section, ignore it
            if not section:
                continue

            found_templates = []
            for current_file in files:
                pattern_path = os.path.join(root, current_file)
                pattern_path = os.path.relpath(pattern_path, lookup_dir)

                if is_pattern(pattern_path):
                    template = get_template(pattern_path)
                    pattern_config = get_pattern_config(pattern_path)
                    pattern_name = pattern_config.get('name')
                    pattern_filename = os.path.relpath(
                        template.origin.template_name,
                        base_path,
                    )
                    if pattern_name:
                        template.pattern_name = pattern_name
                    else:
                        template.pattern_name = pattern_filename

                    template.pattern_filename = pattern_filename

                    found_templates.append(template)

            if found_templates:
                sub_folders = os.path.relpath(root, lookup_dir)
                sub_folders = os.path.relpath(sub_folders,
                                              path)  # TODO improve this
                templates_to_store = templates
                for folder in [section, *sub_folders.split(os.sep)]:
                    try:
                        templates_to_store = templates_to_store[
                            'template_groups'][folder]
                    except KeyError:
                        templates_to_store['template_groups'][
                            folder] = base_dict()
                        templates_to_store = templates_to_store[
                            'template_groups'][folder]

                templates_to_store['templates_stored'].extend(found_templates)

    # Order the templates alphabetically
    for templates_objs in templates['template_groups'].values():
        templates_objs['template_groups'] = order_dict(
            templates_objs['template_groups'])

    # Order the top level by the sections
    section_order = [section for section, _ in get_sections()]
    templates['template_groups'] = order_dict(
        templates['template_groups'],
        key_sort=lambda key: section_order.index(key))

    return templates
Ejemplo n.º 46
0
 def get_template_dirs(self):
     # TODO Should be able to control this with settings
     template_dir_list = get_app_template_dirs('templates')
     return list(template_dir_list) + list(settings.TEMPLATES[0]['DIRS'])
Ejemplo n.º 47
0
 def base_template_dirs(self):
     for template_dir in get_app_template_dirs('templates'):
         yield template_dir
     for template_dir in settings.TEMPLATES[0]['DIRS']:
         yield template_dir
Ejemplo n.º 48
0
import os
import fnmatch
import re

from django.conf import settings
from django.core.cache import cache
try:
    from django.apps import apps
    get_model = apps.get_model
except:
    from django.db.models.loading import get_model

try:
    # Django >= 1.8
    from django.template.loaders.app_directories import get_app_template_dirs
    app_template_dirs = get_app_template_dirs('templates')
except AttributeError:
    # Django <= 1.7
    from django.template.loaders.app_directories import app_template_dirs


def get_page_templates_raw(ignore_templates=None):

    if not ignore_templates:
        ignore_templates = ()

    files = []
    template_dirs = settings.TEMPLATES[0]['DIRS']
    for template_dir in template_dirs:
        for root, dirnames, filenames in os.walk(template_dir):
            for filename in fnmatch.filter(filenames, '*.html'):