예제 #1
0
파일: tests.py 프로젝트: banabana/fsb
    def testTariffLoad(self):
        """docstring for billing core"""
        from currency.money import Money
        #from currency.models import Currency
        #from django.contrib.sites.models import RequestSite
        from django.contrib.sites.models import Site

        f = open(
            os.path.join(os.path.dirname(__file__), 'fixtures',
                         'tariff_test.csv'), "rt")
        #tf = TariffPlan.objects.get(enabled=True, primary=True)
        save_cnt = 0
        tariff = 1
        site = 1
        format_csv = 1
        fixture_count = 0
        object_count = 0

        try:
            cd = CsvData(
                "delimiter=';'time_format='%d.%m.%Y'country_code|operator_type|name|pref_digits|price|rate|currency|weeks|time_start|time_end|date_start|time_round|code"
            )
            reader = csv.reader(f, delimiter=';', dialect='excel')
            tf = TariffPlan.objects.get(pk=tariff, enabled=True, site__pk=site)
            s = Site.objects.get(pk=site)
            for row in reader:
                try:
                    country_list, country_code, n = cd.parse(row)
                    log.debug("country_code {0} ({1})".format(
                        country_code, country_list))
                    for country in country_list:
                        n['country_code'] = country_code
                        digits = country
                        price = n['price']
                        log.debug("digits {0}".format(country))
                        if n['weeks'] is not None:
                            if n['weeks'] == "all":
                                n['week'] = 0
                                objects_in_fixture = Tariff.objects.add_tariff(
                                    tf, n, country, price)
                                object_count += objects_in_fixture
                            else:
                                for i in eval(n['weeks']):
                                    n['week'] = int(i)
                                    objects_in_fixture = Tariff.objects.add_tariff(
                                        tf, n, country, price)
                                    object_count += objects_in_fixture
                except Exception, e:
                    log.error("line: {0} => {1}".format(cd.line_num, e))
                    pass
            label_found = True
예제 #2
0
파일: tests.py 프로젝트: banabana/fsb
    def testTariffLoad(self):
        """docstring for billing core"""
        from currency.money import Money
        #from currency.models import Currency
        #from django.contrib.sites.models import RequestSite
        from django.contrib.sites.models import Site

        f = open(os.path.join(os.path.dirname(__file__), 'fixtures', 'tariff_test.csv'), "rt")
        #tf = TariffPlan.objects.get(enabled=True, primary=True)
        save_cnt = 0
        tariff=1
        site = 1
        format_csv = 1
        fixture_count = 0
        object_count = 0

        try:
            cd = CsvData("delimiter=';'time_format='%d.%m.%Y'country_code|operator_type|name|pref_digits|price|rate|currency|weeks|time_start|time_end|date_start|time_round|code")
            reader = csv.reader(f, delimiter=';', dialect='excel')
            tf = TariffPlan.objects.get(pk=tariff, enabled=True, site__pk=site)
            s = Site.objects.get(pk=site)
            for row in reader:
                try:
                    country_list, country_code, n = cd.parse(row)
                    log.debug("country_code {0} ({1})".format(country_code, country_list))
                    for country in country_list:
                        n['country_code'] = country_code
                        digits = country
                        price = n['price']
                        log.debug("digits {0}".format(country))
                        if n['weeks'] is not None:
                            if n['weeks'] == "all":
                                n['week'] = 0
                                objects_in_fixture = Tariff.objects.add_tariff(tf, n, country, price)
                                object_count += objects_in_fixture
                            else:
                                for i in eval(n['weeks']):
                                    n['week'] = int(i)
                                    objects_in_fixture = Tariff.objects.add_tariff(tf, n, country, price)
                                    object_count += objects_in_fixture
                except Exception, e:
                    log.error("line: {0} => {1}".format(cd.line_num, e))
                    pass
            label_found = True
