def _log_status_if_pct_update(self): """ Calculates new counts and percentages and logs if diff pct """ new_pct = int(100 * float(self.current_row) / float(self.total_rows)) if new_pct != self.current_pct: self.current_pct = new_pct update_percent(new_pct) self.logger.info( f"Processed row #{self.current_row} ||| {new_pct}%")
def __call__(self): try: update_percent(0) output = CsvExporter.create_csv_response(self.file_name) writer = csv.DictWriter(output, fieldnames=CURRENT_FIELDS) writer.writeheader() self.total_rows = Item.objects.count() for item in Item.objects.all(): writer.writerow(self.format_row(item)) self.current_row += 1 self._log_status_if_pct_update() self.logger.info("Import completed") return output # Celery will set SUCCESS on return except Exception: self.logger.error(f"Error on row #{self.current_row}") raise
def __call__(self): try: rows = StrippedDictReader(self.csvpath, delimiter=',') print(rows.fieldnames) self.total_rows = sum(1 for line in StrippedDictReader(self.csvpath)) update_percent(0) self.parse_rows(rows) self.logger.info("Parsed all rows") if self.bulk_model is not None: self.create_bulk_model() self.logger.info(f"Bulk created {len(self.model_bulk)} rows") set_success() self.logger.info("Import completed") except Exception as e: self.logger.error(f"Error on row #{self.current_row}") raise e
def parser(self, csvfile): update_percent(0) item_bulk = [] row_count, previous_percent = 0, 0 total_row_count = sum(1 for line in csv.DictReader(csvfile)) read_file = csv.DictReader(csvfile, delimiter=',') for row in read_file: item_bulk.append(parse_row(row)) row_count += 1 process_percent = int(100 * float(row_count) / float(total_row_count)) if process_percent != previous_percent: update_percent(process_percent) previous_percent = process_percent print(f"Parsed row #{row_count} ||| {process_percent}%") print("Adding all items") Item.objects.bulk_create(item_bulk) print("Parsing completed") set_success()
def __call__(self): update_percent(0) # Note: SMTP may fail due to Gmail security policy. Check credentials # and settings if self.shouldSendCopy: self.m.start_server() for row in serializers.deserialize('json', self.qs): donation = row.object self.donation_pks.append(donation.pk) context = self.generate_context(donation) pdf = render_to_pdf('pdf/receipt.html', donation, context) file_name = f'{donation.pk} {donation.donor.donor_name}.pdf' self.pdfs.append(pdf) self.pdf_names.append(file_name) self.mails.append( self.generate_mail(donation.donor, pdf, file_name)) self.current_row += 1 self.log_status_if_pct_update() if self.shouldSendCopy: self.logger.info('Receipt generation completed; Sending Mails') self.m.send_mails(self.mails) self.logger.info(f'Sent {len(self.mails)} mails') self.logger.info(f"Receipted {self.current_row} donation(s)") Donation.objects.filter(pk__in=self.donation_pks).update( tax_receipt_created_at=tz.localtime()) try: if len(self.pdfs) == 1: return self.pdfs[0] else: return generate_zip(self.pdfs, self.pdf_names) finally: if self.shouldSendCopy: self.m.close_server() update_percent(100)