Example #1
0
def query(request):
    """
    Parses and returns event request
    """
    # handle both: HTTP POST and HTTP GET variables
    params = parse_query_parameters(QUERY_PARAMETERS,
                                    getattr(request, request.method))

    # A returned string is interpreted as an error message.
    if isinstance(params, str):
        return _error(request, params, status_code=400)

    if params.get("starttime") and params.get("endtime") and (
            params.get("endtime") <= params.get("starttime")):
        return _error(request, 'Start time must be before end time')

    if params.get("latitude") is not None or \
            params.get("longitude") is not None or \
            params.get("maxradius") is not None:
        if params.get("longitude") is None:
            return _error(request, "'longitude' must also be given for "
                          "radial queries.",
                          status_code=400)
        if params.get("latitude") is None:
            return _error(request, "'latitude' must also be given for "
                          "radial queries.",
                          status_code=400)
        if params.get("maxradius") is None:
            return _error(request, "'maxradius' must also be given for "
                          "radial queries.",
                          status_code=400)

    if params.get("nodata") not in [204, 404]:
        return _error(request,
                      "nodata must be '204' or '404'.",
                      status_code=400)

    if params.get("format") not in ["xml", "text"]:
        return _error(request,
                      "format must be 'xml' or 'text'.",
                      status_code=400)

    if params["format"] == "xml":
        content_type = "text/xml"
    elif params["format"] == "text":
        content_type = "text"
    else:
        raise NotImplementedError

    with io.BytesIO() as fh:
        status = query_event(fh, **params)
        fh.seek(0, 0)

        if status == 200:
            response = HttpResponse(fh, content_type=content_type)
            return response
        else:
            msg = 'Not Found: No data selected'
            return _error(request, msg, status)
Example #2
0
def query(request):
    """
    Parses and returns event request
    """
    # handle both: HTTP POST and HTTP GET variables
    params = parse_query_parameters(QUERY_PARAMETERS,
                                    getattr(request, request.method))

    # A returned string is interpreted as an error message.
    if isinstance(params, str):
        return _error(request, params, status_code=400)

    if params.get("starttime") and params.get("endtime") and (
            params.get("endtime") <= params.get("starttime")):
        return _error(request, 'Start time must be before end time')

    if params.get("latitude") is not None or \
            params.get("longitude") is not None or \
            params.get("maxradius") is not None:
        if params.get("longitude") is None:
            return _error(request, "'longitude' must also be given for "
                                   "radial queries.", status_code=400)
        if params.get("latitude") is None:
            return _error(request, "'latitude' must also be given for "
                                   "radial queries.", status_code=400)
        if params.get("maxradius") is None:
            return _error(request, "'maxradius' must also be given for "
                                   "radial queries.", status_code=400)

    if params.get("nodata") not in [204, 404]:
        return _error(request, "nodata must be '204' or '404'.",
                      status_code=400)

    if params.get("format") not in ["xml", "text"]:
        return _error(request, "format must be 'xml' or 'text'.",
                      status_code=400)

    if params["format"] == "xml":
        content_type = "text/xml"
    elif params["format"] == "text":
        content_type = "text"
    else:
        raise NotImplementedError

    with io.BytesIO() as fh:
        status = query_event(fh, **params)
        fh.seek(0, 0)

        if status == 200:
            response = HttpResponse(fh, content_type=content_type)
            return response
        else:
            msg = 'Not Found: No data selected'
            return _error(request, msg, status)