예제 #3
0
파일: tests.py 프로젝트: banabana/fsb
 def setUp(self):
     # Every test needs a client.
     self.client = Client()
     self.auth_string = 'Basic {0:>s}'.format(base64.encodestring('alice:swordfish').rstrip())
     self.auth_string_error = 'Basic {0:>s}'.format(base64.encodestring('alice:alice').rstrip())
     self.site = Site.objects.get_current()
     self.user = User.objects.create_user('test', '*****@*****.**', 'test')
     try:
         #f = open(os.path.join(os.path.dirname(__file__), 'fixtures', 'test_all.csv'), "rt")
         f = open(os.path.join(os.path.dirname(__file__), 'fixtures', 'test_prepaid.csv'), "rt")
         save_cnt = 0
         cd = CsvData("delimiter=';'time_format='%d.%m.%Y'num_prepaid|code|rate|nt|zeros|date_end")
         reader = csv.reader(f, delimiter=';', dialect='excel')
         for row in reader:
             try:
                 n = cd.parse(row)
                 objects_in_fixture = Prepaid.objects.add_prepaid(n)
             except Exception, e:
                 l.error("line: {0} => {1}".format(cd.line_num, e))
         #objects_in_fixture = Prepaid.objects.load_prepaid(c, site, f)
         label_found = True
예제 #4
0
파일: managers.py 프로젝트: banabana/fsb
 def load_tariff(self, tf, base_file):
     """
     Загрузка данных из csv файла
     для успешной lcr загрузки необходимо в таблице gateway конфигурации
     прописать lcr_format 
     """
     save_cnt = 0
     try:
         cd = CsvData(tf.tariff_format)
         reader = csv.reader(base_file, delimiter=';', dialect='excel')
         no_base = []
         for row in reader:
             save_flag = False
             n = {}
             row_save = []
             n['country_code'] = ''
             n["name"] = False
             n['special_digits'] = False
             n['date_start'] = datetime.datetime.now()
             n['date_end'] = datetime.datetime.max
             for index, c in enumerate(cd.data_col):
                 try:
                     #l.debug("%s=%s" % (c,row[index].strip()))
                     if c != 'zeros' and len(row[index].strip()) > 0:
                         if c == 'name':
                             n["name"] = row[index].strip()
                         elif c == 'lcr':
                             n["lcr"] = row[index].strip()
                         elif c == 'rate':
                             n['rate'] = cd.set_num(row[index].strip())
                         elif c == 'country_code':
                             n['country_code'] = row[index].strip()
                         elif c == 'special_digits':
                             save_flag = True
                             n["special_digits"] = row[index].strip()
                         elif c == 'date_start' and len(
                                 row[index].strip()) > 1:
                             n['date_start'] = cd.set_time(
                                 row[index].strip())
                         elif c == 'date_end' and len(
                                 row[index].strip()) > 1:
                             n['date_end'] = cd.set_time(row[index].strip())
                         elif c == 'digits':
                             save_flag = True
                             n["digits"] = row[index].strip()
                         elif row[index].strip() != '':
                             n[c] = row[index].strip()
                 except:
                     pass
             if save_flag:
                 if not n['name']:
                     n['name'] = n['lcr']
                 if n['special_digits']:
                     l.debug(n['special_digits'])
                     for dig in n['special_digits'].split(';'):
                         digit = dig.split('-')
                         if len(digit) == 2:
                             for digits in range(int(digit[0]),
                                                 int(digit[1]) + 1):
                                 d = '%s%s' % (n['country_code'].strip(),
                                               digits)
                                 l.debug('digits: %s/%s/' % (d, n["name"]))
                                 save_cnt += self.add_tariff(tf, n, d)
                         elif len(dig) > 0 and dig != '':
                             d = '%s%s' % (n['country_code'], dig.strip())
                             l.debug('digits: %s/%s/%s/' %
                                     (d, n["name"], dig))
                             save_cnt += self.add_tariff(tf, n, d)
                 elif n["digits"] != '':
                     d = '%s%s' % (n['country_code'], n["digits"])
                     save_cnt += self.add_tariff(tf, n, d)
                     l.debug('digits: %s/%s/' % (d, n["name"]))
             n.clear()
     except csv.Error, e:
         l.error('line %d: %s' % (reader.line_num, e))
