Esempio n. 1
0
    def handle(self, *args, **options):
        # don't add django internal code
        apps = [app for app in filter(lambda app: not app.startswith('django.contrib'), settings.INSTALLED_APPS)]
        template_dirs = get_template_setting('DIRS', [])
        if template_dirs:
            apps += template_dirs
        for app_dir in apps:
            app_dir = app_dir.replace(".", "/")
            for top, dirs, files in os.walk(app_dir):
                for fn in files:
                    if os.path.splitext(fn)[1] in ('.py', '.html'):
                        fpath = os.path.join(top, fn)
                        annotation_lines = []
                        with open(fpath, 'r') as fd:
                            i = 0
                            for line in fd.readlines():
                                i += 1
                                if ANNOTATION_RE.search(line):
                                    tag, msg = ANNOTATION_RE.findall(line)[0]
                                    if len(args) == 1:
                                        search_for_tag = args[0].upper()
                                        if not search_for_tag == tag:
                                            break

                                    if ANNOTATION_END_RE.search(msg.strip()):
                                        msg = ANNOTATION_END_RE.findall(msg.strip())[0][0]

                                    annotation_lines.append("[%3s] %-5s %s" % (i, tag, msg.strip()))
                            if annotation_lines:
                                print("%s:" % fpath)
                                for annotation in annotation_lines:
                                    print("  * %s" % annotation)
                                print("")
Esempio n. 2
0
    def handle(self, *args, **options):
        # don't add django internal code
        apps = [app for app in filter(lambda app: not app.startswith('django.contrib'), settings.INSTALLED_APPS)]
        template_dirs = get_template_setting('DIRS', [])
        if template_dirs:
            apps += template_dirs
        for app_dir in apps:
            app_dir = app_dir.replace(".", "/")
            for top, dirs, files in os.walk(app_dir):
                for fn in files:
                    if os.path.splitext(fn)[1] in ('.py', '.html'):
                        fpath = os.path.join(top, fn)
                        annotation_lines = []
                        with open(fpath, 'r') as fd:
                            i = 0
                            for line in fd.readlines():
                                i += 1
                                if ANNOTATION_RE.search(line):
                                    tag, msg = ANNOTATION_RE.findall(line)[0]
                                    if len(args) == 1:
                                        search_for_tag = args[0].upper()
                                        if not search_for_tag == tag:
                                            break

                                    if ANNOTATION_END_RE.search(msg.strip()):
                                        msg = ANNOTATION_END_RE.findall(msg.strip())[0][0]

                                    annotation_lines.append("[%3s] %-5s %s" % (i, tag, msg.strip()))
                            if annotation_lines:
                                print("%s:" % fpath)
                                for annotation in annotation_lines:
                                    print("  * %s" % annotation)
                                print("")
Esempio n. 3
0
    def handle(self, *args, **options):
        if hasattr(settings, 'VALIDATE_TEMPLATES_IGNORES'):
            self.ignores = getattr(settings, 'VALIDATE_TEMPLATES_IGNORES')

        style = color_style()
        template_dirs = set(get_template_setting('DIRS'))
        template_dirs |= set(options.get('includes', []))
        template_dirs |= set(
            getattr(settings, 'VALIDATE_TEMPLATES_EXTRA_TEMPLATE_DIRS', []))

        if not options['no_apps']:
            ignore_apps = options['ignore_apps']
            if not ignore_apps and hasattr(settings,
                                           'VALIDATE_TEMPLATES_IGNORE_APPS'):
                ignore_apps = getattr(settings,
                                      'VALIDATE_TEMPLATES_IGNORE_APPS')
            for app in apps.get_app_configs():
                if app.name in ignore_apps:
                    continue
                app_template_dir = os.path.join(app.path, 'templates')
                if os.path.isdir(app_template_dir):
                    template_dirs.add(app_template_dir)

        # This is unsafe:
        # https://docs.djangoproject.com/en/1.10/topics/settings/#altering-settings-at-runtime
        if hasattr(settings, 'TEMPLATES'):
            settings.TEMPLATES[0]['DIRS'] = list(template_dirs)
        else:
            settings.TEMPLATE_DIRS = list(template_dirs)
        settings.TEMPLATE_DEBUG = True
        verbosity = int(options.get('verbosity', 1))
        errors = 0

        for template_dir in template_dirs:
            for root, dirs, filenames in os.walk(template_dir):
                for filename in filenames:
                    if self.ignore_filename(filename):
                        continue

                    filepath = os.path.join(root, filename)
                    if verbosity > 1:
                        self.stdout.write(filepath)
                    try:
                        get_template(filepath)
                    except Exception as e:
                        errors += 1
                        self.stdout.write(
                            "%s: %s" %
                            (filepath,
                             style.ERROR("%s %s" %
                                         (e.__class__.__name__, str(e)))))
                    if errors and options.get('break', False):
                        raise CommandError("Errors found")

        if errors:
            raise CommandError("%s errors found" % errors)
        self.stdout.write("%s errors found" % errors)
