예제 #1
0
파일: __init__.py 프로젝트: assembl/assembl
def get_default_context(request, **kwargs):
    kwargs.update(default_context)
    from ..auth.util import get_user, get_current_discussion
    if request.scheme == "http"\
            and asbool(config.get("require_secure_connection")):
        raise HTTPFound(get_global_base_url(True) + request.path_qs)
    react_url = '/static2'
    use_webpack_server = asbool(config.get('use_webpack_server'))
    if use_webpack_server:
        # Allow to specify a distinct webpack_host in configuration.
        # Useful for development tests of social auth through a reverse tunnel.
        # Otherwise fallback on public_hostname, then localhost.
        webpack_host = config.get(
            'webpack_host',
            config.get('public_hostname',
                       'localhost'))
        react_url = 'http://%s:%d' % (
            webpack_host,
            int(config.get('webpack_port', 8000)))
    socket_proxied = asbool(config.get('changes_websocket_proxied'))
    websocket_port = None if socket_proxied \
        else config.get('changes_websocket_port')
    secure_socket = socket_proxied and (
        asbool(config.get("require_secure_connection")) or (asbool(config.get("accept_secure_connection")) and request.url.startswith('https:')))
    application_url = get_global_base_url()
    socket_url = get_global_base_url(
        secure_socket, websocket_port) + config.get('changes_prefix')

    localizer = request.localizer
    _ = TranslationStringFactory('assembl')
    user = get_user(request)
    if user and user.username:
        user_profile_edit_url = request.route_url(
            'profile_user', type='u', identifier=user.username.username)
    elif user:
        user_profile_edit_url = request.route_url(
            'profile_user', type='id', identifier=user.id)
    else:
        user_profile_edit_url = None

    web_analytics_piwik_script = config.get(
        'web_analytics_piwik_script') or False
    discussion = get_current_discussion()
    if (web_analytics_piwik_script and discussion and discussion.web_analytics_piwik_id_site):
        web_analytics_piwik_script = web_analytics_piwik_script % (
            discussion.web_analytics_piwik_id_site,
            discussion.web_analytics_piwik_id_site)
    else:
        web_analytics_piwik_script = False

    web_analytics_piwik_custom_variable_size = config.get('web_analytics_piwik_custom_variable_size')
    if not web_analytics_piwik_custom_variable_size:
        web_analytics_piwik_custom_variable_size = 5

    help_url = config.get('help_url') or ''
    if discussion and discussion.help_url:
        help_url = discussion.help_url
    if help_url and "%s" in help_url:
        help_url = help_url % localizer.locale_name

    first_login_after_auto_subscribe_to_notifications = False
    if (user and discussion and discussion.id and user.is_first_visit and
        discussion.subscribe_to_notifications_on_signup and
            user.is_participant(discussion.id)):
        first_login_after_auto_subscribe_to_notifications = True
    locales = config.get('available_languages').split()
    countries_for_locales = defaultdict(set)
    for locale in locales:
        countries_for_locales[get_language(locale)].add(get_country(locale))
    show_locale_country = {
        locale: (len(countries_for_locales[get_language(locale)]) > 1)
        for locale in locales}
    jedfilename = os.path.join(
        os.path.dirname(__file__), '..', 'locale',
        localizer.locale_name, 'LC_MESSAGES', 'assembl.jed.json')
    if not os.path.exists(jedfilename) and '_' in localizer.locale_name:
        jedfilename = os.path.join(
            os.path.dirname(__file__), '..', 'locale',
            get_language(localizer.locale_name), 'LC_MESSAGES',
            'assembl.jed.json')
    assert os.path.exists(jedfilename)

    from ..models.facebook_integration import language_sdk_existance
    fb_lang_exists, fb_locale = language_sdk_existance(
        get_language(localizer.locale_name), countries_for_locales)

    def process_export_list(ls):
        return map(lambda s: s.strip(), ls.split(","))

    social_settings = {
        'fb_export_permissions': config.get('facebook.export_permissions'),
        'fb_debug': asbool(config.get('facebook.debug_mode')),
        'fb_app_id': config.get('facebook.consumer_key'),
        'fb_api_version': config.get('facebook.api_version') or '2.2',
        'supported_exports': process_export_list(
            config.get('supported_exports_list'))
    }

    # A container for all analytics related settings. All future
    # analytics based settings that will be exposed to the templates
    # should be included in this dictionary
    analytics_settings = {
        'enabled': True if web_analytics_piwik_script else False,
    }

    if analytics_settings.get('enabled', False):
        analytics_settings['piwik'] = {
            'script': web_analytics_piwik_script,
            'host': config.get('piwik_host')
        }

    analytics_url = config.get('web_analytics_piwik_url', None)

    get_route = create_get_route(request, discussion)
    providers = get_provider_data(get_route)

    errors = request.session.pop_flash()
    if kwargs.get('error', None):
        errors.append(kwargs['error'])
    if errors:
        kwargs['error'] = '<br />'.join(errors)
    messages = request.session.pop_flash('message')
    if messages:
        kwargs['messages'] = '<br />'.join(messages)

    admin_email = config.get('assembl.admin_email', None)
    # If an admin_email is improperly configured, raise an error
    if admin_email is None or admin_email is '':
        raise HTTPInternalServerError(explanation="Assembl MUST have an admin_email configured in order to operate.")

    theme_name, theme_relative_path = get_theme_info(discussion)
    node_env = os.getenv('NODE_ENV', 'production')
    under_test = bool(config.get('under_test') or False)
    base = dict(
        kwargs,
        request=request,
        application_url=application_url,
        get_route=get_route,
        user=user,
        templates=get_template_views(),
        discussion=discussion or {},  # Templates won't load without a discussion object
        preferences=discussion.preferences if discussion else {},
        user_profile_edit_url=user_profile_edit_url,
        locale=localizer.locale_name,
        locales=locales,
        fb_lang_exists=fb_lang_exists,
        fb_locale=fb_locale,
        social_settings=social_settings,
        show_locale_country=show_locale_country,
        NODE_ENV=node_env,
        theme_name=theme_name,
        theme_relative_path=theme_relative_path,
        minified_js=config.get('minified_js') or False,
        web_analytics=analytics_settings,
        analytics_url=analytics_url,
        help_url=help_url,
        socket_url=socket_url,
        REACT_URL=react_url,
        elasticsearch_lang_indexes=config.get('elasticsearch_lang_indexes', 'en fr'),
        first_login_after_auto_subscribe_to_notifications=first_login_after_auto_subscribe_to_notifications,
        sentry_dsn=config.get('sentry_dsn', ''),
        activate_tour=str(config.get('activate_tour') or False).lower(),
        providers=providers,
        providers_json=json.dumps(providers),
        translations=io.open(jedfilename, encoding='utf-8').read(),
        admin_email=admin_email,
        under_test=under_test
    )

    base.update({
        "opengraph_locale": get_opengraph_locale(request),
        "get_description": get_description(request),
        "get_landing_page_image": get_landing_page_image(),
        "private_social_sharing": private_social_sharing(),
        "get_topic": get_topic(request),
        "get_discussion_url": get_discussion_url(),
        "discussion_title": discussion_title(),
    })
    base.update(get_v1_resources_hash())
    return base
