示例#1
0
    def _get_aggr_donazioni(self, ):
        values = []
        # get aggr donazioni uses programmazione filters because it's used only for home page: no filters or
        # territorio page: only territorio filters

        for tipologia in Donazione.TIPO_CEDENTE:
            d = {'name': tipologia[1], 'tipologia': tipologia}
            d.update(Donazione.get_aggregates(tipologia=tipologia, **self.programmazione_filters))
            values.append(d)

        return values
示例#2
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)

        self.input_file = options['file']
        self.logger.info('Input file:{}'.format(self.input_file))
        udr = None
        territori_not_found = {}
        wrong_dates = {}
        wrong_date_counter = 0
        missing_date_counter = 0

        # read file
        try:
            udr = UnicodeDictReader(f=open(self.input_file),
                                    dialect=csv.excel_tab,
                                    encoding=self.encoding)
        except IOError:
            self.logger.error("It was impossible to open file {}".format(
                self.input_file))
            exit(1)

        self.logger.info("Deleting all previous records...")
        Donazione.objects.all().delete()
        DonazioneInterventoProgramma.objects.all().delete()
        self.logger.info("Done")

        donation_counter = 0
        row_counter = -1
        set_autocommit(False)
        for row in udr:
            ip = None
            row_counter += 1
            rowdata = RowData(row)
            self.logger.debug(u"Import donazione (Line {}) {}".format(
                row_counter, rowdata.denominazione))

            if rowdata.importo == Decimal(0):
                self.handle_error(rowdata, row_counter,
                                  "Donazione has importo=0, skip")
                continue

            if rowdata.tipologia_donazione is None or (
                    rowdata.tipologia_donazione != '1'
                    and rowdata.tipologia_donazione != '2'):
                self.handle_error(
                    rowdata, row_counter,
                    "Donazione has incorrect tipologia_donazione, skip")
                continue

            if rowdata.data is None:
                missing_date_counter += 1
                self.handle_error(rowdata, row_counter,
                                  "Donazione has no date, skip")
                continue

            territorio = self.get_territorio(rowdata.territorio)
            if territorio is None:
                self.handle_error(rowdata, row_counter,
                                  "Donazione has wrong territorio, skip")

                if rowdata.territorio not in territori_not_found:
                    territori_not_found[rowdata.territorio] = 1
                else:
                    territori_not_found[rowdata.territorio] += 1

                continue

            if rowdata.n_ordine:
                n_ordine_zeropad = rowdata.n_ordine.zfill(6)
                try:
                    ip = InterventoProgramma.objects.get(
                        Q(n_ordine=rowdata.n_ordine)
                        | Q(n_ordine=n_ordine_zeropad))
                except ObjectDoesNotExist:
                    self.handle_error(
                        rowdata, row_counter,
                        "Cannot find interv.programma for n_ordine:{}".format(
                            rowdata.n_ordine))
                    continue
                else:
                    self.logger.debug(
                        "Found intervento:{} associated with donazione".format(
                            ip.slug))

            don_dict = {
                'territorio': territorio,
                'informazioni': rowdata.info,
                'denominazione': rowdata.denominazione,
                'tipologia_cedente': rowdata.tipologia_cedente,
                'tipologia_donazione': rowdata.tipologia_donazione,
                'data': rowdata.data,
                'importo': rowdata.importo,
            }

            donazione = Donazione(**don_dict)
            donazione.save()
            if ip is not None:
                commit()
                # if the donazione is linked to an InterventoProgramma, creates
                # the DonazioneInterventoProgramma object
                dip = DonazioneInterventoProgramma()
                dip.intervento_programma = ip
                dip.donazione = donazione
                dip.save()

            donation_counter += 1

        commit()

        if wrong_date_counter > 0:
            self.logger.error("********** Wrong dates ***********")
            self.logger.error(
                "Found {} wrong dates".format(wrong_date_counter))
            for key, value in wrong_dates.iteritems():
                self.logger.error("{}:{}".format(key, value))

        if missing_date_counter > 0:
            self.logger.error(
                "Found {} missing dates".format(missing_date_counter))

        if self.invalid_values_counter > 0:
            self.logger.error("********** Invalid data ***********")
            self.logger.error(
                "Could not import {} donazioni for errors in the data".format(
                    self.invalid_values_counter))

        if len(territori_not_found.keys()):
            self.logger.error("********** Territori not found ***********")
            for t, counter in territori_not_found.iteritems():
                self.logger.error("{}:{}".format(t, counter))

        self.logger.info("Imported {} donazioni".format(donation_counter))

        UltimoAggiornamento.objects.update_or_create(
            tipologia=UltimoAggiornamento.TIPOLOGIA.DONAZIONI,
            defaults={
                'data': datetime.today(),
            })
        self.logger.info("Set Ultimo aggiornamento to today")