Esempio n. 4
0
    def handle(self, *args, **options):
        from django.conf import settings
        style = color_style()
        template_dirs = set(get_template_setting('DIRS'))
        template_dirs |= set(options.get('includes', []))
        template_dirs |= set(
            getattr(settings, 'VALIDATE_TEMPLATES_EXTRA_TEMPLATE_DIRS', []))

        # This is unsafe:
        # https://docs.djangoproject.com/en/1.10/topics/settings/#altering-settings-at-runtime
        if hasattr(settings, 'TEMPLATES'):
            settings.TEMPLATES[0]['DIRS'] = list(template_dirs)
        else:
            settings.TEMPLATE_DIRS = list(template_dirs)
        settings.TEMPLATE_DEBUG = True
        verbosity = int(options.get('verbosity', 1))
        errors = 0

        # Replace built in template tags with our own validating versions
        if options.get('check_urls', False):
            add_to_builtins_compat(
                'django_extensions.utils.validatingtemplatetags')

        for template_dir in template_dirs:
            for root, dirs, filenames in os.walk(template_dir):
                for filename in filenames:
                    if filename.endswith(".swp"):
                        continue
                    if filename.endswith("~"):
                        continue
                    filepath = os.path.join(root, filename)
                    if verbosity > 1:
                        print(filepath)
                    validatingtemplatetags.before_new_template(
                        options.get('force_new_urls', False))
                    try:
                        get_template(filepath)
                    except Exception as e:
                        errors += 1
                        print("%s: %s" %
                              (filepath,
                               style.ERROR("%s %s" %
                                           (e.__class__.__name__, str(e)))))
                    template_errors = validatingtemplatetags.get_template_errors(
                    )
                    for origin, line, message in template_errors:
                        errors += 1
                        print("%s(%s): %s" %
                              (origin, line, style.ERROR(message)))
                    if errors and options.get('break', False):
                        raise CommandError("Errors found")

        if errors:
            raise CommandError("%s errors found" % errors)
        print("%s errors found" % errors)
    def handle(self, *args, **options):
        if hasattr(settings, 'VALIDATE_TEMPLATES_IGNORES'):
            self.ignores = getattr(settings, 'VALIDATE_TEMPLATES_IGNORES')

        style = color_style()
        template_dirs = set(get_template_setting('DIRS'))
        template_dirs |= set(options.get('includes', []))
        template_dirs |= set(getattr(settings, 'VALIDATE_TEMPLATES_EXTRA_TEMPLATE_DIRS', []))

        if not options['no_apps']:
            ignore_apps = options['ignore_apps']
            if not ignore_apps and hasattr(settings, 'VALIDATE_TEMPLATES_IGNORE_APPS'):
                ignore_apps = getattr(settings, 'VALIDATE_TEMPLATES_IGNORE_APPS')
            for app in apps.get_app_configs():
                if app.name in ignore_apps:
                    continue
                app_template_dir = os.path.join(app.path, 'templates')
                if os.path.isdir(app_template_dir):
                    template_dirs.add(app_template_dir)

        # This is unsafe:
        # https://docs.djangoproject.com/en/1.10/topics/settings/#altering-settings-at-runtime
        if hasattr(settings, 'TEMPLATES'):
            settings.TEMPLATES[0]['DIRS'] = list(template_dirs)
        else:
            settings.TEMPLATE_DIRS = list(template_dirs)
        settings.TEMPLATE_DEBUG = True
        verbosity = int(options.get('verbosity', 1))
        errors = 0

        for template_dir in template_dirs:
            for root, dirs, filenames in os.walk(template_dir):
                for filename in filenames:
                    if self.ignore_filename(filename):
                        continue

                    filepath = os.path.realpath(os.path.join(root, filename))
                    if verbosity > 1:
                        print(filepath)
                    try:
                        get_template(filepath)
                    except Exception as e:
                        errors += 1
                        print("%s: %s" % (filepath, style.ERROR("%s %s" % (e.__class__.__name__, str(e)))))
                    if errors and options.get('break', False):
                        raise CommandError("Errors found")

        if errors:
            raise CommandError("%s errors found" % errors)
        print("%s errors found" % errors)
    def handle(self, *args, **options):
        if hasattr(settings, 'VALIDATE_TEMPLATES_IGNORES'):
            self.ignores = getattr(settings, 'VALIDATE_TEMPLATES_IGNORES')

        style = color_style()
        template_dirs = set(get_template_setting('DIRS'))
        template_dirs |= set(options['includes'])
        template_dirs |= set(getattr(settings, 'VALIDATE_TEMPLATES_EXTRA_TEMPLATE_DIRS', []))

        if not options['no_apps']:
            ignore_apps = options['ignore_apps']
            if not ignore_apps and hasattr(settings, 'VALIDATE_TEMPLATES_IGNORE_APPS'):
                ignore_apps = getattr(settings, 'VALIDATE_TEMPLATES_IGNORE_APPS')
            for app in apps.get_app_configs():
                if app.name in ignore_apps:
                    continue
                app_template_dir = os.path.join(app.path, 'templates')
                if os.path.isdir(app_template_dir):
                    template_dirs.add(app_template_dir)

        template_dirs = map(lambda path: os.path.abspath(path), template_dirs)
        settings.TEMPLATES[0]['DIRS'] = list(template_dirs)
        settings.TEMPLATE_DEBUG = True
        verbosity = options["verbosity"]
        errors = 0

        for template_dir in template_dirs:
            for root, dirs, filenames in os.walk(template_dir):
                for filename in filenames:
                    if self.ignore_filename(filename):
                        continue

                    filepath = os.path.join(root, filename)
                    if verbosity > 1:
                        self.stdout.write(filepath)
                    try:
                        get_template(filepath)
                    except Exception as e:
                        errors += 1
                        self.stdout.write("%s: %s" % (filepath, style.ERROR("%s %s" % (e.__class__.__name__, str(e)))))
                    if errors and options['break']:
                        raise CommandError("Errors found")

        if errors:
            raise CommandError("%s errors found" % errors)
        self.stdout.write("%s errors found" % errors)
