示例#1
0
def _get_details(request):
    params = request.query_params
    catalog = params.get('catalog')

    if not catalog:
        raise ValidationError({'error': 'Required argument: catalog'})

    if catalog not in CATALOGS:
        raise ValidationError({
            'error':
            'Catalog must be one of: {}'.format(', '.join(CATALOGS.keys()))
        })

    details = CATALOGS[catalog]['details']

    if not details:
        raise NotFound({'error': 'No details endpoint for {}'.format(catalog)})

    details_kwargs = {
        'wsdl': params.get('wsdl'),
        'site': params.get('site'),
    }

    try:
        return details(**details_kwargs)
    except ValueError as ex:
        raise ParseError(ex.message)
示例#2
0
def _do_search(request):
    params = request.data
    catalog = params.get('catalog')
    page = int(params.get('page', 1))
    request_uri = request.build_absolute_uri()

    if not catalog:
        raise ValidationError({'error': 'Required argument: catalog'})

    if catalog not in CATALOGS:
        raise ValidationError({
            'error':
            'Catalog must be one of: {}'.format(', '.join(CATALOGS.keys()))
        })

    # Store geojson to pass in search kwargs
    geojson = json.dumps(params.get('geom'))
    # Use a proper GEOS shape and calculate the bbox
    aoi = GEOSGeometry(geojson)
    bbox = get_bounds(aoi)

    search_kwargs = {
        'query': params.get('query'),
        'to_date': parse_date(params.get('to_date')),
        'from_date': parse_date(params.get('from_date')),
        'bbox': bbox,
        'geojson': geojson,
        'options': params.get('options', ''),
        'page': page,
    }

    search = CATALOGS[catalog]['search']
    serializer = CATALOGS[catalog]['serializer']
    is_pageable = CATALOGS[catalog]['is_pageable']

    try:
        results = search(**search_kwargs)

        filtered_results, cnt = filter_results(results, aoi, is_pageable)
        results.results = filtered_results
        results.count = cnt

        result = ResourceListSerializer(results,
                                        context={
                                            'page': page,
                                            'is_pageable': is_pageable,
                                            'request_uri': request_uri,
                                            'serializer': serializer
                                        })

        return [result.data]
    except ValueError as ex:
        raise ParseError(ex.message)