예제 #5
0
파일: managers.py 프로젝트: banabana/fsb
 def load_tariff(self, gw, base_file):
     """
     Загрузка данных из csv файла
     для успешной lcr загрузки необходимо в таблице gateway конфигурации
     прописать lcr_format 
     """
     save_cnt = 0
     try:
         cd = CsvData(gw.lcr_format)
         reader = csv.reader(base_file, delimiter=';', dialect='excel')
         no_base = []
         for row in reader:
             save_flag = False
             n = {}
             row_save = []
             n['country_code'] = ''
             n['special_digits'] = False
             n['date_start'] = datetime.datetime.now()
             n['date_end'] = datetime.datetime.max
             for index, c in enumerate(cd.data_col):
                 try:
                     #l.debug("%s=%s" % (c,row[index].strip()))
                     if c != 'zeros' and len(row[index].strip()) > 0:
                         if c == 'name':
                             n["name"] = row[index].strip()
                         elif c == 'rate':
                             n['rate'] = cd.set_num(row[index].strip())
                         elif c == 'country_code':
                             n['country_code'] = row[index].strip()
                         elif c == 'special_digits':
                             save_flag = True
                             n["special_digits"] = row[index].strip()
                         elif c == 'date_start' and len(row[index].strip()) > 1:
                             n['date_start'] = cd.set_time(row[index].strip())
                         elif c == 'date_end' and len(row[index].strip()) > 1:
                             n['date_end'] = cd.set_time(row[index].strip())
                         elif c == 'digits':
                             save_flag = True
                             n["digits"] = row[index].strip()
                         elif row[index].strip() != '':
                             n[c]=row[index].strip()
                 except:
                     pass
             if save_flag:
                 if n['special_digits']:
                     l.debug(n['special_digits'])
                     for dig in n['special_digits'].split(';'):
                         digit = dig.split('-')
                         if len(digit) == 2:
                             for digits in range(int(digit[0]), int(digit[1])+1):
                                 d = '%s%s' % (n['country_code'].strip(), digits)
                                 l.debug('digits: %s/%s/' % (d,n["name"]))
                                 save_cnt += self.add_lcr(gw, n, d)
                         elif len(dig) > 0 and dig != '':
                             d = '%s%s' % (n['country_code'], dig.strip())
                             l.debug('digits: %s/%s/%s/' % (d,n["name"],dig))
                             save_cnt += self.add_lcr(gw, n, d)
                 elif n["digits"] != '':
                     d = '%s%s' % (n['country_code'], n["digits"])
                     save_cnt += self.add_lcr(gw, n, d)
                     l.debug('digits: %s/%s/' % (d,n["name"]))
             n.clear()
     except csv.Error, e:
         l.error('line %d: %s' % (reader.line_num, e))
예제 #6
0
    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
        from fsb.tariff.models import TariffPlan, Tariff
        from django.contrib.sites.models import RequestSite
        from django.contrib.sites.models import Site

        tariff = options.get('tariff',1)
        site = options.get('site',1)
        format_csv = options.get('format_csv',1)

        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
            
        f = open(fixture_labels, "rt")
        try:
            tf = TariffPlan.objects.get(pk=tariff, enabled=True, site__pk=site)
            s = Site.objects.get(pk=site)
            #d1="delimiter=';'time_format='%d.%m.%Y 00:00'name|country_code|special_digits|rate"
            csb = CsvBase.objects.get(pk=format_csv)
            cd = CsvData(csb.val)
            log.debug(fixture_labels)
            reader = csv.reader(f, delimiter=';', dialect='excel')
            for row in reader:
                try:
                    #log.debug('row: {0}'.format(row))
                    country_list, country_code, n = cd.parse(row)
                    log.debug("country_code {0} ({1})".format(country_code, country_list))
                    for country in country_list:
                        n['country_code'] = country_code
                        digits = country
                        price = n['price']
                        log.debug("digits {0}".format(country))
                        if n['weeks'] is not None:
                            if n['weeks'] == "all":
                                n['week'] = 0
                                objects_in_fixture = Tariff.objects.add_tariff(tf, n, country, price)
                                object_count += objects_in_fixture
                            else:
                                for i in eval(n['weeks']):
                                    n['week'] = int(i)
                                    objects_in_fixture = Tariff.objects.add_tariff(tf, n, country, price)
                                    object_count += objects_in_fixture
                except Exception, e:
                    log.error("line: {0} => {1}".format(cd.line_num, e))
                    pass
            label_found = True