Esempio n. 7
0
    def handle(self, *args, **options):
        # don't add django internal code
        apps = [
            app
            for app in filter(lambda app: not app.startswith('django.contrib'),
                              settings.INSTALLED_APPS)
        ]
        template_dirs = get_template_setting('DIRS', [])
        base_dir = getattr(settings, 'BASE_DIR')
        if template_dirs:
            apps += template_dirs
        for app_dir in apps:
            app_dir = app_dir.replace(".", "/")
            if base_dir:
                app_dir = os.path.join(base_dir, app_dir)
            for top, dirs, files in os.walk(app_dir):
                for fn in files:
                    if os.path.splitext(fn)[1] in ('.py', '.html'):
                        fpath = os.path.join(top, fn)
                        annotation_lines = []
                        with open(fpath, 'r') as fd:
                            i = 0
                            for line in fd.readlines():
                                i += 1
                                if ANNOTATION_RE.search(line):
                                    tag, msg = ANNOTATION_RE.findall(line)[0]
                                    if options['tag']:
                                        if tag not in map(
                                                str.upper,
                                                map(str, options['tag'])):
                                            break

                                    if ANNOTATION_END_RE.search(msg.strip()):
                                        msg = ANNOTATION_END_RE.findall(
                                            msg.strip())[0][0]

                                    annotation_lines.append(
                                        "[%3s] %-5s %s" %
                                        (i, tag, msg.strip()))
                            if annotation_lines:
                                self.stdout.write("%s:" % fpath)
                                for annotation in annotation_lines:
                                    annotation = annotation.decode(
                                        'utf-8') if six.PY2 else annotation
                                    self.stdout.write("  * %s" % annotation)
                                self.stdout.write("")
    def handle(self, *args, **options):
        if hasattr(settings, 'VALIDATE_TEMPLATES_IGNORES'):
            self.ignores = getattr(settings, 'VALIDATE_TEMPLATES_IGNORES')

        style = color_style()
        template_dirs = set(get_template_setting('DIRS'))
        template_dirs |= set(options['includes'])
        template_dirs |= set(getattr(settings, 'VALIDATE_TEMPLATES_EXTRA_TEMPLATE_DIRS', []))

        if not options['no_apps']:
            ignore_apps = options['ignore_apps']
            if not ignore_apps and hasattr(settings, 'VALIDATE_TEMPLATES_IGNORE_APPS'):
                ignore_apps = getattr(settings, 'VALIDATE_TEMPLATES_IGNORE_APPS')
            for app in apps.get_app_configs():
                if app.name in ignore_apps:
                    continue
                app_template_dir = os.path.join(app.path, 'templates')
                if os.path.isdir(app_template_dir):
                    template_dirs.add(app_template_dir)

        settings.TEMPLATES[0]['DIRS'] = list(template_dirs)
        settings.TEMPLATE_DEBUG = True
        verbosity = options["verbosity"]
        errors = 0

        for template_dir in template_dirs:
            for root, dirs, filenames in os.walk(template_dir):
                for filename in filenames:
                    if self.ignore_filename(filename):
                        continue

                    filepath = os.path.join(root, filename)
                    if verbosity > 1:
                        self.stdout.write(filepath)
                    try:
                        get_template(filepath)
                    except Exception as e:
                        errors += 1
                        self.stdout.write("%s: %s" % (filepath, style.ERROR("%s %s" % (e.__class__.__name__, str(e)))))
                    if errors and options['break']:
                        raise CommandError("Errors found")

        if errors:
            raise CommandError("%s errors found" % errors)
        self.stdout.write("%s errors found" % errors)
