Пример #1
0
Файл: views.py Проект: 3taps/geo
def main(request):
    """ Respond to the "/" URL.

        This is the main entry point for the tableau_api application.
    """
    # Extract our CGI parameters.

    location = get_param(request, "location")
    if location == None:
        return error_response(code=400,
                              message="Missing 'location' parameter.",
                              callback=get_param(request, "callback"))

    name = get_param(request, "name")

    ok = True # initially.
    width = get_param(request, "width")
    if width == None:
        ok = False
    else:
        try:
            width = int(width)
        except ValueError:
            ok = False

    if ok:
        if width < 10 or width > 2000:
            ok = False

    if not ok:
        return error_response(code=400,
                              message="Missing or invalid 'width' parameter.",
                              callback=get_param(request, "callback"))

    ok = True # initially.
    height = get_param(request, "height")
    if height == None:
        ok = False
    else:
        try:
            height = int(height)
        except ValueError:
            ok = False

    if ok:
        if height < 10 or height > 2000:
            ok = False

    if not ok:
        return error_response(code=400,
                              message="Missing or invalid 'height' parameter.",
                              callback=get_param(request, "callback"))

    # See which tableau we need to generate.

    tableau = tableauGenerator.select_tableau(location, name)
    if tableau == None:
        return error_response(code=500, message="No tableau found.",
                              callback=get_param(request, "callback"))

    # See if this tableau already exists in the tableau cache for the requested
    # size.  If so, we can use the cached JSON data directly.

    if tableauCache != None:
        json_data = tableauCache.get(tableau.id, width, height)
    else:
        json_data = None # No cache module -> nothing is cached.

    # If we didn't have a cache entry for this tableau and size, we have to
    # generate it.

    if json_data == None:
        try:
            generated_tableau = tableauGenerator.generate_tableau(tableau,
                                                                  width,
                                                                  height)
        except:
            # Something went wrong.  Print the traceback into the Django log
            # and return an error message back to the caller.
            traceback.print_exc()
            return error_response(code=500, message="Internal server error.",
                                  callback=get_param(request, "callback"))

        json_data = json.dumps({'tableau' : generated_tableau})

        # If we have a tableau cache, save the generated tableau into the cache
        # so that we can retrieve it quickly next time.

        if tableauCache != None:
            tableauCache.save(tableau.id, width, height, json_data)

    # Finally, return the results back to the caller.

    callback = get_param(request, "callback")
    if callback != None:
        json_data = callback + "(" + json_data + ")" # Add JSONP callback.

    return HttpResponse(json_data, mimetype="application/json")
Пример #2
0
Файл: views.py Проект: 3taps/geo
def get(request):
    """ Respond to the "/get" URL.

        This API call retrieves a generated tableau from the cache.

        The "/get" API call expects the following (GET or POST) parameters:

            "loc_code"

                The 3taps location code for the location we want to generate a
                tableau for.

            "name"

                The name of the tableau to generate.

            "width", "height"

                The pixel width and height of the tableau to generate.

        If the given tableau is in the cache, we return the cached tableau.
        Otherwise, we generate the tableau on-the-fly, add it to the cache, and
        return it back to the caller.

        Upon completion, we return a JSON-format object with the following
        entries:

            'tableau'

                The cached (or generated) tableau.

            'error'

                If an error occurred, this will be an object with 'code' and
                'message' fields describing what went wrong.
    """
    # Extract our parameters.

    if request.method == "GET":
        params = request.GET
    else:
        params = request.POST

    if "loc_code" not in params:
        return HttpResponse("Missing required 'loc_code' parameter.",
                            mimetype="text/plain")
    else:
        loc_code = params['loc_code']

    if "name" not in params:
        name = "DEFAULT"
    else:
        name = params['name']

    if "width" not in params:
        return HttpResponse("Missing required 'width' parameter.",
                            mimetype="text/plain")
    else:
        try:
            width = int(params['width'])
        except ValueError:
            return HttpResponse("Invalid 'width' parameter.",
                                mimetype="text/plain")

    if "height" not in params:
        return HttpResponse("Missing required 'height' parameter.",
                            mimetype="text/plain")
    else:
        try:
            height = int(params['height'])
        except ValueError:
            return HttpResponse("Invalid 'height' parameter.",
                                mimetype="text/plain")

    # See which tableau the caller is requesting.

    tableau = tableauGenerator.select_tableau(loc_code, name)
    if tableau == None:
        return HttpResponse("There is no tableau for this location.",
                            mimetype="text/plain")

    # If the requested tableau is in the cache, return it back to the caller.

    json_data = tableauCache.get(tableau.id, width, height)
    if json_data != None:
        callback = params.get("callback")
        if callback != None:
            json_data = callback + "(" + json_data + ")" # Add JSONP callback.
        return HttpResponse(json_data, mimetype="application/json")

    # If we get here, the requested tableau isn't in the cache.  Generate it
    # on-the-fly.

    try:
        generated_tableau = tableauGenerator.generate_tableau(tableau,
                                                              width,
                                                              height)
    except:
        # Something went wrong.  Print the traceback into the Django log and
        # return an error back to the caller.
        traceback.print_exc()
        return HttpResponse("Internal exception while generating tableau.",
                            mimetype="text/plain")

    json_data = json.dumps({'tableau' : generated_tableau})

    # Save the generated tableau into the cache.

    tableauCache.save(tableau.id, width, height, json_data)

    # Finally, return the newly-generated tableau back to the caller.

    return HttpResponse(json_data, mimetype="application/json")