def simulate_app(request): c = { 'zoom_override':18, 'sim_mode':True, } #TODO proper form if request.method == "POST": if 'message' in request.POST and 'longitude' in request.POST and 'latitude' in request.POST: if request.POST['message'] != "": admin, created = Client.get_or_register_client("admin") wl1 = WorldLayer.objects.get(id=1) Totem.add_totem(admin,request.POST['longitude'],request.POST['latitude'],request.POST['message'],wl1) return render_to_response("custom_admin/simulator/map.html",c,context_instance=RequestContext(request))
def clients_list(request,sort_param=None): if sort_param: sort_param = sort_param else: sort_param = "last_activity" clients = Client.objects.all().order_by('-'+str(sort_param)) c = { 'clients':clients } for client in clients: client.x_num_messages = TotemMessage.get_message_count_by_client(client) client.x_num_totems = Totem.get_totem_count_by_client(client) return render_to_response("custom_admin/clients/list.html",c,context_instance=RequestContext(request))
def add_totem(request): if request.method == "POST": output = {} required_params = [ 'device_id', 'longitude', 'latitude', 'message', 'worldlayer_id', 'timestamp', 'secret', ] # all parameters must be sent for param in required_params: if param not in request.POST.keys(): output['success'] = False output['alert'] = "missing parameters" return HttpResponse(simplejson.dumps(output), 'application/json') # device id must be non-empty if request.POST['device_id'] == "": output['success'] = False output['alert'] = "empty device id" return HttpResponse(simplejson.dumps(output), 'application/json') # cannot send empty message if request.POST['message'] == "": output['success'] = False output['alert'] = "empty message" return HttpResponse(simplejson.dumps(output), 'application/json') # authorize secret if _auth_secret([ request.POST['device_id'], request.POST['longitude'], request.POST['latitude'], request.POST['timestamp']], request.POST['secret']) == False: output['success'] = False output['alert'] = "malformed request" return HttpResponse(simplejson.dumps(output), 'application/json') # client must exist in system and be registered try: client = Client.objects.get(device_id=request.POST['device_id']) except: output['success'] = False output['alert'] = "unknown client" return HttpResponse(simplejson.dumps(output), 'application/json') # make sure worldlayer exists try: worldlayer = WorldLayer.objects.get(id=request.POST['worldlayer_id']) except: output['success'] = False output['alert'] = "unknown world layer" return HttpResponse(simplejson.dumps(output), 'application/json') Totem.add_totem( client, request.POST['longitude'], request.POST['latitude'], request.POST['message'], worldlayer ) output['success'] = True print output return HttpResponse(simplejson.dumps(output), 'application/json') else: raise Http404