Esempio n. 9
0
    def handle(self, *args, **options):
        from django.conf import settings
        style = color_style()
        template_dirs = set(get_template_setting('DIRS'))
        template_dirs |= set(options.get('includes', []))
        template_dirs |= set(
            getattr(settings, 'VALIDATE_TEMPLATES_EXTRA_TEMPLATE_DIRS', []))

        # This is unsafe:
        # https://docs.djangoproject.com/en/1.10/topics/settings/#altering-settings-at-runtime
        if hasattr(settings, 'TEMPLATES'):
            settings.TEMPLATES[0]['DIRS'] = list(template_dirs)
        else:
            settings.TEMPLATE_DIRS = list(template_dirs)
        settings.TEMPLATE_DEBUG = True
        verbosity = int(options.get('verbosity', 1))
        errors = 0

        for template_dir in template_dirs:
            for root, dirs, filenames in os.walk(template_dir):
                for filename in filenames:
                    if filename.endswith(".swp"):
                        continue
                    if filename.endswith("~"):
                        continue
                    filepath = os.path.join(root, filename)
                    if verbosity > 1:
                        print(filepath)
                    try:
                        get_template(filepath)
                    except Exception as e:
                        errors += 1
                        print("%s: %s" %
                              (filepath,
                               style.ERROR("%s %s" %
                                           (e.__class__.__name__, str(e)))))
                    if errors and options.get('break', False):
                        raise CommandError("Errors found")

        if errors:
            raise CommandError("%s errors found" % errors)
        print("%s errors found" % errors)