예제 #7
0
파일: tests.py 프로젝트: grengojbo/fsa
    def testLoadCSV(self):
        """docstring for testLoadCSV"""
        try:
            from fsa.lcr.models import Lcr
            from fsa.gateway.models import SofiaGateway

            # from currency.money import Money
            # from currency.models import Currency
            from django.contrib.sites.models import RequestSite
            from django.contrib.sites.models import Site

            # f = open(os.path.join(os.path.dirname(__file__), 'fixtures', 'test_all.csv'), "rt")
            f = open(os.path.join(os.path.dirname(__file__), "fixtures", "lcr_test.csv"), "rt")
            gw = 3
            site = 1
            gateway = SofiaGateway.objects.get(pk=gw, enabled=True)
            s = Site.objects.get(pk=site)
            save_cnt = 0
            # default_currency = 'GRN'
            # curency_grn = '8.11'
            # All
            # cd = CsvData("delimiter=';'time_format='%d.%m.%Y 00:00'country_code|name|other|pref_digits|rate|currency|brand")
            # Ukraina
            # cd = CsvData("delimiter=';'time_format='%d.%m.%Y 00:00'country_code|name|digits|rate|brand|currency")
            # Russian
            cd = CsvData(
                "delimiter=';'time_format='%d.%m.%Y 00:00'country_code|name|digits|price|rate|currency|weeks|time_start|time_end"
            )
            # delimiter=';'time_format='%d.%m.%Y 00:00'name|country_code|special_digits|rate
            # fw = open(os.path.join(os.path.dirname(__file__), 'fixtures', 'test_result.csv'), "wt")
            # writer = csv.writer(fw, delimiter=',', dialect='excel')
            # Abill price
            # writer.writerow(('countrycode', 'pattern', 'name',	'weight', 'connectcharge', 'includedseconds', 'minimumprice', 'price', 'brand'))
            # Abill route
            # writer.writerow(('countrycode', 'routename', 'pattern', 'costplan', 'connectcharge', 'includedseconds', 'billincrement', 'minimumcost', 'cost', 'trunk'))
            reader = csv.reader(f, delimiter=";", dialect="excel")
            for row in reader:
                try:
                    # l.debug(row)
                    country_list, country_code, n = cd.parse(row)
                    l.debug(country_code)
                    for country in country_list:
                        n["country_code"] = country_code
                        digits = n["digits"]
                        # price = Money(n['price'], n['currency'])
                        price = Money(n["price"], "USD")
                        # price = n['price']
                        objects_in_fixture = Lcr.objects.add_lcr(gateway, n, digits, price, s)
                        save_cnt += objects_in_fixture
                        # l.debug(price)
                        # l.debug(n["time_start"])
                        # , n["name"], price )
                        # route
                        # writer.writerow((country_code, n["name"], country, 0, Decimal('0.0000'), Decimal('0.0000'), 1,   Decimal('0.0000'), price, n['brand']))

                except Exception, e:
                    l.error("line: %i => %s" % (cd.line_num, e))
                    pass
            self.assertEquals(save_cnt, 3)
            res = Lcr.objects.get(digits="38039")
            self.assertEquals(res.country_code, 380)
            self.assertEquals(res.time_start, datetime.time(0, 10))
            self.assertEquals(res.time_end, datetime.time(23, 54))
            self.assertEquals(res.rate, Decimal("0.804"))
            self.assertEquals(res.price, Decimal("0.67"))
            cur = Currency.objects.get_default()
            self.assertEquals(cur.currency.iso3_code, "USD")
            exc = Currency.objects.get_currency("EUR")
            self.assertEquals(exc.exchange_rate, Decimal("1.39"))
예제 #8
0
    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
        from fsb.prepaid.models import Prepaid

        format_csv = options.get('format_csv', '')

        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

        #cd = CsvData(CsvBase.objects.get(pk=format_csv))
        try:
            csb = CsvBase.objects.get(pk=format_csv)
            cd = CsvData(csb.val)
            log.debug(fixture_labels)
            f = open(fixture_labels, "rt")
            reader = csv.reader(f, delimiter=';', dialect='excel')
            for row in reader:
                try:
                    n = cd.parse(row)
                    objects_in_fixture = Prepaid.objects.add_prepaid(n)
                    log.debug("line: {0} => {1}".format(cd.line_num, row))
                except Exception, e:
                    log.error("line: {0} => {1}".format(cd.line_num, e))
                    pass
            label_found = True
예제 #9
0
    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
        from fsb.prepaid.models import Prepaid

        format_csv = options.get('format_csv','')

        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

        #cd = CsvData(CsvBase.objects.get(pk=format_csv))
        try:
            csb = CsvBase.objects.get(pk=format_csv)
            cd = CsvData(csb.val)
            log.debug(fixture_labels)
            f = open(fixture_labels, "rt")
            reader = csv.reader(f, delimiter=';', dialect='excel')
            for row in reader:
                try:
                    n = cd.parse(row)
                    objects_in_fixture = Prepaid.objects.add_prepaid(n)
                    log.debug("line: {0} => {1}".format(cd.line_num, row))
                except Exception, e:
                    log.error("line: {0} => {1}".format(cd.line_num, e))
                    pass
            label_found = True