def handle(self, *args, **options):
        verbosity = options['verbosity']
        if verbosity == '0':
            self.logger.setLevel(logging.ERROR)
        elif verbosity == '1':
            self.logger.setLevel(logging.WARNING)
        elif verbosity == '2':
            self.logger.setLevel(logging.INFO)
        elif verbosity == '3':
            self.logger.setLevel(logging.DEBUG)

        city_code = options['city']
        if not city_code:
            raise Exception("Missing city parameter")

        mapper = FLMapper()
        city = mapper.get_city(city_code)

        year = options['year']

        preventivo_node = Voce.objects.get(slug='preventivo-spese-spese-per-investimenti-funzioni')
        t = Territorio.objects.get(cod_finloc=city)
        y = year
        valori_bilancio = ValoreBilancio.objects.filter(territorio=t, anno=y).\
            filter(voce__in=preventivo_node.get_descendants(include_self=True))
        valori_bilancio_dict = dict(
            (v['pk'], {'valore': v['valore'], 'valore_procapite': v['valore_procapite']})
            for v in valori_bilancio.values('pk', 'valore', 'valore_procapite')
        )
        bilanci_tree = make_tree(preventivo_node, valori_bilancio_dict)

        bilanci_tree.emit()
Пример #2
0
# To avoid repetitions in the testing code, as:
#
# class Roma2006TestCase(SimplifyBaseTestCaseMixin, TestCase):
#     code = "2006_ROMA--3120700900"
#
# class Roma2008TestCase(SimplifyBaseTestCaseMixin, TestCase):
#     code = "2008_ROMA--3120700900"
#
# ...
#
# and to allow for *parametric testing*, a bit of meta-programming is used.
#
# This loop generates TestCase subclasses, so that the python manage.py test
# can discover and launch the test suite for all of them.
#
# Invocation:
#     python manage.py test bilanci --settings=bilanci.settings.testnodb [-v2]
mapper = FLMapper()
for year in (2004, 2008, 2010, 2012, 2013, 2014):
    for city_name in ('Roma', 'Milano','Torino', 'Napoli'):
        name = "{}{}TestCase".format(city_name, year)
        city = mapper.get_city(city_name)
        code = "{}_{}".format(year, city)

        Class = type(name, (BilanciSimpleBaseTestCaseMixin, TestCase), dict(city=city, code=code))
        globals()[name] = Class

# The Class variable contains a *TestCase type, at this point
# so we clear it, in order to avoid repeating an already
# performed TestCase
Class = None
Пример #3
0
#
# class Roma2006TestCase(SimplifyBaseTestCaseMixin, TestCase):
#     code = "2006_ROMA--3120700900"
#
# class Roma2008TestCase(SimplifyBaseTestCaseMixin, TestCase):
#     code = "2008_ROMA--3120700900"
#
# ...
#
# and to allow for *parametric testing*, a bit of meta-programming is used.
#
# This loop generates TestCase subclasses, so that the python manage.py test
# can discover and launch the test suite for all of them.
#
# Invocation:
#     python manage.py test bilanci --settings=bilanci.settings.testnodb [-v2]
mapper = FLMapper()
for year in (2004, 2008, 2010, 2012, 2013, 2014):
    for city_name in ('Roma', 'Milano', 'Torino', 'Napoli'):
        name = "{}{}TestCase".format(city_name, year)
        city = mapper.get_city(city_name)
        code = "{}_{}".format(year, city)

        Class = type(name, (BilanciSimpleBaseTestCaseMixin, TestCase),
                     dict(city=city, code=code))
        globals()[name] = Class

# The Class variable contains a *TestCase type, at this point
# so we clear it, in order to avoid repeating an already
# performed TestCase
Class = None
Пример #4
0
    def handle(self, *args, **options):
        verbosity = options['verbosity']
        if verbosity == '0':
            self.logger.setLevel(logging.ERROR)
        elif verbosity == '1':
            self.logger.setLevel(logging.WARNING)
        elif verbosity == '2':
            self.logger.setLevel(logging.INFO)
        elif verbosity == '3':
            self.logger.setLevel(logging.DEBUG)

        offset = int(options['offset'])
        limit = int(options['limit'])

        self.dryrun = options['dryrun']
        self.apidomain = options['apidomain']
        if options['auth']:
            (user, pwd) = options['auth'].split(",")
            self.baseurl = "http://{0}:{1}@{2}".format(user, pwd, self.apidomain)
        else:
            self.baseurl = "http://{0}".format(self.apidomain)

        self.logger.info(u"=== Starting ===")

        # all cities in the DB
        comuni = Territorio.objects.filter(territorio=Territorio.TERRITORIO.C)

        mapper = FLMapper()

        c = 0
        for comune in comuni:
            c += 1
            if c < offset:
                continue
            if limit and c >= limit + offset:
                break
            city_url = "{0}/maps/places/{1}".format(self.baseurl, comune.slug)
            self.logger.debug("CITY_URL: {0}".format(city_url))
            place = requests.get(city_url).json()

            # get identifiers needed and build the finloc code
            identifiers = place['placeidentifiers']
            macroregion_id = None
            region_id = None
            province_id = None
            city_id = None
            for i in identifiers:
                identifier = i['identifier']
                value = i['value']
                if 'istat-macroregion-id' in identifier:
                    macroregion_id = int(value)
                if 'minint-region-id' in identifier:
                    region_id = int(value)
                if 'minint-province-id' in identifier:
                    province_id = int(value)
                if 'minint-city-id' in identifier:
                    city_id = int(value)


            # build numeric code for finanzalocale
            num_cod_finloc = "{0:d}{1:02d}{2:03d}{3:04d}".format(
                macroregion_id, region_id, province_id, city_id
            )

            # store complete finloc code inside the database
            try:
                comune.cod_finloc = mapper.get_city(num_cod_finloc)
            except IndexError:
                name = "-".join(comune.slug.split('-')[:-2])
                try:
                    comune.cod_finloc = mapper.get_city(name)

                except IndexError:
                    try:
                        # next try: (Sant'Antonio => sant-antonio)
                        # to fetch names with apostrophe
                        # that are not fetched with the preceding tries
                        denominazione = comune.denominazione.replace("'", " ")
                        name = slugify(denominazione)
                        comune.cod_finloc = mapper.get_city(name)
                    except IndexError:
                        self.logger.warning("Could not find city: {0}".format(comune.slug))
                        continue

                except CityNameNotUnique:
                    # add the province code to get_city because this city
                    # name is not unique
                    name_prov = "{0}({1})".format(name,comune.prov)
                    comune.cod_finloc = mapper.get_city(name_prov)

            self.logger.info(u"{0}, slug: {1.slug}, cod_finloc: {1.cod_finloc}".format(
                c, comune
            ))


            if not self.dryrun:
                try:
                    comune.save()
                except IntegrityError:
                    # given that finloc field is unique if the comune has a duplicated finloc code
                    # there is an error
                    self.logger.error("Finloc code:{0} for City: {1} is already present in DB, quitting...".\
                        format(comune.cod_finloc,comune.slug)
                        )
                    return


        self.logger.info(u" === End ===")