<commit_before># This file is part of Indico.
# Copyright (C) 2002 - 2017 European Organization for Nuclear Research (CERN).
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# Indico is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Indico; if not, see <http://www.gnu.org/licenses/>.

import os

from flask import current_app, json, session, render_template, send_from_directory, Response
from werkzeug.exceptions import NotFound

from indico.core.config import Config
from indico.core.plugins import plugin_engine
from indico.modules.events.layout import theme_settings
from indico.modules.users.util import serialize_user
from indico.util.caching import make_hashable
from indico.util.i18n import po_to_json
from indico.util.string import crc32
from indico.web.assets.util import get_asset_path
from indico.web.assets.vars_js import generate_global_file
from indico.web.flask.util import send_file
from indico.web.flask.wrappers import IndicoBlueprint


assets_blueprint = IndicoBlueprint('assets', __name__, url_prefix='/assets')


@assets_blueprint.route('/js-vars/global.js')
def js_vars_global():
    """
    Provides a JS file with global definitions (all users)
    Useful for server-wide config options, URLs, etc...
    """
    config = Config.getInstance()
    config_hash = crc32(repr(make_hashable(sorted(config._configVars.items()))))
    cache_file = os.path.join(config.getCacheDir(), 'assets_global_{}.js'.format(config_hash))

    if not os.path.exists(cache_file):
        data = generate_global_file(config)
        with open(cache_file, 'wb') as f:
            f.write(data)

    return send_file('global.js', cache_file,
                     mimetype='application/x-javascript', no_cache=False, conditional=True)


@assets_blueprint.route('/js-vars/user.js')
def js_vars_user():
    """
    Provides a JS file with user-specific definitions
    Useful for favorites, settings etc.
    """
    user = session.user
    if user is None:
        user_vars = {}<commit_msg>Add `assets.image` endpoint to build image URLs<commit_after># This file is part of Indico.
# Copyright (C) 2002 - 2017 European Organization for Nuclear Research (CERN).
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# Indico is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Indico; if not, see <http://www.gnu.org/licenses/>.

import os

from flask import current_app, json, session, render_template, send_from_directory, Response
from werkzeug.exceptions import NotFound

from indico.core.config import Config
from indico.core.plugins import plugin_engine
from indico.modules.events.layout import theme_settings
from indico.modules.users.util import serialize_user
from indico.util.caching import make_hashable
from indico.util.i18n import po_to_json
from indico.util.string import crc32
from indico.web.assets.util import get_asset_path
from indico.web.assets.vars_js import generate_global_file
from indico.web.flask.util import send_file
from indico.web.flask.wrappers import IndicoBlueprint


assets_blueprint = IndicoBlueprint('assets', __name__, url_prefix='/assets')
assets_blueprint.add_url_rule('!/images/<path:filename>', 'image', build_only=True)


@assets_blueprint.route('/js-vars/global.js')
def js_vars_global():
    """
    Provides a JS file with global definitions (all users)
    Useful for server-wide config options, URLs, etc...
    """
    config = Config.getInstance()
    config_hash = crc32(repr(make_hashable(sorted(config._configVars.items()))))
    cache_file = os.path.join(config.getCacheDir(), 'assets_global_{}.js'.format(config_hash))

    if not os.path.exists(cache_file):
        data = generate_global_file(config)
        with open(cache_file, 'wb') as f:
            f.write(data)

    return send_file('global.js', cache_file,
                     mimetype='application/x-javascript', no_cache=False, conditional=True)


@assets_blueprint.route('/js-vars/user.js')
def js_vars_user():
    """
    Provides a JS file with user-specific definitions
    Useful for favorites, settings etc.
    """
    user = session.user
    if user is None:
        user_vars = {}
{'fraction_non_alphanumeric': 0.0895492620662146, 'fraction_numerical': 0.005983246908655764, 'mean_word_length': 4.24581589958159, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3688243', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1444, 'n_words': 491}