Пример #1
0
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
Пример #2
0
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
Пример #3
0
    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")
Пример #4
0
    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 create_callrequest(campaign_id, amount, day_delta):
    """
    This function create fake Callrequest for a given:
        * campaign_id
        * amount
        * day_delta

    """
    try:
        obj_campaign = Campaign.objects.get(id=campaign_id)
    except:
        print _('Can\'t find this Campaign : %(id)s' % {'id': campaign_id})
        return False

    #content_type_id is survey
    try:
        content_type_id = ContentType.objects.get(model='survey').id
    except:
        content_type_id = 1

    list_vc = []
    for i in range(1, int(amount) + 1):
        delta_days = random.randint(0, day_delta)
        delta_minutes = random.randint(-720, 720)
        created_date = datetime.utcnow().replace(tzinfo=utc) \
            - timedelta(minutes=delta_minutes) \
            - timedelta(days=delta_days)

        phonenumber = '' . join([choice("1234567890") for i in range(PHONENUMBER_LENGHT)])
        new_callrequest = Callrequest.objects.create(
            request_uuid=uuid1(),
            user=obj_campaign.user,
            phone_number=phonenumber,
            campaign=obj_campaign,
            aleg_gateway_id=1,
            status=choice("12345678"),
            call_type=1,
            content_type_id=content_type_id,
            call_time=created_date,
            created_date=created_date,
            object_id=1)
        print "new_callrequest: " + str(new_callrequest)

        new_voipcall = VoIPCall(
            request_uuid=uuid1(),
            callid=uuid1(),
            user=obj_campaign.user,
            callrequest=new_callrequest,
            starting_date=created_date,
            phone_number=phonenumber,
            duration=random.randint(50, 1000),
            disposition=weighted_choice(VOIPCALL_DISPOSITION),
            amd_status=choice(VOIPCALL_AMD_STATUS))
        new_voipcall.starting_date = created_date
        # print "voipcall: " + str(new_voipcall)
        list_vc.append(new_voipcall)

        if i % 100 == 0:
            VoIPCall.objects.bulk_create(list_vc)
            list_vc = []

        """
        alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        response_count = choice("1234567890")

        # print "Get list section:"
        from survey.models import Section, ResultAggregate
        list_section = Section.objects.filter(survey_id=obj_campaign.object_id)
        #list_section = Section.objects.all()


        for j in range(1, 3):
            section_id = random.randint(0, len(list_section) - 1)
            print section_id
            print list_section[section_id]
            print "-----------------"
            try:
                cpg_result = Result.objects.create(
                                    section=list_section[section_id],
                                    response=choice(RESPONSE),
                                    record_file='xyz.mp3',
                                    recording_duration=10,
                                    callrequest=new_callrequest)
            except:
                pass
        #response = '' . join([choice(alpha) for i in range(length)])
        ResultAggregate.objects.create(
                            survey_id=obj_campaign.object_id,
                            section=list_section[section_id],
                            response=choice(RESPONSE),
                            #response=response,
                            count=response_count)
        """

    # create the last one
    if list_vc:
        VoIPCall.objects.bulk_create(list_vc)

    print _("Callrequests and CDRs created : %(count)s" % {'count': amount})
Пример #6
0
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()
Пример #7
0
    def save(self,
             obj_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 into buffer
        """
        if leg == 'aleg':
            # A-Leg
            leg_type = LEG_TYPE.A_LEG
            used_gateway = obj_callrequest.aleg_gateway
        else:
            # B-Leg
            leg_type = LEG_TYPE.B_LEG
            used_gateway = obj_callrequest.aleg_gateway
            # This code is useful if we want to let the survey editor select the gateway
            # if obj_callrequest.content_object.__class__.__name__ == 'Survey':
            #     #Get the gateway from the App
            #     used_gateway = obj_callrequest.content_object.gateway
            # else:
            #     #Survey
            #     used_gateway = obj_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 for bulk saving
        self.list_voipcall.append(
            VoIPCall(
                user_id=obj_callrequest.user_id,
                request_uuid=request_uuid,
                leg_type=leg_type,
                used_gateway=used_gateway,
                callrequest_id=obj_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))
Пример #8
0
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()
Пример #9
0
def create_callrequest(campaign_id, amount, day_delta):
    """
    This function create fake Callrequest for a given:
        * campaign_id
        * amount
        * day_delta

    """
    try:
        obj_campaign = Campaign.objects.get(id=campaign_id)
    except:
        print _('Can\'t find this Campaign : %(id)s' % {'id': campaign_id})
        return False

    #content_type_id is survey
    try:
        content_type_id = ContentType.objects.get(model='survey').id
    except:
        content_type_id = 1

    list_vc = []
    for i in range(1, int(amount) + 1):
        delta_days = random.randint(0, day_delta)
        delta_minutes = random.randint(-720, 720)
        created_date = datetime.utcnow().replace(tzinfo=utc) \
            - timedelta(minutes=delta_minutes) \
            - timedelta(days=delta_days)

        phonenumber = ''.join(
            [choice("1234567890") for i in range(PHONENUMBER_LENGHT)])
        new_callrequest = Callrequest.objects.create(
            request_uuid=uuid1(),
            user=obj_campaign.user,
            phone_number=phonenumber,
            campaign=obj_campaign,
            aleg_gateway_id=1,
            status=choice("12345678"),
            call_type=1,
            content_type_id=content_type_id,
            call_time=created_date,
            created_date=created_date,
            object_id=1)
        print "new_callrequest: " + str(new_callrequest)

        new_voipcall = VoIPCall(
            request_uuid=uuid1(),
            callid=uuid1(),
            user=obj_campaign.user,
            callrequest=new_callrequest,
            starting_date=created_date,
            phone_number=phonenumber,
            duration=random.randint(50, 1000),
            disposition=weighted_choice(VOIPCALL_DISPOSITION),
            amd_status=choice(VOIPCALL_AMD_STATUS))
        new_voipcall.starting_date = created_date
        # print "voipcall: " + str(new_voipcall)
        list_vc.append(new_voipcall)

        if i % 100 == 0:
            VoIPCall.objects.bulk_create(list_vc)
            list_vc = []
        """
        alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        response_count = choice("1234567890")

        # print "Get list section:"
        from survey.models import Section, ResultAggregate
        list_section = Section.objects.filter(survey_id=obj_campaign.object_id)
        #list_section = Section.objects.all()


        for j in range(1, 3):
            section_id = random.randint(0, len(list_section) - 1)
            print section_id
            print list_section[section_id]
            print "-----------------"
            try:
                cpg_result = Result.objects.create(
                                    section=list_section[section_id],
                                    response=choice(RESPONSE),
                                    record_file='xyz.mp3',
                                    recording_duration=10,
                                    callrequest=new_callrequest)
            except:
                pass
        #response = '' . join([choice(alpha) for i in range(length)])
        ResultAggregate.objects.create(
                            survey_id=obj_campaign.object_id,
                            section=list_section[section_id],
                            response=choice(RESPONSE),
                            #response=response,
                            count=response_count)
        """

    # create the last one
    if list_vc:
        VoIPCall.objects.bulk_create(list_vc)

    print _("Callrequests and CDRs created : %(count)s" % {'count': amount})
Пример #10
0
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()
Пример #11
0
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()
Пример #12
0
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()
Пример #13
0
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()
Пример #14
0
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()