示例#3
0
 def _get_totale_donazioni(self):
     return Donazione.get_aggregates(tipologia=None, **self.programmazione_filters)
    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)

        self.input_file = options['file']
        self.logger.info('Input file:{}'.format(self.input_file))
        udr = None
        territori_not_found = {}
        wrong_dates = {}
        wrong_date_counter = 0
        missing_date_counter = 0

        # read file
        try:
            udr = UnicodeDictReader(f=open(self.input_file),dialect=csv.excel, delimiter=';', encoding=self.encoding)
        except IOError:
            self.logger.error("It was impossible to open file {}".format(self.input_file))
            exit(1)

        set_autocommit(False)

        self.logger.info("Deleting all previous records...")
        Donazione.objects.all().delete()
        DonazioneInterventoProgramma.objects.all().delete()
        self.logger.info("Done")

        donation_counter = 0
        row_counter = -1
        for row in udr:
            ip = None
            row_counter += 1
            rowdata = RowData(row)
            self.logger.debug(u"Import donazione (Line {}) {}".format(row_counter, rowdata.denominazione))


            if rowdata.importo == Decimal(0):
                self.handle_error(rowdata, row_counter, "Donazione has importo=0, skip")
                continue

            if rowdata.tipologia_donazione is None or (
                        rowdata.tipologia_donazione != '1' and rowdata.tipologia_donazione != '2'):
                self.handle_error(rowdata, row_counter, "Donazione has incorrect tipologia_donazione, skip")
                continue

            if rowdata.data is None:
                missing_date_counter += 1
                self.handle_error(rowdata, row_counter, "Donazione has no date, skip")
                continue

            territorio = self.get_territorio(rowdata.territorio)
            if territorio is None:
                self.handle_error(rowdata, row_counter, "Donazione has wrong territorio, skip")

                if rowdata.territorio not in territori_not_found:
                    territori_not_found[rowdata.territorio] = 1
                else:
                    territori_not_found[rowdata.territorio] += 1

                continue

            if rowdata.n_ordine:
                n_ordine_zeropad = rowdata.n_ordine.zfill(6)
                try:
                    ip = InterventoProgramma.objects.get(Q(n_ordine=rowdata.n_ordine)|Q(n_ordine=n_ordine_zeropad))
                except ObjectDoesNotExist:
                    self.handle_error(rowdata, row_counter, "Cannot find interv.programma for n_ordine:{}".format(rowdata.n_ordine))
                    # continue
                else:
                    self.logger.debug("Found intervento:{} associated with donazione".format(ip.slug))

            don_dict = {
                'territorio': territorio,
                'informazioni': rowdata.info,
                'denominazione': rowdata.denominazione,
                'tipologia_cedente': rowdata.tipologia_cedente,
                'tipologia_donazione': rowdata.tipologia_donazione,
                'data': rowdata.data,
                'importo': rowdata.importo,

            }

            donazione = Donazione(**don_dict)
            donazione.save()
            if ip is not None:
                # commit()
                # if the donazione is linked to an InterventoProgramma, creates
                # the DonazioneInterventoProgramma object
                dip = DonazioneInterventoProgramma()
                dip.intervento_programma = ip
                dip.donazione = donazione
                dip.save()

            donation_counter += 1

        commit()

        if wrong_date_counter > 0:
            self.logger.error("********** Wrong dates ***********")
            self.logger.error("Found {} wrong dates".format(wrong_date_counter))
            for key, value in wrong_dates.iteritems():
                self.logger.error("{}:{}".format(key, value))

        if missing_date_counter > 0:
            self.logger.error("Found {} missing dates".format(missing_date_counter))

        if self.invalid_values_counter > 0:
            self.logger.error("********** Invalid data ***********")
            self.logger.error("Could not import {} donazioni for errors in the data".format(self.invalid_values_counter))

        if len(territori_not_found.keys()):
            self.logger.error("********** Territori not found ***********")
            for t, counter in territori_not_found.iteritems():
                self.logger.error("{}:{}".format(t, counter))

        self.logger.info("Imported {} donazioni".format(donation_counter))

        UltimoAggiornamento.objects.update_or_create(
            tipologia=UltimoAggiornamento.TIPOLOGIA.DONAZIONI,
            defaults={
                'data': datetime.today(),
            }
        )
        self.logger.info("Set Ultimo aggiornamento to today")