예제 #1
0
    def themed(self, name, theme):
        """
        Returns True if given asset override is provided by the given theme otherwise returns False.
        Args:
            name: asset name e.g. 'images/logo.png'
            theme: theme name e.g. 'red-theme', 'edx.org'

        Returns:
            True if given asset override is provided by the given theme otherwise returns False
        """
        if not is_comprehensive_theming_enabled():
            return False

        # in debug mode check static asset from within the project directory
        if settings.DEBUG:
            themes_location = get_theme_base_dir(theme, suppress_error=True)
            # Nothing can be themed if we don't have a theme location or required params.
            if not all((themes_location, theme, name)):
                return False

            themed_path = "/".join([
                themes_location,
                theme,
                "static/"
            ])
            name = name[1:] if name.startswith("/") else name
            path = safe_join(themed_path, name)
            return os.path.exists(path)
        # in live mode check static asset in the static files dir defined by "STATIC_ROOT" setting
        return self.exists(os.path.join(theme, name))
예제 #2
0
    def themed(self, name, theme):
        """
        Returns True if given asset override is provided by the given theme otherwise returns False.
        Args:
            name: asset name e.g. 'images/logo.png'
            theme: theme name e.g. 'red-theme', 'edx.org'

        Returns:
            True if given asset override is provided by the given theme otherwise returns False
        """
        if not is_comprehensive_theming_enabled():
            return False

        # in debug mode check static asset from within the project directory
        if settings.DEBUG:
            themes_location = get_base_themes_dir()
            # Nothing can be themed if we don't have a theme location or required params.
            if not all((themes_location, theme, name)):
                return False

            themed_path = "/".join([
                themes_location,
                theme,
                "static/"
            ])
            name = name[1:] if name.startswith("/") else name
            path = safe_join(themed_path, name)
            return os.path.exists(path)
        # in live mode check static asset in the static files dir defined by "STATIC_ROOT" setting
        else:
            return self.exists(os.path.join(theme, name))
예제 #3
0
    def handle(self, *args, **options):
        """
        Handle update_assets command.
        """
        logger.info("Sass compilation started.")
        info = []

        themes, system, source_comments, output_style, collect = self.parse_arguments(
            *args, **options)

        if not is_comprehensive_theming_enabled():
            themes = []
            logger.info(
                "Skipping theme sass compilation as theming is disabled.")

        for sass_dir in get_sass_directories(themes, system):
            result = compile_sass(
                sass_source_dir=sass_dir['sass_source_dir'],
                css_destination_dir=sass_dir['css_destination_dir'],
                lookup_paths=sass_dir['lookup_paths'],
                output_style=output_style,
                source_comments=source_comments,
            )
            info.append(result)

        logger.info("Sass compilation completed.")

        for sass_dir, css_dir, duration in info:
            logger.info(">> %s -> %s in %ss", sass_dir, css_dir, duration)
        logger.info("\n")

        if collect and not settings.DEBUG:
            # Collect static assets
            collect_assets()
    def handle(self, *args, **options):
        """
        Handle update_assets command.
        """
        logger.info("Sass compilation started.")
        info = []

        themes, system, source_comments, output_style, collect = self.parse_arguments(*args, **options)

        if not is_comprehensive_theming_enabled():
            themes = []
            logger.info("Skipping theme sass compilation as theming is disabled.")

        for sass_dir in get_sass_directories(themes, system):
            result = compile_sass(
                sass_source_dir=sass_dir['sass_source_dir'],
                css_destination_dir=sass_dir['css_destination_dir'],
                lookup_paths=sass_dir['lookup_paths'],
                output_style=output_style,
                source_comments=source_comments,
            )
            info.append(result)

        logger.info("Sass compilation completed.")

        for sass_dir, css_dir, duration in info:
            logger.info(">> %s -> %s in %ss", sass_dir, css_dir, duration)
        logger.info("\n")

        if collect and not settings.DEBUG:
            # Collect static assets
            collect_assets()
