示例#1
0
def collect_theme_assets(theme, assets=None, template=None, parents=[]):
    theme_name = theme['name']
    themes = get_resource_service('themes')
    is_local_upload = themes.is_uploaded_theme(
        theme_name) and not themes.is_s3_storage_enabled

    assets = assets or {
        'scripts': [],
        'styles': [],
        'devScripts': [],
        'devStyles': []
    }
    # Load the template.
    if not template:
        if themes.is_local_theme(theme_name) or is_local_upload:
            template_file_name = themes.get_theme_template_filename(theme_name)
            if os.path.exists(template_file_name):
                template = open(template_file_name, encoding='utf-8').read()
            else:
                template = theme.get('template')
        else:
            template = theme.get('template')
    # Add assets from parent theme.
    if theme.get('extends') and not \
            theme.get('seoTheme') and \
            (theme.get('name') != theme.get('extends')) and \
            (theme.get('extends') not in parents):
        parent_theme = get_resource_service('themes').find_one(
            req=None, name=theme.get('extends'))
        if parent_theme:
            parents.append(theme.get('extends'))
            assets, template = collect_theme_assets(parent_theme,
                                                    assets=assets,
                                                    template=template,
                                                    parents=parents)
        else:
            error_message = 'Embed: "%s" theme depends on "%s" but this theme is not registered.' \
                % (theme_name, theme.get('extends'))
            logger.info(error_message)
            raise UnknownTheme(error_message)

    # Add assets from theme.
    static_endpoint = 'themes_assets.static'
    if themes.is_uploaded_theme(theme_name):
        static_endpoint = 'themes_uploads.static'

    for asset_type in ('scripts', 'styles', 'devScripts', 'devStyles'):
        theme_folder = theme['name']
        for url in theme.get(asset_type, []):
            if is_relative_to_current_folder(url):
                if theme.get('public_url', False):
                    url = '%s%s' % (theme.get('public_url'), url)
                else:
                    url = url_for(static_endpoint,
                                  filename=os.path.join(theme_folder, url),
                                  _external=False)
            assets[asset_type].append(url)

    return assets, template
示例#2
0
def collect_theme_assets(theme, assets=None, template=None):
    assets = assets or {
        'scripts': [],
        'styles': [],
        'devScripts': [],
        'devStyles': []
    }
    # Load the template.
    if not template:
        template_file_name = os.path.join(THEMES_DIRECTORY, THEMES_ASSETS_DIR,
                                          theme['name'], 'template.html')
        if os.path.isfile(template_file_name):
            template = open(template_file_name, encoding='utf-8').read()

    # Add assets from parent theme.
    if theme.get('extends', None):
        parent_theme = get_resource_service('themes').find_one(
            req=None, name=theme.get('extends'))
        if parent_theme:
            assets, template = collect_theme_assets(parent_theme,
                                                    assets=assets,
                                                    template=template)
        else:
            error_message = 'Embed: "%s" theme depends on "%s" but this theme is not registered.' \
                % (theme.get('name'), theme.get('extends'))
            logger.info(error_message)
            raise UnknownTheme(error_message)

    # Add assets from theme.
    for asset_type in ('scripts', 'styles', 'devScripts', 'devStyles'):
        theme_folder = theme['name']
        for url in theme.get(asset_type, []):
            if is_relative_to_current_folder(url):
                if theme.get('public_url', False):
                    url = '%s%s' % (theme.get('public_url'), url)
                else:
                    url = url_for('themes_assets.static',
                                  filename=os.path.join(theme_folder, url),
                                  _external=False)
            assets[asset_type].append(url)

    return assets, template