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")
def generate(request): """ Respond to the "/generate" URL. This is an API call which is called by our daemon process to generate and cache a single tableau. The "/generate" 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. Upon completion, we return a single-line string indicating the success or otherwise of the request. If the tableau was successfully generated, we return the string "OK". """ # 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: return HttpResponse("Missing required 'name' parameter.", mimetype="text/plain") 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") # Ask the tableau generator to select the tableau to use. tableau = tableauGenerator.select_tableau(loc_code, name) if tableau == None: return HttpResponse("There is no tableau for this location.", mimetype="text/plain") # Generate the tableau. 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, tell the caller the good news. return HttpResponse("OK", mimetype="text/plain")