예제 #5
0
    def ready(self):
        """
        startup run method, this method is called after the application has successfully initialized.
        Anything that needs to executed once (and only once) the theming app starts can be placed here.
        """
        if is_comprehensive_theming_enabled():
            # proceed only if comprehensive theming in enabled

            enable_theming()
예제 #6
0
    def parse_arguments(*args, **options):  # pylint: disable=unused-argument
        """
        Parse and validate arguments for update_assets command.

        Args:
            *args: Positional arguments passed to the update_assets command
            **options: optional arguments passed to the update_assets command
        Returns:
            A tuple containing parsed values for themes, system, source comments and output style.
            1. themes (list): list of Theme objects
            2. system (bool): True if system sass need to be compiled, False otherwise
            3. source_comments (bool): True if source comments need to be included in output, False otherwise
            4. output_style (str): Coding style for compiled css files.
        """
        given_themes = options.get("themes", ["all"])
        output_style = options.get("output_style", "nested")
        system = options.get("system", True)
        source_comments = options.get("source_comments", False)
        collect = options.get("collect", True)

        available_themes = {t.theme_dir: t for t in get_themes()}

        if 'no' in given_themes or 'all' in given_themes:
            # Raise error if 'all' or 'no' is present and theme names are also given.
            if len(given_themes) > 1:
                raise CommandError(
                    "Invalid themes value, It must either be 'all' or 'no' or list of themes."
                )
        # Raise error if any of the given theme name is invalid
        # (theme name would be invalid if it does not exist in themes directory)
        elif (not set(given_themes).issubset(available_themes.keys())
              ) and is_comprehensive_theming_enabled():
            raise CommandError(
                "Given themes '{invalid_themes}' do not exist inside themes directory '{themes_dir}'"
                .format(
                    invalid_themes=", ".join(
                        set(given_themes) - set(available_themes.keys())),
                    themes_dir=get_base_themes_dir(),
                ), )

        if "all" in given_themes:
            themes = get_themes()
        elif "no" in given_themes:
            themes = []
        else:
            # convert theme names to Theme objects
            themes = [available_themes.get(theme) for theme in given_themes]

        return themes, system, source_comments, output_style, collect
    def parse_arguments(*args, **options):  # pylint: disable=unused-argument
        """
        Parse and validate arguments for update_assets command.

        Args:
            *args: Positional arguments passed to the update_assets command
            **options: optional arguments passed to the update_assets command
        Returns:
            A tuple containing parsed values for themes, system, source comments and output style.
            1. themes (list): list of Theme objects
            2. system (bool): True if system sass need to be compiled, False otherwise
            3. source_comments (bool): True if source comments need to be included in output, False otherwise
            4. output_style (str): Coding style for compiled css files.
        """
        given_themes = options.get("themes", ["all"])
        output_style = options.get("output_style", "nested")
        system = options.get("system", True)
        source_comments = options.get("source_comments", False)
        collect = options.get("collect", True)

        available_themes = {t.theme_dir: t for t in get_themes()}

        if 'no' in given_themes or 'all' in given_themes:
            # Raise error if 'all' or 'no' is present and theme names are also given.
            if len(given_themes) > 1:
                raise CommandError("Invalid themes value, It must either be 'all' or 'no' or list of themes.")
        # Raise error if any of the given theme name is invalid
        # (theme name would be invalid if it does not exist in themes directory)
        elif (not set(given_themes).issubset(available_themes.keys())) and is_comprehensive_theming_enabled():
            raise CommandError(
                "Given themes '{invalid_themes}' do not exist inside themes directory '{themes_dir}'".format(
                    invalid_themes=", ".join(set(given_themes) - set(available_themes.keys())),
                    themes_dir=get_base_themes_dir(),
                ),
            )

        if "all" in given_themes:
            themes = get_themes()
        elif "no" in given_themes:
            themes = []
        else:
            # convert theme names to Theme objects
            themes = [available_themes.get(theme) for theme in given_themes]

        return themes, system, source_comments, output_style, collect