Exemplo n.º 1
0
def place(place_dcid=None):
    redirect_args = dict(flask.request.args)
    should_redirect = False
    if 'topic' in flask.request.args:
        redirect_args['category'] = flask.request.args.get('topic', '')
        del redirect_args['topic']
        should_redirect = True

    category = redirect_args.get('category', None)
    if category in CATEGORY_REDIRECTS:
        redirect_args['category'] = CATEGORY_REDIRECTS[category]
        should_redirect = True

    if should_redirect:
        redirect_args['place_dcid'] = place_dcid
        return flask.redirect(flask.url_for('place.place', **redirect_args))

    dcid = flask.request.args.get('dcid', None)
    if dcid:
        # Traffic from "explore more" in Search. Forward along all parameters,
        # except for dcid, to the new URL format.
        redirect_args = dict(flask.request.args)
        redirect_args['place_dcid'] = dcid
        del redirect_args['dcid']
        redirect_args['category'] = category
        url = flask.url_for('place.place',
                            **redirect_args,
                            _external=True,
                            _scheme=current_app.config.get('SCHEME', 'https'))
        return flask.redirect(url)

    if not place_dcid:
        # Use display names (including state, if applicable) for the static page
        place_names = place_api.get_display_name(
            '^'.join(_PLACE_LANDING_DCIDS), g.locale)
        return flask.render_template(
            'place_landing.html',
            place_names=place_names,
            maps_api_key=current_app.config['MAPS_API_KEY'])

    place_type = place_api.get_place_type(place_dcid)
    place_names = place_api.get_i18n_name([place_dcid])
    if place_names and place_names.get(place_dcid):
        place_name = place_names[place_dcid]
    else:
        place_name = place_dcid
    return flask.render_template(
        'place.html',
        place_type=place_type,
        place_name=place_name,
        place_dcid=place_dcid,
        category=category if category else '',
        maps_api_key=current_app.config['MAPS_API_KEY'])
Exemplo n.º 2
0
def topic_page(topic_id=None, place_dcid=None):
    topics_summary = json.dumps(current_app.config['TOPIC_PAGE_SUMMARY'])
    # Redirect to the landing page.
    if not place_dcid and not topic_id:
        return flask.render_template('topic_page_landing.html')

    all_configs = current_app.config['TOPIC_PAGE_CONFIG']
    if os.environ.get('FLASK_ENV') == 'local':
        all_configs = libutil.get_topic_page_config()
    topic_configs = all_configs.get(topic_id, [])
    if len(topic_configs) < 1:
        return "Error: no config found"

    if not place_dcid:
        return flask.render_template(
            'topic_page.html',
            place_type="",
            place_name="",
            place_dcid="",
            topic_id=topic_id,
            topic_name=topic_configs[0].metadata.topic_name or "",
            config={},
            topics_summary=topics_summary)

    # Find the config for the topic & place.
    topic_place_config = None
    for config in topic_configs:
        if place_dcid in config.metadata.place_dcid:
            topic_place_config = config
            break
    if not topic_place_config:
        return "Error: no config found"

    # TODO: should use place metadata API to fetch these data in one call.
    place_type = place_api.get_place_type(place_dcid)
    place_names = place_api.get_i18n_name([place_dcid])
    if place_names:
        place_name = place_names[place_dcid]
    else:
        place_name = place_dcid
    return flask.render_template(
        'topic_page.html',
        place_type=place_type,
        place_name=place_name,
        place_dcid=place_dcid,
        topic_id=topic_id,
        topic_name=topic_place_config.metadata.topic_name or "",
        config=MessageToJson(topic_place_config),
        topics_summary=topics_summary)
Exemplo n.º 3
0
def ranking(stat_var, place_type, place_dcid=''):
    place_name = ''
    if place_dcid:
        place_names = place_api.get_i18n_name([place_dcid])
        place_name = place_names[place_dcid]
        if place_name == '':
            place_name = place_dcid
    else:
        place_name = 'the World'
    per_capita = flask.request.args.get('pc', False) != False
    return flask.render_template('ranking.html',
                                 place_name=place_name,
                                 place_dcid=place_dcid,
                                 place_type=place_type,
                                 per_capita=per_capita,
                                 stat_var=stat_var)
Exemplo n.º 4
0
def get_i18n_all_child_places(raw_page_data):
    all_child_places = raw_page_data.get('allChildPlaces', {})
    all_dcids = []
    for place_type in list(all_child_places.keys()):
        for place in all_child_places[place_type]['places']:
            all_dcids.append(place.get('dcid', ''))
    i18n_names = place_api.get_i18n_name(all_dcids,
                                         False)  # Don't resolve en-only names
    for place_type in list(all_child_places.keys()):
        for place in all_child_places[place_type]['places']:
            dcid = place.get('dcid')
            i18n_name = i18n_names.get(dcid, '')
            if i18n_name:
                place['name'] = i18n_name
    for place_type in list(all_child_places.keys()):
        all_child_places[place_type] = all_child_places[place_type]['places']
    return all_child_places
Exemplo n.º 5
0
def place(place_dcid=None):
    dcid = flask.request.args.get('dcid', None)
    topic = flask.request.args.get('topic', None)
    if dcid:
        # Traffic from "explore more" in Search. Forward along all parameters,
        # except for dcid, to the new URL format.
        redirect_args = dict(flask.request.args)
        redirect_args['place_dcid'] = dcid
        del redirect_args['dcid']
        redirect_args['topic'] = topic
        url = flask.url_for('place.place',
                            **redirect_args,
                            _external=True,
                            _scheme=current_app.config.get('SCHEME', 'https'))
        return flask.redirect(url)

    if not place_dcid:
        # Use display names (including state, if applicable) for the static page
        place_names = place_api.get_display_name(
            '^'.join(_PLACE_LANDING_DCIDS), g.locale)
        return flask.render_template(
            'place_landing.html',
            place_names=place_names,
            maps_api_key=current_app.config['MAPS_API_KEY'])

    place_type = place_api.get_place_type(place_dcid)
    place_names = place_api.get_i18n_name([place_dcid])
    if place_names:
        place_name = place_names[place_dcid]
    else:
        place_name = place_dcid
    return flask.render_template(
        'place.html',
        place_type=place_type,
        place_name=place_name,
        place_dcid=place_dcid,
        topic=topic if topic else '',
        maps_api_key=current_app.config['MAPS_API_KEY'])