Ejemplo n.º 1
0
def admin_ui(request: PyramidRequest):
    admin_resource = get_admin(request.root)
    api_info_url = request.resource_url(
        admin_resource['api_info.json']).rstrip('/')
    admin_params = [f'"apiInfoUrl": "{api_info_url}"']

    auth_provider = http_client = ''
    ui_settings = get_admin_ui_settings(request.registry)
    if ui_settings.auth_provider:
        auth_provider = ui_settings.auth_provider.code
        admin_params.append(
            f'"getAuthProvider": {ui_settings.auth_provider.name}')
    if ui_settings.http_client:
        http_client = ui_settings.http_client.code
        admin_params.append(f'"getHttpClient": {ui_settings.http_client.name}')

    admin_params = ','.join(admin_params)
    admin_ui_params = TEMPLATE.format(
        auth_provider=auth_provider,
        http_client=http_client,
        admin_params=f'{{{admin_params}}}',
    )
    html = ui_settings.index_html_tpl.replace('__ADMIN_PARAMS__',
                                              admin_ui_params)
    response = HTTPOk(content_type='text/html', conditional_response=True)
    response.text = html
    response.md5_etag()
    return response
Ejemplo n.º 2
0
def country_view(request):
    client_addr = request.client_addr
    geoip_db = request.registry.geoip_db

    if request.body in EMPTY and client_addr and geoip_db is not None:
        # Optimize common case of geoip-only request
        country = geoip_db.country_lookup(client_addr)
        if country:
            result = HTTPOk()
            result.content_type = 'application/json'
            result.text = '{"country_code": "%s", "country_name": "%s"}' % (
                country.code, country.name)
            return result
        else:
            result = HTTPNotFound()
            result.content_type = 'application/json'
            result.body = NOT_FOUND
            return result

    data, errors = preprocess_request(
        request,
        schema=GeoLocateSchema(),
        response=JSONParseError,
        accept_empty=True,
    )
    data = map_data(data, client_addr=client_addr)

    session = request.db_slave_session
    result = CountrySearcher(
        {
            'geoip': geoip_db,
            'session': session
        },
        api_key_log=False,
        api_key_name=None,
        api_name='country',
    ).search(data)

    if not result:
        result = HTTPNotFound()
        result.content_type = 'application/json'
        result.body = NOT_FOUND
        return result

    return result