Example #3
0
def query(request):
    """
    Parses and returns event request
    """
    # handle both: HTTP POST and HTTP GET variables
    params = parse_query_parameters(QUERY_PARAMETERS,
                                    getattr(request, request.method))

    # A returned string is interpreted as an error message.
    if isinstance(params, str):
        return _error(request, params, status_code=400)

    if params.get("starttime") and params.get("endtime") and (
            params.get("endtime") <= params.get("starttime")):
        return _error(request, 'Start time must be before end time')

    if params.get("latitude") is not None or \
            params.get("longitude") is not None or \
            params.get("maxradius") is not None:
        if params.get("longitude") is None:
            return _error(request, "'longitude' must also be given for "
                          "radial queries.",
                          status_code=400)
        if params.get("latitude") is None:
            return _error(request, "'latitude' must also be given for "
                          "radial queries.",
                          status_code=400)
        if params.get("maxradius") is None:
            return _error(request, "'maxradius' must also be given for "
                          "radial queries.",
                          status_code=400)

    if params.get("nodata") not in [204, 404]:
        return _error(request,
                      "nodata must be '204' or '404'.",
                      status_code=400)

    format = params.get("format").lower()
    if format not in ("xml", "text"):
        return _error(request,
                      "format must be 'xml' or 'text'.",
                      status_code=400)
    params["format"] = format

    if params["format"] == "text" and params.get("level") == "response":
        return _error(request, "format='text' is not compatible with "
                      "level='response'",
                      status_code=400)

    if params["format"] == "xml":
        content_type = "text/xml"
    elif params["format"] == "text":
        content_type = "text"
    else:
        raise NotImplementedError

    if params.get("level") not in [
            "network", "station", "channel", "response"
    ]:
        return _error(request, "level must be 'network', 'station', "
                      "'channel', or 'response'",
                      status_code=400)

    for key in ["network", "station", "location", "channel"]:
        if key not in params:
            continue
        params[key] = [
            _i.strip().upper()
            for _i in params[key].replace(' ', '').split(',')
        ]
    if "location" in params:
        params["location"] = [
            _i.replace('--', '') for _i in params["location"]
        ]

    # Get the url to put it into the StationXML file.
    url = request.build_absolute_uri(request.get_full_path())

    # user
    if request.user.is_authenticated():
        user = request.user
    else:
        user = None

    with io.BytesIO() as fh:
        status = query_stations(fh, url=url, user=user, **params)
        fh.seek(0, 0)

        if status == 200:
            response = HttpResponse(fh, content_type=content_type)
            return response
        else:
            msg = 'Not Found: No data selected'
            return _error(request, msg, status)
Example #4
0
def query(request):
    """
    Parses and returns event request
    """
    # handle both: HTTP POST and HTTP GET variables
    params = parse_query_parameters(QUERY_PARAMETERS,
                                    getattr(request, request.method))

    # A returned string is interpreted as an error message.
    if isinstance(params, str):
        return _error(request, params, status_code=400)

    if params.get("starttime") and params.get("endtime") and (
            params.get("endtime") <= params.get("starttime")):
        return _error(request, 'Start time must be before end time')

    if params.get("latitude") is not None or \
            params.get("longitude") is not None or \
            params.get("maxradius") is not None:
        if params.get("longitude") is None:
            return _error(request, "'longitude' must also be given for "
                                   "radial queries.", status_code=400)
        if params.get("latitude") is None:
            return _error(request, "'latitude' must also be given for "
                                   "radial queries.", status_code=400)
        if params.get("maxradius") is None:
            return _error(request, "'maxradius' must also be given for "
                                   "radial queries.", status_code=400)

    if params.get("nodata") not in [204, 404]:
        return _error(request, "nodata must be '204' or '404'.",
                      status_code=400)

    format = params.get("format").lower()
    if format not in ("xml", "text"):
        return _error(request, "format must be 'xml' or 'text'.",
                      status_code=400)
    params["format"] = format

    if params["format"] == "text" and params.get("level") == "response":
        return _error(request, "format='text' is not compatible with "
                               "level='response'", status_code=400)

    if params["format"] == "xml":
        content_type = "text/xml"
    elif params["format"] == "text":
        content_type = "text"
    else:
        raise NotImplementedError

    if params.get("level") not in ["network", "station", "channel",
                                   "response"]:
        return _error(request, "level must be 'network', 'station', "
                               "'channel', or 'response'", status_code=400)

    for key in ["network", "station", "location", "channel"]:
        if key not in params:
            continue
        params[key] = [_i.strip().upper() for _i in
                       params[key].replace(' ', '').split(',')]
    if "location" in params:
        params["location"] = [_i.replace('--', '')
                              for _i in params["location"]]

    # Get the url to put it into the StationXML file.
    url = request.build_absolute_uri(request.get_full_path())

    # user
    if request.user.is_authenticated():
        user = request.user
    else:
        user = None

    with io.BytesIO() as fh:
        status = query_stations(fh, url=url, user=user, **params)
        fh.seek(0, 0)

        if status == 200:
            response = HttpResponse(fh, content_type=content_type)
            return response
        else:
            msg = 'Not Found: No data selected'
            return _error(request, msg, status)