Ejemplo n.º 1
0
Archivo: views.py Proyecto: 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")
Ejemplo n.º 2
0
def tableau(request):
    """ Implement the "/geo/api/location/tableau" API call.
    """
    # 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 use the cached JSON data directly.

    try:
        cacheEntry = TableauCacheEntry.objects.get(tableau=tableau,
                                                   width=width, height=height)
    except TableauCacheEntry.DoesNotExist:
        cacheEntry = None

    if cacheEntry != None:
        results = cacheEntry.json_data

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

    if cacheEntry == 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"))

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

        # Add the generated tableau into the cache so that we can use it next
        # time.

#        cacheEntry = TableauCacheEntry()
#        cacheEntry.tableau   = tableau
#        cacheEntry.width     = width
#        cacheEntry.height    = height
#        cacheEntry.json_data = results
#        cacheEntry.save()
    
    # Finally, return the results back to the caller.

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

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