def handle(self, fixture_labels, **options): from django.db.models import get_apps from django.core import serializers from django.db import connection, transaction from django.conf import settings import fsa.cdr self.style = no_style() verbosity = int(options.get('verbosity', 1)) show_traceback = options.get('traceback', False) # commit is a stealth option - it isn't really useful as # a command line option, but it can be useful when invoking # loaddata from within another script. # If commit=True, loaddata will use its own transaction; # if commit=False, the data load SQL will become part of # the transaction in place when loaddata was invoked. commit = options.get('commit', True) # Keep a count of the installed objects and fixtures fixture_count = 0 object_count = 0 models = set() humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path' # Get a cursor (even though we don't need one yet). This has # the side effect of initializing the test database (if # it isn't already initialized). cursor = connection.cursor() if commit: transaction.commit_unless_managed() transaction.enter_transaction_management() transaction.managed(True) class SingleZipReader(zipfile.ZipFile): def __init__(self, *args, **kwargs): zipfile.ZipFile.__init__(self, *args, **kwargs) if settings.DEBUG: assert len(self.namelist()) == 1, "Zip-compressed fixtures must contain only one file." def read(self): return zipfile.ZipFile.read(self, self.namelist()[0]) compression_types = { None: file, 'gz': gzip.GzipFile, 'zip': SingleZipReader } if has_bz2: compression_types['bz2'] = bz2.BZ2File #app_fixtures = os.path.join(os.path.dirname(fsa.cdr.__file__), 'fixtures') #print app_fixtures time_format = "%Y-%m-%d %H:%M:%S" objects_in_fixture = 0 for fixture_dir in os.listdir(fixture_labels): #if verbosity > 1: print "Checking %s for fixtures..." % humanize(fixture_dir) label_found = False fixture_count += 1 full_path = os.path.join(fixture_labels, fixture_dir) f = open(full_path, 'r') xml_cdr = Soup(f) f.close() try: objects_in_fixture += 1 new_cdr = Cdr(caller_id_name = xml_cdr.cdr.callflow.caller_profile.caller_id_name.string, caller_id_number = xml_cdr.cdr.callflow.caller_profile.caller_id_number.string) new_cdr.accountcode = xml_cdr.cdr.variables.accountcode.string new_cdr.destination_number = xml_cdr.cdr.callflow.caller_profile.destination_number.string new_cdr.context = xml_cdr.cdr.callflow.caller_profile.context.string new_cdr.start_timestamp = datetime.datetime.utcfromtimestamp(time.mktime(time.strptime(urllib.unquote(xml_cdr.cdr.variables.start_stamp.string), time_format))) #new_cdr.start_timestamp = '2009-02-24 16:47:54.099098' new_cdr.answer_timestamp = datetime.datetime.utcfromtimestamp(time.mktime(time.strptime(urllib.unquote(xml_cdr.cdr.variables.answer_stamp.string), time_format))) new_cdr.end_timestamp = datetime.datetime.utcfromtimestamp(time.mktime(time.strptime(urllib.unquote(xml_cdr.cdr.variables.end_stamp.string), time_format))) new_cdr.duration = xml_cdr.cdr.variables.duration.string new_cdr.billsec = xml_cdr.cdr.variables.billsec.string new_cdr.hangup_cause = xml_cdr.cdr.variables.hangup_cause.string new_cdr.uuid = xml_cdr.cdr.callflow.caller_profile.uuid.string new_cdr.read_codec = xml_cdr.cdr.variables.read_codec.string new_cdr.write_codec = xml_cdr.cdr.variables.write_codec.string #print xml_cdr.cdr.variables.answer_stamp.string new_cdr.save() object_count += objects_in_fixture label_found = True except Exception: import traceback transaction.rollback() transaction.leave_transaction_management() if show_traceback: traceback.print_exc() else: sys.stderr.write(self.style.ERROR("Problem installing fixture '%s': %s\n" % (full_path, ''.join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))))) return # If the fixture we loaded contains 0 objects, assume that an # error was encountered during fixture loading. if objects_in_fixture == 0: sys.stderr.write(self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" % (fixture_name))) transaction.rollback() transaction.leave_transaction_management() return #filename = "tmp/lcr.csv" #reader = csv.reader(open(filename, "rb"), delimiter=';') #try: # for row in reader: # print row # except csv.Error, e: # sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) #call_command('loaddata', 'alias.xml', 'server.xml', 'sipprofile.xml', 'fsgroup.xml', 'context.xml', interactive=True) # If we found even one object in a fixture, we need to reset the # database sequences. if object_count > 0: sequence_sql = connection.ops.sequence_reset_sql(self.style, models) if sequence_sql: if verbosity > 1: print "Resetting sequences" for line in sequence_sql: cursor.execute(line) if commit: transaction.commit() transaction.leave_transaction_management() if object_count == 0: if verbosity > 1: print "No fixtures found." else: if verbosity > 0: print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count) # Close the DB connection. This is required as a workaround for an # edge case in MySQL: if the same connection is used to # create tables, load data, and query, the query can return # incorrect results. See Django #7572, MySQL #37735. if commit: connection.close()
def create(self, request): time_format = "%Y-%m-%d %H:%M:%S" attrs = self.flatten_dict(request.POST) user = request.user #log.debug(attrs.get('cdr')) #xml_cdr = Soup(request.raw_post_data) #parser = etree.XMLParser(remove_blank_text=True) #root = "<root><tag>text</tag><aa><bb>bbb</bb><cc>ccc</cc></aa></root>" #tree = etree.fromstring(root, parser) #return rc.NOT_HERE if user.has_perm("cdr.add_cdr"): try: xml_cdr = Soup(attrs.get('cdr')) if xml_cdr.cdr.callflow.caller_profile.caller_id_number is not None: caller_id_number=xml_cdr.cdr.callflow.caller_profile.caller_id_number.string else: caller_id_number='000000' if xml_cdr.cdr.callflow.caller_profile.caller_id_name.string is not None: caller_id_name = xml_cdr.cdr.callflow.caller_profile.caller_id_name.string else: caller_id_name = caller_id_number new_cdr = Cdr(caller_id_name=caller_id_name, caller_id_number=caller_id_number) #log.debug("caller_id_number: {0} caller_id_name: {1}".format(caller_id_number, caller_id_name)) #new_cdr.procesed = 1 # TODO: New stats (n CDRs) rtcp_packet_count and rtcp_octet_count if xml_cdr.cdr.variables.lcr_rate is not None: log.debug("lcr_rate %s" % xml_cdr.cdr.variables.lcr_rate.string) new_cdr.lcr_rate = trunc_decimal(xml_cdr.cdr.variables.lcr_rate.string, 4) if xml_cdr.cdr.variables.lcr_currency is not None: new_cdr.lprice_currency = xml_cdr.cdr.variables.lcr_currency.string else: new_cdr.lprice_currency = "UAH" if xml_cdr.cdr.variables.lcr_price is not None: new_cdr.lprice = trunc_decimal(xml_cdr.cdr.variables.lcr_price.string, 4) else: new_cdr.lprice = trunc_decimal("0.18", 4) if xml_cdr.cdr.variables.lcr_name is not None: new_cdr.lcr_name = xml_cdr.cdr.variables.lcr_name.string if xml_cdr.cdr.variables.lcr_digits is not None: new_cdr.lcr_digits = int(xml_cdr.cdr.variables.lcr_digits.string) if xml_cdr.cdr.variables.lcr_carrier is not None: log.debug("lcr_carrier %s" % xml_cdr.cdr.variables.lcr_carrier.string) new_cdr.lcr_carrier = xml_cdr.cdr.variables.lcr_carrier.string if xml_cdr.cdr.variables.lcr_direction is not None and xml_cdr.cdr.variables.lcr_direction.string == 'in': # входящий звонок new_cdr.direction = 1 new_cdr.nibble_rate = trunc_decimal("0.0", 4) elif xml_cdr.cdr.variables.lcr_direction is not None and xml_cdr.cdr.variables.lcr_direction.string == 'out': # исходящий звонок new_cdr.direction = 2 if xml_cdr.cdr.variables.nibble_rate is not None: log.debug("nibble_rate %s" % xml_cdr.cdr.variables.nibble_rate.string) new_cdr.nibble_rate = trunc_decimal(xml_cdr.cdr.variables.nibble_rate.string, 4) else: new_cdr.nibble_rate = trunc_decimal("0.0", 4) else: new_cdr.direction = 0 new_cdr.nibble_rate = trunc_decimal("0.0", 4) if xml_cdr.cdr.variables.nibble_current_balance is not None: #<nibble_total_billed>0.087786</nibble_total_billed> log.debug("nibble_current_balance %s" % xml_cdr.cdr.variables.nibble_current_balance.string) new_cdr.nibble_current_balance = trunc_decimal(xml_cdr.cdr.variables.nibble_current_balance.string, 6) if xml_cdr.cdr.variables.nibble_tariff is not None: new_cdr.nibble_tariff = int(xml_cdr.cdr.variables.nibble_tariff.string) if xml_cdr.cdr.variables.nibble_account is not None: new_cdr.nibble_account = int(xml_cdr.cdr.variables.nibble_account.string) if xml_cdr.cdr.variables.site_id is not None: new_cdr.site_id = int(xml_cdr.cdr.variables.site_id.string) if xml_cdr.cdr.variables.billusec is not None: new_cdr.billusec = int(xml_cdr.cdr.variables.billusec.string) log.debug("billusec: %i" % int(xml_cdr.cdr.variables.billusec.string)) #elif xml_cdr.cdr.channel_data.direction is not None: #log.debug("direction %s" % xml_cdr.cdr.channel_data.direction.string) #if xml_cdr.cdr.channel_data.direction.string == 'inbound': #new_cdr.direction = 1 #elif xml_cdr.cdr.channel_data.direction.string == 'outbound': #new_cdr.direction = 2 #l.debug("billsec: %i %i" % xml_cdr.cdr.variables.billsec, xml_cdr.cdr.variables.billmsec) if xml_cdr.cdr.variables.accountcode is not None: log.debug("accountcode %s" % xml_cdr.cdr.variables.accountcode.string) new_cdr.accountcode = xml_cdr.cdr.variables.accountcode.string else: new_cdr.accountcode = "none" if xml_cdr.cdr.variables.sip_received_ip is not None: log.debug("sip_received_ip %s" % xml_cdr.cdr.variables.sip_received_ip.string) new_cdr.sip_received_ip = xml_cdr.cdr.variables.sip_received_ip.string if xml_cdr.cdr.variables.number_alias is not None: log.debug("number_alias %s" % xml_cdr.cdr.variables.number_alias.string) new_cdr.number_alias = xml_cdr.cdr.variables.number_alias.string new_cdr.destination_number = xml_cdr.cdr.callflow.caller_profile.destination_number.string log.debug("destination_number %s" % xml_cdr.cdr.callflow.caller_profile.destination_number.string) new_cdr.context = xml_cdr.cdr.callflow.caller_profile.context.string log.debug("context %s" % xml_cdr.cdr.callflow.caller_profile.context.string) new_cdr.start_timestamp = datetime.datetime.utcfromtimestamp( time.mktime(time.strptime(urllib.unquote(xml_cdr.cdr.variables.start_stamp.string), time_format))) log.debug("start_stamp: %s" % new_cdr.start_timestamp) if xml_cdr.cdr.variables.answer_stamp is not None: new_cdr.answer_timestamp = datetime.datetime.utcfromtimestamp(time.mktime(time.strptime(urllib.unquote(xml_cdr.cdr.variables.answer_stamp.string), time_format))) else: new_cdr.answer_timestamp = new_cdr.start_timestamp log.debug("answer_stamp:{0}".format(new_cdr.answer_timestamp)) if xml_cdr.cdr.variables.end_stamp is not None: new_cdr.end_timestamp = datetime.datetime.utcfromtimestamp(time.mktime(time.strptime(urllib.unquote(xml_cdr.cdr.variables.end_stamp.string), time_format))) else: new_cdr.end_timestamp = new_cdr.answer_timestamp log.debug("answer_stamp:{0}".format(new_cdr.end_timestamp)) try: new_cdr.duration = xml_cdr.cdr.variables.duration.string except Exception as e: log.error(e) new_cdr.duration = '0' log.debug("duration: {0}".format(new_cdr.duration)) new_cdr.billsec = int(xml_cdr.cdr.variables.billsec.string) log.debug("billsec {0}".format(xml_cdr.cdr.variables.billsec.string)) new_cdr.hangup_cause = xml_cdr.cdr.variables.hangup_cause.string log.debug("hangup_cause {0}".format(xml_cdr.cdr.variables.hangup_cause.string)) new_cdr.uuid = xml_cdr.cdr.callflow.caller_profile.uuid.string log.debug("uuid {0}".format(xml_cdr.cdr.callflow.caller_profile.uuid.string)) try: new_cdr.read_codec = xml_cdr.cdr.variables.read_codec.string except Exception as e: #log.error(e) # TODO: add to None new_cdr.read_codec = 'PCMA' log.debug("read_codec{0}".format(new_cdr.read_codec)) try: new_cdr.write_codec = xml_cdr.cdr.variables.write_codec.string except Exception as e: #log.error(e) new_cdr.write_codec = new_cdr.read_codec log.debug("write_codec {0}".format(new_cdr.write_codec)) if xml_cdr.cdr.variables.rtp_audio_in_raw_bytes is not None: new_cdr.rtp_audio_in_raw_bytes = int(xml_cdr.cdr.variables.rtp_audio_in_raw_bytes.string) if xml_cdr.cdr.variables.rtp_audio_in_media_bytes is not None: new_cdr.rtp_audio_in_media_bytes = int(xml_cdr.cdr.variables.rtp_audio_in_media_bytes.string) if xml_cdr.cdr.variables.rtp_audio_in_packet_count is not None: new_cdr.rtp_audio_in_packet_count = int(xml_cdr.cdr.variables.rtp_audio_in_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_in_media_packet_count is not None: new_cdr.rtp_audio_in_media_packet_count = int( xml_cdr.cdr.variables.rtp_audio_in_media_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_in_skip_packet_count is not None: new_cdr.rtp_audio_in_skip_packet_count = int( xml_cdr.cdr.variables.rtp_audio_in_skip_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_in_jb_packet_count is not None: new_cdr.rtp_audio_in_jb_packet_count = int(xml_cdr.cdr.variables.rtp_audio_in_jb_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_in_dtmf_packet_count is not None: new_cdr.rtp_audio_in_dtmf_packet_count = int( xml_cdr.cdr.variables.rtp_audio_in_dtmf_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_in_cng_packet_count is not None: new_cdr.rtp_audio_in_cng_packet_count = int(xml_cdr.cdr.variables.rtp_audio_in_cng_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_in_flush_packet_count is not None: new_cdr.rtp_audio_in_flush_packet_count = int( xml_cdr.cdr.variables.rtp_audio_in_flush_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_out_raw_bytes is not None: new_cdr.rtp_audio_out_raw_bytes = int(xml_cdr.cdr.variables.rtp_audio_out_raw_bytes.string) if xml_cdr.cdr.variables.rtp_audio_out_media_bytes is not None: new_cdr.rtp_audio_out_media_bytes = int(xml_cdr.cdr.variables.rtp_audio_out_media_bytes.string) if xml_cdr.cdr.variables.rtp_audio_out_packet_count is not None: new_cdr.rtp_audio_out_packet_count = int(xml_cdr.cdr.variables.rtp_audio_out_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_out_media_packet_count is not None: new_cdr.rtp_audio_out_media_packet_count = int( xml_cdr.cdr.variables.rtp_audio_out_media_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_out_skip_packet_count is not None: new_cdr.rtp_audio_out_skip_packet_count = int( xml_cdr.cdr.variables.rtp_audio_out_skip_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_out_dtmf_packet_count is not None: new_cdr.rtp_audio_out_dtmf_packet_count = int( xml_cdr.cdr.variables.rtp_audio_out_dtmf_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_out_cng_packet_count is not None: new_cdr.rtp_audio_out_cng_packet_count = int( xml_cdr.cdr.variables.rtp_audio_out_cng_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_rtcp_packet_count is not None: new_cdr.rtp_audio_rtcp_packet_count = int(xml_cdr.cdr.variables.rtp_audio_rtcp_packet_count.string) if xml_cdr.cdr.variables.rtp_audio_rtcp_octet_count is not None: new_cdr.rtp_audio_rtcp_octet_count = int(xml_cdr.cdr.variables.rtp_audio_rtcp_octet_count.string) if xml_cdr.cdr.variables.nibble_total_billed is not None: #<nibble_total_billed>0.087786</nibble_total_billed> log.debug("nibble_total_billed {0}".format(xml_cdr.cdr.variables.nibble_total_billed.string)) new_cdr.nibble_total_billed = trunc_decimal(xml_cdr.cdr.variables.nibble_total_billed.string, 6) #sip_user_agent #if new_cdr.lcr_rate > Decimal("0") and new_cdr.nibble_rate > Decimal("0") and new_cdr.billsec > 0: if new_cdr.lcr_rate > Decimal("0") and new_cdr.billusec > 0: #new_cdr.cash = new_cdr.nibble_rate/60*new_cdr.billsec new_cdr.cash = new_cdr.lcr_rate / 1000000 / 60 * new_cdr.billusec if new_cdr.cash is not None and new_cdr.nibble_total_billed is not None: new_cdr.marja = new_cdr.cash - new_cdr.nibble_total_billed #try: #from fsb.billing.models import Balance #bal = Balance.objects.get(accountcode__username__exact=new_cdr.accountcode) #bal.cash -= new_cdr.cash #bal.save() #except: #pass #<endpoint_disposition>ANSWER</endpoint_disposition> #<proto_specific_hangup_cause>sip%3A200</proto_specific_hangup_cause> #<sip_hangup_phrase>OK</sip_hangup_phrase> ## <sip_use_codec_name>GSM</sip_use_codec_name> ## <sip_use_codec_rate>8000</sip_use_codec_rate> ## <sip_use_codec_ptime>20</sip_use_codec_ptime> ## <read_codec>GSM</read_codec> ## <read_rate>8000</read_rate> ## <write_codec>GSM</write_codec> ## <write_rate>8000</write_rate> new_cdr.save() log.debug("caller_id_name {0}".format(new_cdr.caller_id_name)) log.debug("caller_id_number {0}".format(new_cdr.caller_id_number)) #log.debug("bridge_channel %s" % new_cdr.bridge_channel) #resp = rc.CREATED resp = rc.ALL_OK #resp.write(endpoint) return resp except Exception as e: log.error(e) return rc.NOT_HERE else: return rc.FORBIDDEN