def json(request): # if request.is_ajax(): print >>sys.stderr,'this!' # if request.method == 'POST': json_data = simplejson.loads(request.raw_post_data) print >> sys.stderr, type(request.raw_post_data) print >>sys.stderr, type(json_data) # json_data = simplejson.dumps(json_data) print >>sys.stderr, type(json_data) try: print >>sys.stderr, "json baby" # print >>sys.stderr, json_data.get("ID") # liss = json_data.values() # lisst = list(liss) # for j in json_data: j = ast.literal_eval(json_data) print >>sys.stderr, type(j) print >>sys.stderr, type(json_data) car_id = j['ID'] cars_pass = j['cars_passed'] cars_jamm = j['cars_jammed'] cars_quant = j['cars_quantity'] # cars_quant = liss[4] st_date = j['start_date'] # st_date = liss[2] nd_date = j['end_date'] # nd_date = liss[1] cars = cars_passed(ID=car_id, cars_passed=cars_pass, cars_jammed=cars_jamm, cars_quantity=cars_quant, start_date=st_date, end_date=nd_date) cars.save() except KeyError: HttpResponseServerError("Malformed Data") HttpResponse("Got Json") # with open('./this.json', 'w') as out: # JSONSerializer = serializers.get_serializer("json") # json_serializer = JSONSerializer() # json_serializer.serialize(request.raw_post_data, stream=out) return HttpResponse('Raw Data: "%s"' % request.raw_post_data) print 'done' # print 'Raw Data: "%s"'% request.raw_post_data print >>sys.stderr, "hola" return HttpResponse("cool")
def get_json(request): try: car = cars_passed.objects.latest('ID') except: print >>sys.stderr, "No cars registered, creating it...." st_date = datetime.datetime.now() nd_date = datetime.datetime.now() cars = cars_passed(ID=0, cars_passed=0, cars_jammed=0, cars_quantity=0, start_date=st_date, end_date=nd_date) cars.save() car = cars_passed.objects.latest('ID') lis = { "ID" : car.ID, "cars_passed" :car.cars_passed, "cars_jammed": car.cars_jammed, "cars_quantity": car.cars_quantity, "start_data": car.start_date.strftime("%Y-%m-%d %H:%M:%S"), "end_date": car.end_date.strftime("%Y-%m-%d %H:%M:%S"), } return HttpResponse(simplejson.dumps(lis) , mimetype='application/json')