def get_language_options(request, config=None): """Returns a list of information needed to generate the language menu.""" return [{ 'lang': lang, 'endonym': const.LANGUAGE_ENDONYMS.get(lang, '?'), 'url': utils.set_url_param(request.url, 'lang', lang) } for lang in (config and config.language_menu_options or ['en'])]
def get_language_option(request, lang, is_selected): return { 'lang': lang, 'endonym': const.LANGUAGE_ENDONYMS.get(lang, '?'), 'url': utils.set_url_param(request.url, 'lang', lang), 'is_selected': is_selected, }
def test_set_url_param(self): assert utils.set_url_param( 'http://example.com/server/', 'foo', 'bar') == \ 'http://example.com/server/?foo=bar' assert utils.set_url_param( 'http://example.com/server', 'foo', 'bar') == \ 'http://example.com/server?foo=bar' assert utils.set_url_param( 'http://example.com/server?foo=baz', 'foo', 'bar') == \ 'http://example.com/server?foo=bar' assert utils.set_url_param( 'http://example.com/server?foo=baz', 'foo', 'bar') == \ 'http://example.com/server?foo=bar' # Collapses multiple parameters assert utils.set_url_param( 'http://example.com/server?foo=baz&foo=baq', 'foo', 'bar') == \ 'http://example.com/server?foo=bar' assert utils.set_url_param( 'http://example.com/server?foo=baz&foo=baq', 'x', 'y') == \ 'http://example.com/server?foo=baq&x=y' # Unicode is properly converted assert utils.set_url_param( 'http://example.com/server?foo=bar', u'\u4f60\u597d', '\xe4\xbd\xa0\xe5\xa5\xbd') == \ 'http://example.com/server?foo=bar&' + \ '%E4%BD%A0%E5%A5%BD=%E4%BD%A0%E5%A5%BD'
def setup_env(request): """Constructs the 'env' object, which contains various template variables that are commonly used by most handlers.""" env = utils.Struct() env.repo, env.action = get_repo_and_action(request) env.config = config.Configuration(env.repo or '*') env.analytics_id = config.get('analytics_id') env.amp_gtm_id = config.get('amp_gtm_id') env.maps_api_key = config.get('maps_api_key') # Internationalization-related stuff. env.charset = select_charset(request) env.lang = select_lang(request, env.config) env.rtl = env.lang in const.LANGUAGES_BIDI env.virtual_keyboard_layout = const.VIRTUAL_KEYBOARD_LAYOUTS.get(env.lang) # Used for parsing query params. This must be done before accessing any # query params which may have multi-byte value, such as "given_name" below # in this function. request.charset = env.charset # Determine the resource bundle to use. env.default_resource_bundle = config.get('default_resource_bundle', '1') env.resource_bundle = (request.cookies.get('resource_bundle', '') or env.default_resource_bundle) # Information about the request. env.url = utils.set_url_param(request.url, 'lang', env.lang) env.scheme, env.netloc, env.path, _, _ = urlparse.urlsplit(request.url) env.force_https = False env.domain = env.netloc.split(':')[0] env.global_url = utils.get_repo_url(request, 'global') # Commonly used information that's rendered or localized for templates. env.language_options = get_language_options(request, env.config, env.lang) env.repo_options = get_repo_options(request, env.lang) env.expiry_options = [ utils.Struct(value=value, text=const.PERSON_EXPIRY_TEXT[value]) for value in sorted(const.PERSON_EXPIRY_TEXT.keys(), key=int) ] env.status_options = [ utils.Struct(value=value, text=const.NOTE_STATUS_TEXT[value]) for value in pfif.NOTE_STATUS_VALUES if (value != 'believed_dead' or not env.config or env.config.allow_believed_dead_via_ui) ] env.hidden_input_tags_for_preserved_query_params = ( get_hidden_input_tags_for_preserved_query_params(request)) ui_param = request.get('ui', '').strip().lower() # Interprets "small" and "style" parameters for backward compatibility. # TODO(ichikawa): Delete these in near future when we decide to drop # support of these parameters. small_param = request.get('small', '').strip().lower() style_param = request.get('style', '').strip().lower() if not ui_param and small_param == 'yes': ui_param = 'small' elif not ui_param and style_param: ui_param = style_param if ui_param: env.ui = ui_param elif user_agents.is_jp_tier2_mobile_phone(request): env.ui = 'light' else: env.ui = 'default' # UI configurations. # # Enables features which require JavaScript. env.enable_javascript = True # Enables operations which requires Captcha. env.enable_captcha = True # Enables photo upload. env.enable_photo_upload = True # Enables to flag/unflag notes as spam, and to reveal spam notes. env.enable_spam_ops = True # Enables duplicate marking mode. env.enable_dup_mode = True # Shows a logo on top of the page. env.show_logo = True # Shows language menu. env.show_language_menu = True # Uses short labels for buttons. env.use_short_buttons = False # Optional "target" attribute for links to non-small pages. env.target_attr = '' # Shows record IDs in the results page. env.show_record_ids_in_results = True # Shows non AMP HTML pages by default. env.amp = False if env.ui == 'small': env.show_logo = False env.target_attr = ' target="_blank" ' elif env.ui == 'light': # Disables features which requires JavaScript. Some feature phones # doesn't support JavaScript. env.enable_javascript = False # Disables operations which requires Captcha because Captcha requires # JavaScript. env.enable_captcha = False # Uploading is often not supported in feature phones. env.enable_photo_upload = False # Disables spam operations because it requires JavaScript and # supporting more pages on ui=light. env.enable_spam_ops = False # Disables duplicate marking mode because it doesn't support # small screens and it requires JavaScript. env.enable_dup_mode = False # Hides the logo on the top to save the space. Also, the logo links # to the global page which doesn't support small screens. env.show_logo = False # Hides language menu because the menu in the current position is # annoying in feature phones. # TODO(ichikawa): Consider layout of the language menu. env.show_language_menu = False # Too long buttons are not fully shown in some feature phones. env.use_short_buttons = True # To make it simple. env.show_record_ids_in_results = False env.back_chevron = u'\xab' back_chevron_in_charset = True try: env.back_chevron.encode(env.charset) except UnicodeEncodeError: # u'\xab' is not in the charset (e.g. Shift_JIS). back_chevron_in_charset = False if not back_chevron_in_charset or env.ui == 'light': # Use ASCII characters on ui=light too because some feature phones # support UTF-8 but don't render UTF-8 symbols such as u'\xab'. env.back_chevron = u'<<' env.enable_maps = (env.enable_javascript and not env.config.zero_rating_mode and env.maps_api_key) env.enable_analytics = (env.enable_javascript and not env.config.zero_rating_mode and env.analytics_id) env.enable_translate = (env.enable_javascript and not env.config.zero_rating_mode and env.config.translate_api_key) env.admin = AdminEnv(request) # Repo-specific information. if env.repo: # repo_url is the root URL for the repository. env.repo_url = utils.get_repo_url(request, env.repo) # start_url is like repo_url but preserves parameters such as 'ui'. env.start_url = utils.get_url(request, env.repo, '') # URL of the link in the heading. The link on ui=small links to the # normal UI. env.repo_title_url = (env.repo_url if env.ui == 'small' else env.start_url) # URL to force default UI. Note that we show ui=light version in some # user agents when ui parameter is not specified. env.default_ui_url = utils.get_url(request, env.repo, '', ui='default') env.repo_path = urlparse.urlsplit(env.repo_url)[2] env.repo_title = get_localized_message(env.config.repo_titles, env.lang, '?') env.start_page_custom_html = get_localized_message( env.config.start_page_custom_htmls, env.lang, '') env.results_page_custom_html = get_localized_message( env.config.results_page_custom_htmls, env.lang, '') env.view_page_custom_html = get_localized_message( env.config.view_page_custom_htmls, env.lang, '') env.seek_query_form_custom_html = get_localized_message( env.config.seek_query_form_custom_htmls, env.lang, '') env.footer_custom_html = get_localized_message( env.config.footer_custom_htmls, env.lang, '') # If the repository is deactivated, we should not show test mode # notification. env.repo_test_mode = (env.config.test_mode and not env.config.deactivated) env.force_https = env.config.force_https env.params_full_name = request.get('full_name', '').strip() if not env.params_full_name: # Preformat the name from 'given_name' and 'family_name' parameters. given_name = request.get('given_name', '').strip() family_name = request.get('family_name', '').strip() env.params_full_name = utils.get_full_name(given_name, family_name, env.config) return env
def setup_env(request): """Constructs the 'env' object, which contains various template variables that are commonly used by most handlers.""" env = utils.Struct() env.repo, env.action = get_repo_and_action(request) env.config = config.Configuration(env.repo or '*') # TODO(ryok): Rename to local_test_mode or something alike to disambiguate # better from repository's test_mode. env.test_mode = (request.remote_addr == '127.0.0.1' and request.get('test_mode')) # We sometimes want to disable analytics/maps for requests from a specific # mobile carrier (specified by IP ranges). # In this way, we can avoid requests to sites outside google.org, and # allow the carrier to zero-rate access to Person Finder. # TODO(ichikawa): Add server test for this feature. # # TODO(kpy): Make these global config settings and get rid of get_secret(). if utils.is_ip_address_in_one_of_networks( request.remote_addr, env.config.ip_networks_to_disable_analytics): env.analytics_id = None else: env.analytics_id = get_secret('analytics_id') if utils.is_ip_address_in_one_of_networks( request.remote_addr, env.config.ip_networks_to_disable_maps): env.maps_api_key = None else: env.maps_api_key = get_secret('maps_api_key') # Internationalization-related stuff. env.charset = select_charset(request) env.lang = select_lang(request, env.config) env.rtl = env.lang in django_setup.LANGUAGES_BIDI env.virtual_keyboard_layout = const.VIRTUAL_KEYBOARD_LAYOUTS.get(env.lang) # Used for parsing query params. This must be done before accessing any # query params which may have multi-byte value, such as "given_name" below # in this function. request.charset = env.charset # Determine the resource bundle to use. env.default_resource_bundle = config.get('default_resource_bundle', '1') env.resource_bundle = (request.cookies.get('resource_bundle', '') or env.default_resource_bundle) # Information about the request. env.url = utils.set_url_param(request.url, 'lang', env.lang) env.scheme, env.netloc, env.path, _, _ = urlparse.urlsplit(request.url) env.force_https = False env.domain = env.netloc.split(':')[0] env.global_url = utils.get_repo_url(request, 'global') # Commonly used information that's rendered or localized for templates. env.language_options = get_language_options(request, env.config) env.repo_options = get_repo_options(request, env.lang) env.expiry_options = [ utils.Struct(value=value, text=const.PERSON_EXPIRY_TEXT[value]) for value in sorted(const.PERSON_EXPIRY_TEXT.keys(), key=int) ] env.status_options = [ utils.Struct(value=value, text=const.NOTE_STATUS_TEXT[value]) for value in pfif.NOTE_STATUS_VALUES if (value != 'believed_dead' or not env.config or env.config.allow_believed_dead_via_ui) ] env.hidden_input_tags_for_preserved_query_params = ( get_hidden_input_tags_for_preserved_query_params(request)) ui_param = request.get('ui', '').strip().lower() # Interprets "small" and "style" parameters for backward compatibility. # TODO(ichikawa): Delete these in near future when we decide to drop # support of these parameters. small_param = request.get('small', '').strip().lower() style_param = request.get('style', '').strip().lower() if not ui_param and small_param == 'yes': ui_param = 'small' elif not ui_param and style_param: ui_param = style_param if ui_param: env.ui = ui_param elif user_agents.is_jp_tier2_mobile_phone(request): env.ui = 'light' else: env.ui = 'default' # UI configurations. # # Enables features which require JavaScript. env.enable_javascript = True # Enables operations which requires Captcha. env.enable_captcha = True # Enables photo upload. env.enable_photo_upload = True # Enables to flag/unflag notes as spam, and to reveal spam notes. env.enable_spam_ops = True # Enables duplicate marking mode. env.enable_dup_mode = True # Shows a logo on top of the page. env.show_logo = True # Shows language menu. env.show_language_menu = True # Uses short labels for buttons. env.use_short_buttons = False # Optional "target" attribute for links to non-small pages. env.target_attr = '' # Shows record IDs in the results page. env.show_record_ids_in_results = True if env.ui == 'small': env.show_logo = False env.target_attr = ' target="_blank" ' elif env.ui == 'light': # Disables features which requires JavaScript. Some feature phones # doesn't support JavaScript. env.enable_javascript = False # Disables operations which requires Captcha because Captcha requires # JavaScript. env.enable_captcha = False # Uploading is often not supported in feature phones. env.enable_photo_upload = False # Disables spam operations because it requires JavaScript and # supporting more pages on ui=light. env.enable_spam_ops = False # Disables duplicate marking mode because it doesn't support # small screens and it requires JavaScript. env.enable_dup_mode = False # Hides the logo on the top to save the space. Also, the logo links # to the global page which doesn't support small screens. env.show_logo = False # Hides language menu because the menu in the current position is # annoying in feature phones. # TODO(ichikawa): Consider layout of the language menu. env.show_language_menu = False # Too long buttons are not fully shown in some feature phones. env.use_short_buttons = True # To make it simple. env.show_record_ids_in_results = False env.back_chevron = u'\xab' back_chevron_in_charset = True try: env.back_chevron.encode(env.charset) except UnicodeEncodeError: # u'\xab' is not in the charset (e.g. Shift_JIS). back_chevron_in_charset = False if not back_chevron_in_charset or env.ui == 'light': # Use ASCII characters on ui=light too because some feature phones # support UTF-8 but don't render UTF-8 symbols such as u'\xab'. env.back_chevron = u'<<' # Repo-specific information. if env.repo: # repo_url is the root URL for the repository. env.repo_url = utils.get_repo_url(request, env.repo) # start_url is like repo_url but preserves parameters such as 'ui'. env.start_url = utils.get_url(request, env.repo, '') # URL of the link in the heading. The link on ui=small links to the # normal UI. env.repo_title_url = ( env.repo_url if env.ui == 'small' else env.start_url) # URL to force default UI. Note that we show ui=light version in some # user agents when ui parameter is not specified. env.default_ui_url = utils.get_url(request, env.repo, '', ui='default') env.repo_path = urlparse.urlsplit(env.repo_url)[2] env.repo_title = get_localized_message( env.config.repo_titles, env.lang, '?') env.start_page_custom_html = get_localized_message( env.config.start_page_custom_htmls, env.lang, '') env.results_page_custom_html = get_localized_message( env.config.results_page_custom_htmls, env.lang, '') env.view_page_custom_html = get_localized_message( env.config.view_page_custom_htmls, env.lang, '') env.seek_query_form_custom_html = get_localized_message( env.config.seek_query_form_custom_htmls, env.lang, '') env.footer_custom_html = get_localized_message( env.config.footer_custom_htmls, env.lang, '') # If the repository is deactivated, we should not show test mode # notification. env.repo_test_mode = ( env.config.test_mode and not env.config.deactivated) env.force_https = env.config.force_https env.params_full_name = request.get('full_name', '').strip() if not env.params_full_name: # Preformat the name from 'given_name' and 'family_name' parameters. given_name = request.get('given_name', '').strip() family_name = request.get('family_name', '').strip() env.params_full_name = utils.get_full_name( given_name, family_name, env.config) return env
def get_language_options(request, config=None): """Returns a list of information needed to generate the language menu.""" return [{'lang': lang, 'endonym': const.LANGUAGE_ENDONYMS.get(lang, '?'), 'url': utils.set_url_param(request.url, 'lang', lang)} for lang in (config and config.language_menu_options or ['en'])]