def children(request): """ Implement the "/geo/api/location/children" API call. """ loc_code = get_param(request, "location") if loc_code != None: # We've been asked to generate a list of all children of the given # location. try: location = Location.objects.get(code=loc_code) except Location.DoesNotExist: return error_response(code=400, message="Missing or invalid location code", callback=get_param(request, "callback")) children = [] for child in location.children.all(): children.append(child.code) results = json.dumps({'children' : children}) callback = get_param(request, "callback") if callback != None: results = callback + "(" + results + ")" # Add JSONP callback. return HttpResponse(results, mimetype="application/json") # If we get here, we don't have a parent location -> assemble a list of all # the top-level (country) locations. countryLevel = Level.objects.get(level=1) countries = [] for country in Location.objects.filter(level=countryLevel): countries.append(country.code) results = json.dumps({'children' : countries}) callback = get_param(request, "callback") if callback != None: results = callback + "(" + results + ")" # Add JSONP callback. return HttpResponse(results, mimetype="application/json")
def parents(request): """ Implement the "parents" API call. """ loc_code = get_param(request, "location") try: location = Location.objects.get(code=loc_code) except Location.DoesNotExist: return error_response(code=400, message="Missing or invalid location code", callback=get_param(request, "callback")) parents = [] for parent in location.parents.all(): parents.append(parent.code) results = json.dumps({'parents' : parents}) callback = get_param(request, "callback") if callback != None: results = callback + "(" + results + ")" # Add JSONP callback. return HttpResponse(results, mimetype="application/json")
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")