def call(request): """Returns TwiML instructions to Twilio's POST requests""" response = twiml.Response() phone_number = request.POST.get('phoneNumber', '').lstrip('1') if phone_number: """If the browser sent a phoneNumber param, we know this request is an outgoing call from the pyphone""" for c in '()-': phone_number = phone_number.replace(c, '') phone_number = '+1' + phone_number direction = 'outgoing' with response.dial(callerId=settings.TWILIO_NUMBER) as r: r.number(request.POST['phoneNumber']) else: """Otherwise we assume this request is an incoming call.""" direction = 'incoming' with response.dial() as r: r.client('caller') phone_number = request.GET.get('From', '') try: contact = Contact.objects.get(number=phone_number) except ObjectDoesNotExist: contact = Contact(number=phone_number) contact.save() call = Call( contact=contact, direction=direction, ) call.save() return HttpResponse(str(response))
def test_call_queryset_is_all_calls(self): """Call view should show all texts.""" user1 = User() user1.save() self.client.force_login(user1) calls = self.client.get('/api/calls/') self.assertEqual(len(calls.json()), 0) jabba = Contact(name="Jabba", number="+12068675309") jabba.save() call1 = Call(contact=jabba, direction="outgoing", status="completed") call1.save() calls = self.client.get('/api/calls/') self.assertEqual(len(calls.json()), 1)
def test_call_queryset_returns_call_attributes_in_json(self): """Test that a call to the calls api contains call attributes.""" user1 = User() user1.save() self.client.force_login(user1) jabba = Contact(name="Jabba", number="+12068675309") jabba.save() call1 = Call(contact=jabba, direction="outgoing", status="completed") call1.save() calls = self.client.get('/api/calls/') self.assertTrue("direction" in calls.content.decode()) self.assertTrue("outgoing" in calls.content.decode()) self.assertTrue("time" in calls.content.decode()) self.assertTrue("status" in calls.content.decode()) self.assertTrue("contact" in calls.content.decode())
def call_process(request): # Get this request's context context = RequestContext(request) # Get objects called through the HTTP POST request if request.method == 'POST': # Get class id from the POST and associated Class object class_id = request.POST.get('class') requested_class = Class.objects.get(id=int(class_id)) # Construct a new Call object call_to_add = Call(teach=requested_class) call_to_add.save() return HttpResponse("Your tutor is being requested. You will get a text when you get a match.") return HttpResponse("Got here.")
def call_process(request): # Get this request's context context = RequestContext(request) # Get objects called through the HTTP POST request if request.method == 'POST': # Get class id from the POST and associated Class object class_id = request.POST.get('class') requested_class = Class.objects.get(id=int(class_id)) # Construct a new Call object call_to_add = Call(teach=requested_class) call_to_add.save() return HttpResponse( "Your tutor is being requested. You will get a text when you get a match." ) return HttpResponse("Got here.")
def post(self, request): serializer = self.serializer_class(data=request.data) if serializer.is_valid(): call = Call() call.save() try: call_start = serializer.save(call_id=call) except ValidationError as err: call.delete() return Response(data=err, status=status.HTTP_400_BAD_REQUEST) serializer = CallAfterStartSerializer({ 'call_id': call.pk, 'source': call_start.source, 'destination': call_start.destination, 'time': call_start.timestamp }) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_queryset_returns_multiple_calls_when_in_db(self): """Test that queryset returns all calls if there are many in db.""" user1 = User() user1.save() self.client.force_login(user1) calls = self.client.get('/api/calls/') self.assertEqual(len(calls.json()), 0) jabba = Contact(name="Jabba", number="+12068675309") jabba.save() call1 = Call(contact=jabba, direction="outgoing", status="completed") call1.save() han = Contact(name="Han", number="+15555555555") han.save() call2 = Call(contact=han, direction="incoming", status="busy") call2.save() nerfherder = Contact(name="Nerf Herder", number="+16666666666") nerfherder.save() call3 = Call(contact=nerfherder, direction="outgoing", status="completed") call3.save() calls = self.client.get('/api/calls/') self.assertEqual(len(calls.json()), 3)
call = Call.objects.get( event_num__iexact = row[ "EventNum" ].strip() ) action = "UPDATE" except: call = Call() action = "INSERT" call.description = row[ "Description" ] call.event_num = row[ "EventNum" ] call.report_num = row[ "ReportNum" ] call.response = row[ "Response" ] call.address = row[ "Address" ] call.zip_code = row[ "Zip" ] call.call_time = row[ "CallTime" ] call.call_type = call_type call.calltype_id = call_type.id call.beat = beat if ( row[ "Lng" ] is not None or row[ "Lng" ] != "" ) and ( row[ "Lat" ] is not None or row[ "Lng" ] != "" ): try: call.geom = fromstr('POINT(' + row[ "Lng"] + " " + row[ "Lat" ] +')', srid=4326) except: print "ERROR setting the geometry! for %s" % row[ "EventNum" ] call.jurisdiction = juri call.save() #print dir( call ) count +=1 print "%d: %s: %s" % (count, action, row["EventNum"])
print "using Yahoo lat/lng" yahoo_count += 1 new_call.geocoder = "yahoo" else: print "using google lat/lng" google_count += 1 new_call.geocoder = "google" if ( lng is not None or lng != "" ) and ( lat is not None or lng != "" ): try: new_call.geom = fromstr( 'POINT(' + str(lng) + " " + str(lat) +')', srid = 4326 ) except: print "ERROR! setting the lat/lng for this call" new_call.geocoder = None geocode_error_list.append( call[ "event_num" ] ) new_call.save() #it saved so we'll put it in our success dict success_list.append( call[ "event_num" ] ) except: error_list.append( call[ "event_num" ] ) bail() output = "=================================================" output += "\r Summary of Calls Imported " output += "\r" output += "\rTotal Calls Parsed: %d" % ( parse_row_count -2 ) output += "\rTotal Calls Inserted: %d" % len( success_list ) #output += "\rTotal Calls Updated: %d" % update_count output += "\rTotal Calls Skipped: %d" % skip_count output += "\rCalls geocoded using Google: %d" % google_count