def dummy_test_answerurl(request_uuid): """This task triggers a call to local answer This is used for test purposes to simulate the behavior of Plivo **Attributes**: * ``RequestUUID`` - A unique identifier for API request.""" logger = dummy_test_answerurl.get_logger() logger.info("TASK :: dummy_testcall") logger = dummy_test_answerurl.get_logger() logger.debug("Executing task id %r, args: %r kwargs: %r" % \ (dummy_test_answerurl.request.id, dummy_test_answerurl.request.args, dummy_test_answerurl.request.kwargs)) logger.info("Waiting 1 seconds...") sleep(1) #find Callrequest try: obj_callrequest = Callrequest.objects.get(request_uuid=request_uuid) except: sleep(1) obj_callrequest = Callrequest.objects.get(request_uuid=request_uuid) #Update CallRequest obj_callrequest.status = 4 # SUCCESS obj_callrequest.save() #Create CDR new_voipcall = VoIPCall(user=obj_callrequest.user, request_uuid=obj_callrequest.request_uuid, callrequest=obj_callrequest, callid='', callerid=obj_callrequest.callerid, phone_number=obj_callrequest.phone_number, duration=0, billsec=0, disposition=1) new_voipcall.save() #lock to limit running process, do so per campaign #http://ask.github.com/celery/cookbook/tasks.html return True
class DialerCdrModel(TestCase): """Test Callrequest, VoIPCall models""" fixtures = ['auth_user.json', 'gateway.json', 'dialer_setting.json', 'user_profile.json', 'phonebook.json', 'contact.json', 'dnc_list.json', 'dnc_contact.json', 'survey.json', 'campaign.json', 'subscriber.json', 'callrequest.json', 'voipcall.json', 'user_profile.json'] def setUp(self): self.user = User.objects.get(username='******') VoipSearchForm(self.user) try: content_type_id = ContentType.objects.get(model='survey').id except: content_type_id = 1 # Callrequest model self.callrequest = Callrequest( call_type=1, status=1, user=self.user, phone_number='123456', subscriber_id=1, campaign_id=1, aleg_gateway_id=1, content_type_id=content_type_id, object_id=1, ) self.callrequest.save() # VoIPCall model self.voipcall = VoIPCall( user=self.user, used_gateway_id=1, callrequest=self.callrequest, callid='Top Gun', phone_number='123456', leg_type=1, duration=20, ) self.voipcall.save() self.assertEqual(self.voipcall.__unicode__(), u'2 - Top Gun') # Test mgt command call_command("create_callrequest_cdr", "1|1") call_command("create_callrequest_cdr", "3|1") def test_name(self): self.assertEqual(self.callrequest.phone_number, "123456") #self.assertEqual(self.callrequest.__unicode__(), u'Top Gun') self.assertEqual(self.voipcall.phone_number, "123456") Callrequest.objects.get_pending_callrequest() self.voipcall.destination_name() self.voipcall.duration = '' self.voipcall.min_duration() self.voipcall.duration = 12 self.voipcall.min_duration() def teardown(self): self.callrequest.delete() self.voipcall.delete()
def voipcall_save(callrequest, request_uuid, leg='aleg', hangup_cause='', hangup_cause_q850='', callerid='', phonenumber='', starting_date='', call_uuid='', duration=0, billsec=0, amd_status='person'): """ This task will save the voipcall(CDR) to the DB, it will also reformat the disposition """ # TODO: following code is duplicated, see above used_gateway = callrequest.aleg_gateway # Set Leg Type if leg == 'aleg': leg_type = LEG_TYPE.A_LEG else: leg_type = LEG_TYPE.B_LEG # Set AMD status if amd_status == 'machine': amd_status_id = VOIPCALL_AMD_STATUS.MACHINE else: amd_status_id = VOIPCALL_AMD_STATUS.PERSON logger.debug( 'Create CDR - request_uuid=%s;leg=%d;hangup_cause=%s;billsec=%s;amd_status=%s' % (request_uuid, leg_type, hangup_cause, str(billsec), amd_status)) # Get the first word only hangup_cause = hangup_cause.split()[0] if hangup_cause == 'NORMAL_CLEARING' or hangup_cause == 'ALLOTTED_TIMEOUT': disposition = 'ANSWER' elif hangup_cause == 'USER_BUSY': disposition = 'BUSY' elif hangup_cause == 'NO_ANSWER': disposition = 'NOANSWER' elif hangup_cause == 'ORIGINATOR_CANCEL': disposition = 'CANCEL' elif hangup_cause == 'NORMAL_CIRCUIT_CONGESTION': disposition = 'CONGESTION' else: disposition = 'FAILED' # Note: Removed for test performance # Note: Look at prefix PG module : https://github.com/dimitri/prefix #prefix_obj = get_prefix_obj(phonenumber) # Save the VoIPCall new_voipcall = VoIPCall( user_id=callrequest.user_id, request_uuid=request_uuid, leg_type=leg_type, used_gateway=used_gateway, callrequest_id=callrequest.id, callid=call_uuid, callerid=callerid, phone_number=phonenumber, # dialcode=prefix_obj, starting_date=starting_date, duration=duration, billsec=billsec, disposition=disposition, hangup_cause=hangup_cause, hangup_cause_q850=hangup_cause_q850, amd_status=amd_status_id) new_voipcall.save()
class DialerCdrModel(TestCase): """Test Callrequest, VoIPCall models""" fixtures = [ 'auth_user.json', 'gateway.json', 'dialer_setting.json', 'contenttype.json', 'user_profile.json', 'phonebook.json', 'contact.json', 'dnc_list.json', 'dnc_contact.json', 'campaign.json', 'subscriber.json', 'callrequest.json', 'survey.json', 'section.json' ] def setUp(self): self.user = User.objects.get(username='******') VoipSearchForm(self.user) try: content_type_id = ContentType.objects.get(model='survey').id except: content_type_id = 1 # Callrequest model self.callrequest = Callrequest( call_type=1, status=1, user=self.user, phone_number='123456', subscriber_id=1, campaign_id=1, aleg_gateway_id=1, content_type_id=content_type_id, object_id=1, ) self.callrequest.save() # VoIPCall model self.voipcall = VoIPCall( user=self.user, used_gateway_id=1, callrequest=self.callrequest, callid='Top Gun', phone_number='123456', leg_type=1, duration=20, ) self.voipcall.save() self.assertEqual(self.voipcall.__unicode__(), u'1 - Top Gun') # Test mgt command call_command("create_callrequest_cdr", "1|1") call_command("create_callrequest_cdr", "3|1") def test_name(self): self.assertEqual(self.callrequest.phone_number, "123456") #self.assertEqual(self.callrequest.__unicode__(), u'Top Gun') self.assertEqual(self.voipcall.phone_number, "123456") Callrequest.objects.get_pending_callrequest() self.voipcall.destination_name() self.voipcall.duration = '' self.voipcall.min_duration() self.voipcall.duration = 12 self.voipcall.min_duration() def teardown(self): self.callrequest.delete() self.voipcall.delete()
def create_voipcall(obj_callrequest, plivo_request_uuid, data, data_prefix='', leg='a', hangup_cause='', from_plivo='', to_plivo=''): """ Common function to create CDR / VoIP Call **Attributes**: * data : list with call details data * obj_callrequest: refer to the CallRequest object * plivo_request_uuid : cdr uuid """ if 'answer_epoch' in data and data['answer_epoch']: try: cur_answer_epoch = int(data['answer_epoch']) except ValueError: raise starting_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(cur_answer_epoch)) else: starting_date = None if leg == 'a': #A-Leg leg_type = 1 used_gateway = obj_callrequest.aleg_gateway else: #B-Leg leg_type = 2 used_gateway = obj_callrequest.content_object.gateway #check the right variable for hangup cause data_hangup_cause = data["%s%s" % (data_prefix, 'hangup_cause')] if data_hangup_cause and data_hangup_cause != '': cdr_hangup_cause = data_hangup_cause else: cdr_hangup_cause = hangup_cause if cdr_hangup_cause == 'USER_BUSY': disposition = 'BUSY' else: disposition = data["%s%s" % \ (data_prefix, 'endpoint_disposition')] or '' logger.debug('Create CDR - request_uuid=%s ; leg=%d ; hangup_cause= %s' % \ (plivo_request_uuid, leg_type, cdr_hangup_cause)) new_voipcall = VoIPCall( user=obj_callrequest.user, request_uuid=plivo_request_uuid, leg_type=leg_type, used_gateway=used_gateway, callrequest=obj_callrequest, callid=data["%s%s" % (data_prefix, 'call_uuid')] or '', callerid=from_plivo, phone_number=to_plivo, dialcode=None, # TODO starting_date=starting_date, duration=data["%s%s" % (data_prefix, 'duration')] or 0, billsec=data["%s%s" % (data_prefix, 'billsec')] or 0, progresssec=data["%s%s" % \ (data_prefix, 'progresssec')] or 0, answersec=data["%s%s" % (data_prefix, 'answersec')] or 0, disposition=disposition, hangup_cause=cdr_hangup_cause, hangup_cause_q850=data["%s%s" % \ (data_prefix, 'hangup_cause_q850')] or '',) new_voipcall.save()
def voipcall_save(callrequest, request_uuid, leg='aleg', hangup_cause='', hangup_cause_q850='', callerid='', phonenumber='', starting_date='', call_uuid='', duration=0, billsec=0, amd_status='person'): """ This task will save the voipcall(CDR) to the DB, it will also reformat the disposition """ # TODO: following code is duplicated, see above used_gateway = callrequest.aleg_gateway # Set Leg Type if leg == 'aleg': leg_type = LEG_TYPE.A_LEG else: leg_type = LEG_TYPE.B_LEG # Set AMD status if amd_status == 'machine': amd_status_id = VOIPCALL_AMD_STATUS.MACHINE else: amd_status_id = VOIPCALL_AMD_STATUS.PERSON logger.debug('Create CDR - request_uuid=%s;leg=%d;hangup_cause=%s;billsec=%s;amd_status=%s' % (request_uuid, leg_type, hangup_cause, str(billsec), amd_status)) # Get the first word only hangup_cause = hangup_cause.split()[0] if hangup_cause == 'NORMAL_CLEARING' or hangup_cause == 'ALLOTTED_TIMEOUT': disposition = 'ANSWER' elif hangup_cause == 'USER_BUSY': disposition = 'BUSY' elif hangup_cause == 'NO_ANSWER': disposition = 'NOANSWER' elif hangup_cause == 'ORIGINATOR_CANCEL': disposition = 'CANCEL' elif hangup_cause == 'NORMAL_CIRCUIT_CONGESTION': disposition = 'CONGESTION' else: disposition = 'FAILED' # Note: Removed for test performance # Note: Look at prefix PG module : https://github.com/dimitri/prefix #prefix_obj = get_prefix_obj(phonenumber) # Save the VoIPCall new_voipcall = VoIPCall( user_id=callrequest.user_id, request_uuid=request_uuid, leg_type=leg_type, used_gateway=used_gateway, callrequest_id=callrequest.id, callid=call_uuid, callerid=callerid, phone_number=phonenumber, # dialcode=prefix_obj, starting_date=starting_date, duration=duration, billsec=billsec, disposition=disposition, hangup_cause=hangup_cause, hangup_cause_q850=hangup_cause_q850, amd_status=amd_status_id) new_voipcall.save()
def voipcall_save(callrequest, request_uuid, leg='aleg', hangup_cause='', hangup_cause_q850='', callerid='', phonenumber='', starting_date='', call_uuid='', duration=0, billsec=0, amd_status='person'): """ Save voip call immediately """ if leg == 'aleg': #A-Leg leg_type = LEG_TYPE.A_LEG used_gateway = callrequest.aleg_gateway else: #B-Leg leg_type = LEG_TYPE.B_LEG used_gateway = callrequest.aleg_gateway #This code is useful if we want to let the survey editor select the gateway # if callrequest.content_object.__class__.__name__ == 'Survey': # #Get the gateway from the App # used_gateway = callrequest.content_object.gateway # else: # #Survey # used_gateway = callrequest.aleg_gateway if amd_status == 'machine': amd_status_id = VOIPCALL_AMD_STATUS.MACHINE else: amd_status_id = VOIPCALL_AMD_STATUS.PERSON logger.debug('Create CDR - request_uuid=%s;leg=%d;hangup_cause=%s;billsec=%s;amd_status=%s' % (request_uuid, leg_type, hangup_cause, str(billsec), amd_status)) #Get the first word only hangup_cause = hangup_cause.split()[0] if hangup_cause == 'NORMAL_CLEARING' or hangup_cause == 'ALLOTTED_TIMEOUT': hangup_cause = 'ANSWER' if hangup_cause == 'ANSWER': disposition = 'ANSWER' elif hangup_cause == 'USER_BUSY': disposition = 'BUSY' elif hangup_cause == 'NO_ANSWER': disposition = 'NOANSWER' elif hangup_cause == 'ORIGINATOR_CANCEL': disposition = 'CANCEL' elif hangup_cause == 'NORMAL_CIRCUIT_CONGESTION': disposition = 'CONGESTION' else: disposition = 'FAILED' #Note: Removed for test performance #Note: Look at prefix PG module : https://github.com/dimitri/prefix #prefix_obj = get_prefix_obj(phonenumber) #Save this new_voipcall = VoIPCall( user_id=callrequest.user_id, request_uuid=request_uuid, leg_type=leg_type, used_gateway=used_gateway, callrequest_id=callrequest.id, callid=call_uuid, callerid=callerid, phone_number=phonenumber, #dialcode=prefix_obj, starting_date=starting_date, duration=duration, billsec=billsec, disposition=disposition, hangup_cause=hangup_cause, hangup_cause_q850=hangup_cause_q850, amd_status=amd_status_id) new_voipcall.save()
def voipcall_save(callrequest, request_uuid, leg='aleg', hangup_cause='', hangup_cause_q850='', callerid='', phonenumber='', starting_date='', call_uuid='', duration=0, billsec=0, amd_status='person'): """ Save voip call immediately """ if leg == 'aleg': #A-Leg leg_type = LEG_TYPE.A_LEG used_gateway = callrequest.aleg_gateway else: #B-Leg leg_type = LEG_TYPE.B_LEG used_gateway = callrequest.aleg_gateway #This code is useful if we want to let the survey editor select the gateway # if callrequest.content_object.__class__.__name__ == 'Survey': # #Get the gateway from the App # used_gateway = callrequest.content_object.gateway # else: # #Survey # used_gateway = callrequest.aleg_gateway if amd_status == 'machine': amd_status_id = VOIPCALL_AMD_STATUS.MACHINE else: amd_status_id = VOIPCALL_AMD_STATUS.PERSON logger.debug( 'Create CDR - request_uuid=%s;leg=%d;hangup_cause=%s;billsec=%s;amd_status=%s' % (request_uuid, leg_type, hangup_cause, str(billsec), amd_status)) #Get the first word only hangup_cause = hangup_cause.split()[0] if hangup_cause == 'NORMAL_CLEARING' or hangup_cause == 'ALLOTTED_TIMEOUT': hangup_cause = 'ANSWER' if hangup_cause == 'ANSWER': disposition = 'ANSWER' elif hangup_cause == 'USER_BUSY': disposition = 'BUSY' elif hangup_cause == 'NO_ANSWER': disposition = 'NOANSWER' elif hangup_cause == 'ORIGINATOR_CANCEL': disposition = 'CANCEL' elif hangup_cause == 'NORMAL_CIRCUIT_CONGESTION': disposition = 'CONGESTION' else: disposition = 'FAILED' #Note: Removed for test performance #Note: Look at prefix PG module : https://github.com/dimitri/prefix #prefix_obj = get_prefix_obj(phonenumber) #Save this new_voipcall = VoIPCall( user_id=callrequest.user_id, request_uuid=request_uuid, leg_type=leg_type, used_gateway=used_gateway, callrequest_id=callrequest.id, callid=call_uuid, callerid=callerid, phone_number=phonenumber, #dialcode=prefix_obj, starting_date=starting_date, duration=duration, billsec=billsec, disposition=disposition, hangup_cause=hangup_cause, hangup_cause_q850=hangup_cause_q850, amd_status=amd_status_id) new_voipcall.save()
def create_voipcall(obj_callrequest, request_uuid, data, data_prefix='', leg='a', hangup_cause='', caller_pn='', called_pn=''): """ Common function to create CDR / VoIP Call **Attributes**: * data : list with call details data * obj_callrequest: refer to the CallRequest object * request_uuid : cdr uuid """ if 'answer_epoch' in data and data['answer_epoch']: try: cur_answer_epoch = int(data['answer_epoch']) except ValueError: raise starting_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(cur_answer_epoch)) else: starting_date = None if leg == 'a': #A-Leg leg_type = 1 used_gateway = obj_callrequest.aleg_gateway else: #B-Leg leg_type = 2 if obj_callrequest.content_object.__class__.__name__ == 'Survey': used_gateway = obj_callrequest.content_object.gateway else: #Survey used_gateway = obj_callrequest.aleg_gateway #check the right variable for hangup cause data_hangup_cause = data["%s%s" % (data_prefix, 'hangup_cause')] if data_hangup_cause and data_hangup_cause != '': cdr_hangup_cause = data_hangup_cause else: cdr_hangup_cause = hangup_cause if cdr_hangup_cause == 'USER_BUSY': disposition = 'BUSY' else: disposition = data["%s%s" % (data_prefix, 'endpoint_disposition')] or '' if not caller_pn: caller_pn = '' logger.debug('Create CDR - request_uuid=%s ; leg=%d ; hangup_cause= %s' % (request_uuid, leg_type, cdr_hangup_cause)) prefix_obj = get_prefix_obj(called_pn) new_voipcall = VoIPCall( user=obj_callrequest.user, request_uuid=request_uuid, leg_type=leg_type, used_gateway=used_gateway, callrequest=obj_callrequest, callid=data["%s%s" % (data_prefix, 'call_uuid')] or '', callerid=caller_pn, phone_number=called_pn, dialcode=prefix_obj, starting_date=starting_date, duration=data["%s%s" % (data_prefix, 'duration')] or 0, billsec=data["%s%s" % (data_prefix, 'billsec')] or 0, progresssec=data["%s%s" % (data_prefix, 'progresssec')] or 0, answersec=data["%s%s" % (data_prefix, 'answersec')] or 0, disposition=disposition, hangup_cause=cdr_hangup_cause, hangup_cause_q850=data["%s%s" % (data_prefix, 'hangup_cause_q850')] or '',) new_voipcall.save()