def import_cdr(shell=False, logger=False): """ Connect to the `import_cdr` Database and import the new CDRs """ count_imported = 0 log_print(logger, shell, "in func import_cdr...") if not check_connection_sql(): log_print(logger, shell, "check_connection_sql - Error Connection") return (False, "Error Connection") # Each time the task is running we will only take CDR_IMPORT_LIMIT records to import # This define the max speed of import, this limit could be changed new_CDRs = CDRImport.objects.using('import_cdr')\ .filter(imported=False)\ .order_by('-id')[:settings.CDR_IMPORT_LIMIT] (list_newcdr, list_cdrid) = ([], []) for call in new_CDRs: # Increment counter count_imported = count_imported + 1 # Get the dialcode dialcode = get_dialcode(call.destination_number, call.dialcode) switch_info = chk_ipaddress(call.switch) # Check Destination number if len( call.destination_number ) <= settings.INTERNAL_CALL or call.destination_number[:1].isalpha(): authorized = 1 country_id = None call_type = CALL_TYPE.INTERNAL else: # TODO: rename verify_auth_dest_number verify_auth_dest_number destination_data = verify_auth_dest_number(call.destination_number) authorized = destination_data['authorized'] country_id = destination_data['country_id'] call_type = CALL_TYPE.INTERNATIONAL # Sanitize direction if call.direction: direction = call.direction else: direction = CALL_DIRECTION.NOTDEFINED # Find the user for given accountcode try: user = AccountCode.objects.get(accountcode=call.accountcode).user except: # Cannot assign accountcode to an existing user, therefore we will assign to an Admin user = User.objects.filter(is_superuser=True)[0] try: user_profile = user.userprofile except: user_profile = UserProfile(user=user) user_profile.save() # Retrieve VoipPlan if user_profile: voipplan_id = user_profile.voipplan_id else: voipplan_id = False print_shell( shell, "VoipPlan doesn't exist for this user/accountcode (%s)" % call.accountcode) if call.buy_rate or call.buy_cost or call.sell_rate or call.sell_cost: buy_rate = call.buy_rate buy_cost = call.buy_cost sell_rate = call.sell_rate sell_cost = call.sell_cost elif voipplan_id: call_rate = calculate_call_cost(voipplan_id, call.destination_number, call.billsec) buy_rate = call_rate['buy_rate'] buy_cost = call_rate['buy_cost'] sell_rate = call_rate['sell_rate'] sell_cost = call_rate['sell_cost'] else: buy_rate = buy_cost = sell_rate = sell_cost = 0 hangup_cause_id = get_hangupcause_id(call.hangup_cause_id) log_print( logger, shell, "Create new CDR -> date:%s - dst:%s - duration:%s - hangup_cause:%s - sell_cost:%s" % (call.starting_date, call.destination_number, str( call.duration), str(hangup_cause_id), str(call.sell_cost))) # Create the new CDR newCDR = CDR( user=user, switch=switch_info['switch'], cdr_source_type=call.cdr_source_type, callid=call.callid, caller_id_number=call.caller_id_number, caller_id_name=call.caller_id_name, destination_number=call.destination_number, dialcode_id=dialcode, starting_date=call.starting_date, duration=call.duration, billsec=call.billsec, progresssec=call.progresssec, answersec=call.answersec, waitsec=call.waitsec, hangup_cause_id=hangup_cause_id, direction=direction, country_id=country_id, authorized=authorized, accountcode='' if call.accountcode is None else call.accountcode, buy_rate=buy_rate, buy_cost=buy_cost, sell_rate=sell_rate, sell_cost=sell_cost, call_type=call_type, data='' if call.extradata is None else call.extradata) list_newcdr.append(newCDR) list_cdrid.append(str(call.id)) if (count_imported % 100) == 0: bulk_create_cdrs(list_newcdr, list_cdrid) (list_newcdr, list_cdrid) = ([], []) # we exit the loop but we might still have some remaining CDRs to push if len(list_newcdr) > 0: bulk_create_cdrs(list_newcdr, list_cdrid) (list_newcdr, list_cdrid) = ([], []) log_print( logger, shell, 'TASK :: run_cdr_import -> func import_cdr count_imported:%d' % count_imported) return (True, count_imported)
def push_asterisk_cdr(shell, table_name, db_engine, cursor, cursor_updated, switch, ipaddress): """ function to import and aggreagate Asterisk CDR """ cdr_source_type = CDR_SOURCE_TYPE.ASTERISK count_import = 0 # Each time the task is running we will only take 1000 records to import # This define the max speed of import, this limit could be changed if db_engine == 'mysql': cursor.execute("SELECT dst, UNIX_TIMESTAMP(calldate), clid, channel," "duration, billsec, disposition, accountcode, uniqueid," " %s FROM %s WHERE import_cdr=0 LIMIT 0, 1000" % (settings.ASTERISK_PRIMARY_KEY, table_name)) elif db_engine == 'pgsql': cursor.execute( "SELECT dst, extract(epoch FROM calldate), clid, channel," "duration, billsec, disposition, accountcode, uniqueid," " %s FROM %s WHERE import_cdr=0 LIMIT 1000" % (settings.ASTERISK_PRIMARY_KEY, table_name)) row = cursor.fetchone() # Store cdr in list to insert by bulk local_count_import = 0 batch_count = 0 acctid_list = '' while row is not None: destination_number = row[0] if not destination_number: # don't import CDR with no destination number continue acctid = row[9] callerid = row[2] try: m = re.search('"(.+?)" <(.+?)>', callerid) callerid_name = m.group(1) callerid_number = m.group(2) except: callerid_name = '' callerid_number = callerid channel = row[3] if not channel: channel = '' # Set empty string for channel in case is None duration = set_int_default(row[4], 0) billsec = set_int_default(row[5], 0) hangup_cause_id = translate_disposition(row[6]) accountcode = row[7] uniqueid = row[8] start_uepoch = datetime.fromtimestamp(int(row[1])) # Check Destination number if len(destination_number ) <= settings.INTERNAL_CALL or destination_number[:1].isalpha(): authorized = 1 country_id = 999 else: destination_data = verify_auth_dest_number(destination_number) authorized = destination_data['authorized'] country_id = destination_data['country_id'] # Option to get the direction from user_field direction = "unknown" try: # TODO: Code not valide as accountcode moved to his own model AccountCode # FYI, asterisk_fetcher.py is not used voipplan_id = UserProfile.objects.get( accountcode=accountcode).voipplan_id except: voipplan_id = False print_shell(shell, "No VoipPlan created for this user/accountcode") try: # TODO: Code not valide as accountcode moved to his own model AccountCode # FYI, asterisk_fetcher.py is not used user = UserProfile.objects.get(accountcode=accountcode).user except: # Cannot assign accountcode to an existing user # we will then assign to admin user = User.objects.filter(is_superuser=True)[0] call_rate = calculate_call_cost(voipplan_id, destination_number, billsec) buy_rate = call_rate['buy_rate'] buy_cost = call_rate['buy_cost'] sell_rate = call_rate['sell_rate'] sell_cost = call_rate['sell_cost'] # Sanitize callerid_number = sanitize_cdr_field(callerid_number) callerid_name = sanitize_cdr_field(callerid_name) destination_number = sanitize_cdr_field(destination_number) channel = sanitize_cdr_field(channel) cdr_json = {'channel': channel} dialcode = '' cdr = CDR(user=user, switch=switch, cdr_source_type=cdr_source_type, callid=uniqueid, caller_id_number=callerid_number, destination_number=destination_number, dialcode_id=dialcode, starting_date=start_uepoch, duration=duration, billsec=billsec, hangup_cause=hangup_cause_id, direction=direction, country_id=country_id, authorized=authorized, accountcode=accountcode, buy_rate=buy_rate, buy_cost=buy_cost, sell_rate=sell_rate, sell_cost=sell_cost, data=cdr_json) cdr.save() # Append cdr to bulk_cdr list count_import = count_import + 1 local_count_import = local_count_import + 1 batch_count = batch_count + 1 acctid_list += "%s, " % str(acctid) if batch_count == 100: acctid_list = acctid_list[:-2] # trim last comma (,) from string # Postgresql # select * from table_name where id in (1,2,3); update_cdr = "UPDATE %s SET import_cdr=1 WHERE %s in (%s)" % \ (table_name, settings.ASTERISK_PRIMARY_KEY, acctid_list) cursor_updated.execute(update_cdr) batch_count = 0 acctid_list = '' # Fetch a other record row = cursor.fetchone() if len(acctid_list) > 0: acctid_list = acctid_list[:-2] # trim last comma (,) from string # Postgresql # select * from table_name where id in (1,2,3); update_cdr = "UPDATE %s SET import_cdr=1 WHERE %s in (%s)" % \ (table_name, settings.ASTERISK_PRIMARY_KEY, acctid_list) cursor_updated.execute(update_cdr) return count_import
def generate_fake_cdr(cls, day_delta_int): """return tuples of fake CDR data""" # HANGUP_CAUSE = ['NORMAL_CLEARING', 'USER_BUSY', 'NO_ANSWER', 'CALL_REJECTED', 'INVALID_NUMBER_FORMAT'] # HANGUP_CAUSE_Q850 = ['16', '17', '19', '21', '28'] """ HANGUP_CAUSE = ['NORMAL_CLEARING', 'USER_BUSY', 'NO_ANSWER'] HANGUP_CAUSE_Q850 = ['16', '17', '19'] rand_hangup = random.randint(0, len(HANGUP_CAUSE)-1) hangup_cause = HANGUP_CAUSE[rand_hangup] hangup_cause_q850 = HANGUP_CAUSE_Q850[rand_hangup] if hangup_cause == 'NORMAL_CLEARING': duration = random.randint(1, 200) billsec = random.randint(1, 200) else: duration = 0 billsec = 0 """ # from django.db.models import Q # import operator # predicates = [ # ('code__exact', 16), ('code__exact', 17), ('code__exact', 18) # ] # q_list = [Q(x) for x in predicates] # list_hg = HangupCause.objects.filter(reduce(operator.or_, q_list)) HANGUP_CAUSE_Q850 = [16, 17, 19] list_hg = HangupCause.objects.filter(code__in=HANGUP_CAUSE_Q850) hangupcause = list_hg[random.randint(0, len(list_hg) - 1)] if hangupcause.code != 16: # Increase chances to have Answered calls hangupcause = list_hg[random.randint(0, len(list_hg) - 1)] hangup_cause_q850 = hangupcause.code if hangupcause.code == 16: duration = random.randint(1, 200) billsec = random.randint(1, 200) else: duration = 0 billsec = 0 delta_days = random.randint(0, day_delta_int) delta_minutes = random.randint(1, 1440) answer_stamp = datetime.datetime.now() \ - datetime.timedelta(minutes=delta_minutes) \ - datetime.timedelta(days=delta_days) # convert answer_stamp into milliseconds start_uepoch = int(time.mktime(answer_stamp.timetuple())) caller_id = ''.join([random.choice(string.digits) for i in range(8)]) channel_name = 'sofia/internal/' + caller_id + '@127.0.0.1' destination_number = ''.join([random.choice(string.digits) for i in range(8)]) if random.randint(1, 20) == 1: # Add local calls dialcode = None country_id = None authorized = 1 destination_number = ''.join([random.choice(string.digits) for i in range(5)]) else: # International calls destination_number = random.choice(COUNTRY_PREFIX) + destination_number from cdr_alert.functions_blacklist import verify_auth_dest_number destination_data = verify_auth_dest_number(destination_number) authorized = destination_data['authorized'] country_id = destination_data['country_id'] dialcode = destination_data['prefix_id'] end_stamp = str(datetime.datetime.now() - datetime.timedelta(minutes=delta_minutes) - datetime.timedelta(days=delta_days) + datetime.timedelta(seconds=duration)) callid = str(uuid1()) cdr_source_type = CDR_SOURCE_TYPE.FREESWITCH direction = CALL_DIRECTION.INBOUND return ( callid, answer_stamp, start_uepoch, caller_id, channel_name, destination_number, dialcode, hangupcause, hangup_cause_q850, duration, billsec, end_stamp, cdr_source_type, authorized, country_id, direction, # accountcode, buy_rate, buy_cost, sell_rate, sell_cost )
def import_cdr(shell=False, logger=False): """ Connect to the `import_cdr` Database and import the new CDRs """ count_imported = 0 log_print(logger, shell, "in func import_cdr...") if not check_connection_sql(): log_print(logger, shell, "check_connection_sql - Error Connection") return (False, "Error Connection") # Each time the task is running we will only take CDR_IMPORT_LIMIT records to import # This define the max speed of import, this limit could be changed new_CDRs = CDRImport.objects.using('import_cdr')\ .filter(imported=False)\ .order_by('-id')[:settings.CDR_IMPORT_LIMIT] (list_newcdr, list_cdrid) = ([], []) for call in new_CDRs: # Increment counter count_imported = count_imported + 1 # Get the dialcode dialcode = get_dialcode(call.destination_number, call.dialcode) switch_info = chk_ipaddress(call.switch) # Check Destination number if len(call.destination_number) <= settings.INTERNAL_CALL or call.destination_number[:1].isalpha(): authorized = 1 country_id = None call_type = CALL_TYPE.INTERNAL else: # TODO: rename verify_auth_dest_number verify_auth_dest_number destination_data = verify_auth_dest_number(call.destination_number) authorized = destination_data['authorized'] country_id = destination_data['country_id'] call_type = CALL_TYPE.INTERNATIONAL # Sanitize direction if call.direction: direction = call.direction else: direction = CALL_DIRECTION.NOTDEFINED # Find the user for given accountcode try: user = AccountCode.objects.get(accountcode=call.accountcode).user except: # Cannot assign accountcode to an existing user, therefore we will assign to an Admin user = User.objects.filter(is_superuser=True)[0] try: user_profile = user.userprofile except: user_profile = UserProfile(user=user) user_profile.save() # Retrieve VoipPlan if user_profile: voipplan_id = user_profile.voipplan_id else: voipplan_id = False print_shell(shell, "VoipPlan doesn't exist for this user/accountcode (%s)" % call.accountcode) if call.buy_rate or call.buy_cost or call.sell_rate or call.sell_cost: buy_rate = call.buy_rate buy_cost = call.buy_cost sell_rate = call.sell_rate sell_cost = call.sell_cost elif voipplan_id: call_rate = calculate_call_cost(voipplan_id, call.destination_number, call.billsec) buy_rate = call_rate['buy_rate'] buy_cost = call_rate['buy_cost'] sell_rate = call_rate['sell_rate'] sell_cost = call_rate['sell_cost'] else: buy_rate = buy_cost = sell_rate = sell_cost = 0 hangup_cause_id = get_hangupcause_id(call.hangup_cause_id) log_print(logger, shell, "Create new CDR -> date:%s - dst:%s - duration:%s - hangup_cause:%s - sell_cost:%s" % (call.starting_date, call.destination_number, str(call.duration), str(hangup_cause_id), str(call.sell_cost))) # Create the new CDR newCDR = CDR( user=user, switch=switch_info['switch'], cdr_source_type=call.cdr_source_type, callid=call.callid, caller_id_number=call.caller_id_number, caller_id_name=call.caller_id_name, destination_number=call.destination_number, dialcode_id=dialcode, starting_date=call.starting_date, duration=call.duration, billsec=call.billsec, progresssec=call.progresssec, answersec=call.answersec, waitsec=call.waitsec, hangup_cause_id=hangup_cause_id, direction=direction, country_id=country_id, authorized=authorized, accountcode='' if call.accountcode is None else call.accountcode, buy_rate=buy_rate, buy_cost=buy_cost, sell_rate=sell_rate, sell_cost=sell_cost, call_type=call_type, data='' if call.extradata is None else call.extradata) list_newcdr.append(newCDR) list_cdrid.append(str(call.id)) if (count_imported % 100) == 0: bulk_create_cdrs(list_newcdr, list_cdrid) (list_newcdr, list_cdrid) = ([], []) # we exit the loop but we might still have some remaining CDRs to push if len(list_newcdr) > 0: bulk_create_cdrs(list_newcdr, list_cdrid) (list_newcdr, list_cdrid) = ([], []) log_print(logger, shell, 'TASK :: run_cdr_import -> func import_cdr count_imported:%d' % count_imported) return (True, count_imported)
def generate_fake_cdr(cls, day_delta_int): """return tuples of fake CDR data""" # HANGUP_CAUSE = ['NORMAL_CLEARING', 'USER_BUSY', 'NO_ANSWER', 'CALL_REJECTED', 'INVALID_NUMBER_FORMAT'] # HANGUP_CAUSE_Q850 = ['16', '17', '19', '21', '28'] """ HANGUP_CAUSE = ['NORMAL_CLEARING', 'USER_BUSY', 'NO_ANSWER'] HANGUP_CAUSE_Q850 = ['16', '17', '19'] rand_hangup = random.randint(0, len(HANGUP_CAUSE)-1) hangup_cause = HANGUP_CAUSE[rand_hangup] hangup_cause_q850 = HANGUP_CAUSE_Q850[rand_hangup] if hangup_cause == 'NORMAL_CLEARING': duration = random.randint(1, 200) billsec = random.randint(1, 200) else: duration = 0 billsec = 0 """ # from django.db.models import Q # import operator # predicates = [ # ('code__exact', 16), ('code__exact', 17), ('code__exact', 18) # ] # q_list = [Q(x) for x in predicates] # list_hg = HangupCause.objects.filter(reduce(operator.or_, q_list)) HANGUP_CAUSE_Q850 = [16, 17, 19] list_hg = HangupCause.objects.filter(code__in=HANGUP_CAUSE_Q850) hangupcause = list_hg[random.randint(0, len(list_hg) - 1)] if hangupcause.code != 16: # Increase chances to have Answered calls hangupcause = list_hg[random.randint(0, len(list_hg) - 1)] hangup_cause_q850 = hangupcause.code if hangupcause.code == 16: duration = random.randint(1, 200) billsec = random.randint(1, 200) else: duration = 0 billsec = 0 delta_days = random.randint(0, day_delta_int) delta_minutes = random.randint(1, 1440) answer_stamp = datetime.datetime.now() \ - datetime.timedelta(minutes=delta_minutes) \ - datetime.timedelta(days=delta_days) # convert answer_stamp into milliseconds start_uepoch = int(time.mktime(answer_stamp.timetuple())) caller_id = ''.join([random.choice(string.digits) for i in range(8)]) channel_name = 'sofia/internal/' + caller_id + '@127.0.0.1' destination_number = ''.join( [random.choice(string.digits) for i in range(8)]) if random.randint(1, 20) == 1: # Add local calls dialcode = None country_id = None authorized = 1 destination_number = ''.join( [random.choice(string.digits) for i in range(5)]) else: # International calls destination_number = random.choice( COUNTRY_PREFIX) + destination_number from cdr_alert.functions_blacklist import verify_auth_dest_number destination_data = verify_auth_dest_number(destination_number) authorized = destination_data['authorized'] country_id = destination_data['country_id'] dialcode = destination_data['prefix_id'] end_stamp = str(datetime.datetime.now() - datetime.timedelta(minutes=delta_minutes) - datetime.timedelta(days=delta_days) + datetime.timedelta(seconds=duration)) callid = str(uuid1()) cdr_source_type = CDR_SOURCE_TYPE.FREESWITCH direction = CALL_DIRECTION.INBOUND return ( callid, answer_stamp, start_uepoch, caller_id, channel_name, destination_number, dialcode, hangupcause, hangup_cause_q850, duration, billsec, end_stamp, cdr_source_type, authorized, country_id, direction, # accountcode, buy_rate, buy_cost, sell_rate, sell_cost )
def setUp(self): """Create model object""" self.user = User.objects.get(username='******') # AlertRemovePrefix model self.alert_remove_prefix = AlertRemovePrefix(label='test', prefix=32) self.alert_remove_prefix.save() self.assertEquals(self.alert_remove_prefix.__unicode__(), 'test') # Alarm model self.alarm = Alarm(user=self.user, name='Alarm name', period=1, type=1, alert_condition=1, alert_value=10, alert_condition_add_on=1, status=1, email_to_send_alarm='*****@*****.**') self.alarm.save() self.assertEquals(self.alarm.__unicode__(), 'Alarm name') self.alarm_new = Alarm(user=self.user, name='Alarm name new', period=1, type=1, alert_condition=2, alert_value=10, alert_condition_add_on=2, status=1, email_to_send_alarm='*****@*****.**') self.alarm_new.save() self.alarm_new = Alarm(user=self.user, name='Alarm name new', period=1, type=1, alert_condition=3, alert_value=10, alert_condition_add_on=1, status=1, email_to_send_alarm='*****@*****.**') self.alarm_new.save() self.alarm_new = Alarm(user=self.user, name='Alarm name new', period=1, type=1, alert_condition=4, alert_value=10, alert_condition_add_on=1, status=1, email_to_send_alarm='*****@*****.**') self.alarm_new.save() self.alarm_new = Alarm(user=self.user, name='Alarm name new', period=1, type=1, alert_condition=5, alert_value=10, alert_condition_add_on=1, status=1, email_to_send_alarm='*****@*****.**') self.alarm_new.save() self.alarm_new = Alarm(user=self.user, name='Alarm name new', period=1, type=1, alert_condition=6, alert_value=10, alert_condition_add_on=1, status=1, email_to_send_alarm='*****@*****.**') self.alarm_new.save() self.alarm_new = Alarm(user=self.user, name='Alarm name new', period=2, type=1, alert_condition=3, alert_value=10, alert_condition_add_on=2, status=1, email_to_send_alarm='*****@*****.**') self.alarm_new.save() self.alarm_new = Alarm(user=self.user, name='Alarm name new', period=2, type=1, alert_condition=4, alert_value=10, alert_condition_add_on=2, status=1, email_to_send_alarm='*****@*****.**') self.alarm_new.save() self.alarm_new = Alarm(user=self.user, name='Alarm name new', period=3, type=1, alert_condition=5, alert_value=10, alert_condition_add_on=2, status=1, email_to_send_alarm='*****@*****.**') self.alarm_new.save() self.alarm_new = Alarm(user=self.user, name='Alarm name new', period=3, type=1, alert_condition=6, alert_value=10, alert_condition_add_on=2, status=1, email_to_send_alarm='*****@*****.**') self.alarm_new.save() # AlarmReport model self.alarm_report = AlarmReport(alarm=self.alarm, calculatedvalue=10, status=1) self.alarm_report.save() self.assertEquals(self.alarm_report.__unicode__(), 'Alarm name') self.country = Country.objects.get(pk=198) # Blacklist model self.blacklist = Blacklist(user=self.user, phonenumber_prefix=32, country=self.country) self.blacklist.save() self.assertTrue(self.blacklist.__unicode__()) # Whitelist model self.whitelist = Whitelist(user=self.user, phonenumber_prefix=32, country=self.country) self.whitelist.save() self.assertTrue(self.whitelist.__unicode__()) verify_auth_dest_number('9999787424')
def push_asterisk_cdr(shell, table_name, db_engine, cursor, cursor_updated, switch, ipaddress): """ function to import and aggreagate Asterisk CDR """ cdr_source_type = CDR_SOURCE_TYPE.ASTERISK count_import = 0 # Each time the task is running we will only take 1000 records to import # This define the max speed of import, this limit could be changed if db_engine == 'mysql': cursor.execute("SELECT dst, UNIX_TIMESTAMP(calldate), clid, channel," "duration, billsec, disposition, accountcode, uniqueid," " %s FROM %s WHERE import_cdr=0 LIMIT 0, 1000" % (settings.ASTERISK_PRIMARY_KEY, table_name)) elif db_engine == 'pgsql': cursor.execute("SELECT dst, extract(epoch FROM calldate), clid, channel," "duration, billsec, disposition, accountcode, uniqueid," " %s FROM %s WHERE import_cdr=0 LIMIT 1000" % (settings.ASTERISK_PRIMARY_KEY, table_name)) row = cursor.fetchone() # Store cdr in list to insert by bulk local_count_import = 0 batch_count = 0 acctid_list = '' while row is not None: destination_number = row[0] if not destination_number: # don't import CDR with no destination number continue acctid = row[9] callerid = row[2] try: m = re.search('"(.+?)" <(.+?)>', callerid) callerid_name = m.group(1) callerid_number = m.group(2) except: callerid_name = '' callerid_number = callerid channel = row[3] if not channel: channel = '' # Set empty string for channel in case is None duration = set_int_default(row[4], 0) billsec = set_int_default(row[5], 0) hangup_cause_id = translate_disposition(row[6]) accountcode = row[7] uniqueid = row[8] start_uepoch = datetime.fromtimestamp(int(row[1])) # Check Destination number if len(destination_number) <= settings.INTERNAL_CALL or destination_number[:1].isalpha(): authorized = 1 country_id = 999 else: destination_data = verify_auth_dest_number(destination_number) authorized = destination_data['authorized'] country_id = destination_data['country_id'] # Option to get the direction from user_field direction = "unknown" try: voipplan_id = UserProfile.objects.get(accountcode=accountcode).voipplan_id except: voipplan_id = False print_shell(shell, "No VoipPlan created for this user/accountcode") try: user = UserProfile.objects.get(accountcode=accountcode).user except: # Cannot assign accountcode to an existing user # we will then assign to admin user = User.objects.filter(is_superuser=True)[0] call_rate = calculate_call_cost(voipplan_id, destination_number, billsec) buy_rate = call_rate['buy_rate'] buy_cost = call_rate['buy_cost'] sell_rate = call_rate['sell_rate'] sell_cost = call_rate['sell_cost'] # Sanitize callerid_number = sanitize_cdr_field(callerid_number) callerid_name = sanitize_cdr_field(callerid_name) destination_number = sanitize_cdr_field(destination_number) channel = sanitize_cdr_field(channel) cdr_json = {'channel': channel} dialcode = '' cdr = CDR(user=user, switch=switch, cdr_source_type=cdr_source_type, callid=uniqueid, caller_id_number=callerid_number, destination_number=destination_number, dialcode_id=dialcode, starting_date=start_uepoch, duration=duration, billsec=billsec, hangup_cause=hangup_cause_id, direction=direction, country_id=country_id, authorized=authorized, accountcode=accountcode, buy_rate=buy_rate, buy_cost=buy_cost, sell_rate=sell_rate, sell_cost=sell_cost, data=cdr_json) cdr.save() # Append cdr to bulk_cdr list count_import = count_import + 1 local_count_import = local_count_import + 1 batch_count = batch_count + 1 acctid_list += "%s, " % str(acctid) if batch_count == 100: acctid_list = acctid_list[:-2] # trim last comma (,) from string # Postgresql # select * from table_name where id in (1,2,3); update_cdr = "UPDATE %s SET import_cdr=1 WHERE %s in (%s)" % \ (table_name, settings.ASTERISK_PRIMARY_KEY, acctid_list) cursor_updated.execute(update_cdr) batch_count = 0 acctid_list = '' # Fetch a other record row = cursor.fetchone() if len(acctid_list) > 0: acctid_list = acctid_list[:-2] # trim last comma (,) from string # Postgresql # select * from table_name where id in (1,2,3); update_cdr = "UPDATE %s SET import_cdr=1 WHERE %s in (%s)" % \ (table_name, settings.ASTERISK_PRIMARY_KEY, acctid_list) cursor_updated.execute(update_cdr) return count_import
def setUp(self): """Create model object""" self.user = User.objects.get(username='******') # AlertRemovePrefix model self.alert_remove_prefix = AlertRemovePrefix( label='test', prefix=32 ) self.alert_remove_prefix.save() self.assertEquals(self.alert_remove_prefix.__unicode__(), 'test') # Alarm model self.alarm = Alarm( user=self.user, name='Alarm name', period=1, type=1, alert_condition=1, alert_value=10, alert_condition_add_on=1, status=1, email_to_send_alarm='*****@*****.**' ) self.alarm.save() self.assertEquals(self.alarm.__unicode__(), 'Alarm name') self.alarm_new = Alarm( user=self.user, name='Alarm name new', period=1, type=1, alert_condition=2, alert_value=10, alert_condition_add_on=2, status=1, email_to_send_alarm='*****@*****.**' ) self.alarm_new.save() self.alarm_new = Alarm( user=self.user, name='Alarm name new', period=1, type=1, alert_condition=3, alert_value=10, alert_condition_add_on=1, status=1, email_to_send_alarm='*****@*****.**' ) self.alarm_new.save() self.alarm_new = Alarm( user=self.user, name='Alarm name new', period=1, type=1, alert_condition=4, alert_value=10, alert_condition_add_on=1, status=1, email_to_send_alarm='*****@*****.**' ) self.alarm_new.save() self.alarm_new = Alarm( user=self.user, name='Alarm name new', period=1, type=1, alert_condition=5, alert_value=10, alert_condition_add_on=1, status=1, email_to_send_alarm='*****@*****.**' ) self.alarm_new.save() self.alarm_new = Alarm( user=self.user, name='Alarm name new', period=1, type=1, alert_condition=6, alert_value=10, alert_condition_add_on=1, status=1, email_to_send_alarm='*****@*****.**' ) self.alarm_new.save() self.alarm_new = Alarm( user=self.user, name='Alarm name new', period=2, type=1, alert_condition=3, alert_value=10, alert_condition_add_on=2, status=1, email_to_send_alarm='*****@*****.**' ) self.alarm_new.save() self.alarm_new = Alarm( user=self.user, name='Alarm name new', period=2, type=1, alert_condition=4, alert_value=10, alert_condition_add_on=2, status=1, email_to_send_alarm='*****@*****.**' ) self.alarm_new.save() self.alarm_new = Alarm( user=self.user, name='Alarm name new', period=3, type=1, alert_condition=5, alert_value=10, alert_condition_add_on=2, status=1, email_to_send_alarm='*****@*****.**' ) self.alarm_new.save() self.alarm_new = Alarm( user=self.user, name='Alarm name new', period=3, type=1, alert_condition=6, alert_value=10, alert_condition_add_on=2, status=1, email_to_send_alarm='*****@*****.**' ) self.alarm_new.save() # AlarmReport model self.alarm_report = AlarmReport( alarm=self.alarm, calculatedvalue=10, status=1 ) self.alarm_report.save() self.assertEquals(self.alarm_report.__unicode__(), 'Alarm name') self.country = Country.objects.get(pk=198) # Blacklist model self.blacklist = Blacklist( user=self.user, phonenumber_prefix=32, country=self.country ) self.blacklist.save() self.assertTrue(self.blacklist.__unicode__()) # Whitelist model self.whitelist = Whitelist( user=self.user, phonenumber_prefix=32, country=self.country ) self.whitelist.save() self.assertTrue(self.whitelist.__unicode__()) verify_auth_dest_number('9999787424')