Esempio n. 10
0
    def handle(self, *args, **options):
        from django.conf import settings
        style = color_style()
        template_dirs = set(get_template_setting('DIRS'))
        template_dirs |= set(options.get('includes', []))
        template_dirs |= set(getattr(settings, 'VALIDATE_TEMPLATES_EXTRA_TEMPLATE_DIRS', []))

        # This is unsafe:
        # https://docs.djangoproject.com/en/1.10/topics/settings/#altering-settings-at-runtime
        if hasattr(settings, 'TEMPLATES'):
            settings.TEMPLATES[0]['DIRS'] = list(template_dirs)
        else:
            settings.TEMPLATE_DIRS = list(template_dirs)
        settings.TEMPLATE_DEBUG = True
        verbosity = int(options.get('verbosity', 1))
        errors = 0

        for template_dir in template_dirs:
            for root, dirs, filenames in os.walk(template_dir):
                for filename in filenames:
                    if filename.endswith(".swp"):
                        continue
                    if filename.endswith("~"):
                        continue
                    filepath = os.path.join(root, filename)
                    if verbosity > 1:
                        print(filepath)
                    try:
                        get_template(filepath)
                    except Exception as e:
                        errors += 1
                        print("%s: %s" % (filepath, style.ERROR("%s %s" % (e.__class__.__name__, str(e)))))
                    if errors and options.get('break', False):
                        raise CommandError("Errors found")

        if errors:
            raise CommandError("%s errors found" % errors)
        print("%s errors found" % errors)
Esempio n. 11
0
    def handle(self, *args, **options):
        # don't add django internal code
        apps = [app for app in filter(lambda app: not app.startswith('django.contrib'), settings.INSTALLED_APPS)]
        template_dirs = get_template_setting('DIRS', [])
        base_dir = getattr(settings, 'BASE_DIR')
        if template_dirs:
            apps += template_dirs
        for app_dir in apps:
            app_dir = app_dir.replace(".", "/")
            if base_dir:
                app_dir = os.path.join(base_dir, app_dir)
            for top, dirs, files in os.walk(app_dir):
                for fn in files:
                    if os.path.splitext(fn)[1] in ('.py', '.html'):
                        fpath = os.path.join(top, fn)
                        annotation_lines = []
                        with open(fpath, 'r') as fd:
                            i = 0
                            for line in fd.readlines():
                                i += 1
                                if ANNOTATION_RE.search(line):
                                    tag, msg = ANNOTATION_RE.findall(line)[0]
                                    if options['tag']:
                                        if tag not in map(str.upper, map(str, options['tag'])):
                                            break

                                    if ANNOTATION_END_RE.search(msg.strip()):
                                        msg = ANNOTATION_END_RE.findall(msg.strip())[0][0]

                                    annotation_lines.append("[%3s] %-5s %s" % (i, tag, msg.strip()))
                            if annotation_lines:
                                self.stdout.write("%s:" % fpath)
                                for annotation in annotation_lines:
                                    annotation = annotation.decode('utf-8') if six.PY2 else annotation
                                    self.stdout.write("  * %s" % annotation)
                                self.stdout.write("")
Esempio n. 12
0
 def test_new_dir_template(self):
     setting = get_template_setting('DIRS')
     self.assertEqual(setting, ['asdf'])
Esempio n. 13
0
 def test_should_return_default_if_TEMPLATES_setting_is_none(self):
     self.assertEqual(get_template_setting('template_key', 'test'), 'test')
Esempio n. 14
0
 def test_should_return_value_for_key(self):
     self.assertEqual(get_template_setting('BACKEND'),
                      'django.template.backends.django.DjangoTemplates')
Esempio n. 15
0
 def test_should_return_None_by_default_if_TEMPLATES_setting_is_none(self):
     self.assertIsNone(get_template_setting('template_key'))
Esempio n. 16
0
 def test_default_setting(self):
     setting = get_template_setting('ASDF', 'default')
     self.assertEqual(setting, 'default')
Esempio n. 17
0
 def test_old_dir_missing(self):
     setting = get_template_setting('DIRS', 'default')
     self.assertEqual(setting, 'default')