예제 #2
0
 def test_get_country_case5(self):
     locale = "frCA"
     assert get_country(locale) == None
예제 #3
0
 def test_get_country_case4(self):
     locale = "fr-ca"
     assert get_country(locale) == "CA"
예제 #4
0
 def test_get_country_case3(self):
     locale = ""
     assert get_country(locale) == None
예제 #5
0
 def test_get_country_case1(self):
     locale = "fr_CA"
     assert get_country(locale) == "CA"
예제 #6
0
def get_default_context(request, **kwargs):
    kwargs.update(default_context)
    from ..auth.util import get_user, get_current_discussion
    if request.scheme == "http"\
            and asbool(config.get("require_secure_connection")):
        raise HTTPFound(get_global_base_url(True) + request.path_qs)
    react_url = '/static2'
    use_webpack_server = asbool(config.get('use_webpack_server'))
    if use_webpack_server:
        # Allow to specify a distinct webpack_host in configuration.
        # Useful for development tests of social auth through a reverse tunnel.
        # Otherwise fallback on public_hostname, then localhost.
        webpack_host = config.get('webpack_host',
                                  config.get('public_hostname', 'localhost'))
        react_url = 'http://%s:%d' % (webpack_host,
                                      int(config.get('webpack_port', 8000)))
    socket_proxied = asbool(config.get('changes_websocket_proxied'))
    websocket_port = None if socket_proxied \
        else config.get('changes_websocket_port')
    secure_socket = socket_proxied and (
        asbool(config.get("require_secure_connection")) or
        (asbool(config.get("accept_secure_connection"))
         and request.url.startswith('https:')))
    application_url = get_global_base_url()
    socket_url = get_global_base_url(
        secure_socket, websocket_port) + config.get('changes_prefix')

    localizer = request.localizer
    _ = TranslationStringFactory('assembl')
    user = get_user(request)
    if user and user.username:
        user_profile_edit_url = request.route_url(
            'profile_user', type='u', identifier=user.username.username)
    elif user:
        user_profile_edit_url = request.route_url('profile_user',
                                                  type='id',
                                                  identifier=user.id)
    else:
        user_profile_edit_url = None

    web_analytics_piwik_script = config.get(
        'web_analytics_piwik_script') or False
    discussion = get_current_discussion()
    if (web_analytics_piwik_script and discussion
            and discussion.web_analytics_piwik_id_site):
        web_analytics_piwik_script = web_analytics_piwik_script % (
            discussion.web_analytics_piwik_id_site,
            discussion.web_analytics_piwik_id_site)
    else:
        web_analytics_piwik_script = False

    web_analytics_piwik_custom_variable_size = config.get(
        'web_analytics_piwik_custom_variable_size')
    if not web_analytics_piwik_custom_variable_size:
        web_analytics_piwik_custom_variable_size = 5

    help_url = config.get('help_url') or ''
    if discussion and discussion.help_url:
        help_url = discussion.help_url
    if help_url and "%s" in help_url:
        help_url = help_url % localizer.locale_name

    first_login_after_auto_subscribe_to_notifications = False
    if (user and discussion and discussion.id and user.is_first_visit
            and discussion.subscribe_to_notifications_on_signup
            and user.is_participant(discussion.id)):
        first_login_after_auto_subscribe_to_notifications = True
    locales = config.get('available_languages').split()
    countries_for_locales = defaultdict(set)
    for locale in locales:
        countries_for_locales[get_language(locale)].add(get_country(locale))
    show_locale_country = {
        locale: (len(countries_for_locales[get_language(locale)]) > 1)
        for locale in locales
    }
    jedfilename = os.path.join(os.path.dirname(__file__), '..', 'locale',
                               localizer.locale_name, 'LC_MESSAGES',
                               'assembl.jed.json')
    if not os.path.exists(jedfilename) and '_' in localizer.locale_name:
        jedfilename = os.path.join(os.path.dirname(__file__), '..', 'locale',
                                   get_language(localizer.locale_name),
                                   'LC_MESSAGES', 'assembl.jed.json')
    assert os.path.exists(jedfilename)

    from ..models.facebook_integration import language_sdk_existance
    fb_lang_exists, fb_locale = language_sdk_existance(
        get_language(localizer.locale_name), countries_for_locales)

    def process_export_list(ls):
        return map(lambda s: s.strip(), ls.split(","))

    social_settings = {
        'fb_export_permissions':
        config.get('facebook.export_permissions'),
        'fb_debug':
        asbool(config.get('facebook.debug_mode')),
        'fb_app_id':
        config.get('facebook.consumer_key'),
        'fb_api_version':
        config.get('facebook.api_version') or '2.2',
        'supported_exports':
        process_export_list(config.get('supported_exports_list'))
    }

    # A container for all analytics related settings. All future
    # analytics based settings that will be exposed to the templates
    # should be included in this dictionary
    analytics_settings = {
        'enabled': True if web_analytics_piwik_script else False,
    }

    if analytics_settings.get('enabled', False):
        analytics_settings['piwik'] = {
            'script': web_analytics_piwik_script,
            'host': config.get('piwik_host')
        }

    analytics_url = config.get('web_analytics_piwik_url', None)

    get_route = create_get_route(request, discussion)
    providers = get_provider_data(get_route)

    errors = request.session.pop_flash()
    if kwargs.get('error', None):
        errors.append(kwargs['error'])
    if errors:
        kwargs['error'] = '<br />'.join(errors)
    messages = request.session.pop_flash('message')
    if messages:
        kwargs['messages'] = '<br />'.join(messages)

    admin_email = config.get('assembl.admin_email', None)
    # If an admin_email is improperly configured, raise an error
    if admin_email is None or admin_email is '':
        raise HTTPInternalServerError(
            explanation=
            "Assembl MUST have an admin_email configured in order to operate.")

    theme_name, theme_relative_path = get_theme_info(discussion)
    node_env = os.getenv('NODE_ENV', 'production')
    under_test = bool(config.get('under_test') or False)
    base = dict(
        kwargs,
        request=request,
        application_url=application_url,
        get_route=get_route,
        user=user,
        templates=get_template_views(),
        discussion=discussion
        or {},  # Templates won't load without a discussion object
        preferences=discussion.preferences if discussion else {},
        user_profile_edit_url=user_profile_edit_url,
        locale=localizer.locale_name,
        locales=locales,
        fb_lang_exists=fb_lang_exists,
        fb_locale=fb_locale,
        social_settings=social_settings,
        show_locale_country=show_locale_country,
        NODE_ENV=node_env,
        theme_name=theme_name,
        theme_relative_path=theme_relative_path,
        minified_js=config.get('minified_js') or False,
        web_analytics=analytics_settings,
        analytics_url=analytics_url,
        help_url=help_url,
        socket_url=socket_url,
        REACT_URL=react_url,
        elasticsearch_lang_indexes=config.get('elasticsearch_lang_indexes',
                                              'en fr'),
        first_login_after_auto_subscribe_to_notifications=
        first_login_after_auto_subscribe_to_notifications,
        sentry_dsn=config.get('sentry_dsn', ''),
        activate_tour=str(config.get('activate_tour') or False).lower(),
        providers=providers,
        providers_json=json.dumps(providers),
        translations=io.open(jedfilename, encoding='utf-8').read(),
        admin_email=admin_email,
        under_test=under_test)

    base.update({
        "opengraph_locale": get_opengraph_locale(request),
        "get_description": get_description(request),
        "get_landing_page_image": get_landing_page_image(),
        "private_social_sharing": private_social_sharing(),
        "get_topic": get_topic(request),
        "get_discussion_url": get_discussion_url(),
        "discussion_title": discussion_title(),
    })

    return base