Exemple #1
0
 def loadExcelfile(self, model, file):
     errorcount = 0
     warningcount = 0
     try:
         with transaction.atomic(using=self.database):
             wb = load_workbook(filename=file, read_only=True, data_only=True)
             for ws_name in wb.sheetnames:
                 ws = wb[ws_name]
                 for error in parseExcelWorksheet(
                     model, ws, user=self.user, database=self.database
                 ):
                     if error[0] == logging.ERROR:
                         logger.error(
                             "%s Error: %s%s%s%s"
                             % (
                                 datetime.now().replace(microsecond=0),
                                 "Row %s: " % error[1] if error[1] else "",
                                 "field %s: " % error[2] if error[2] else "",
                                 "%s: " % error[3] if error[3] else "",
                                 error[4],
                             )
                         )
                         errorcount += 1
                     elif error[0] == logging.WARNING:
                         logger.warning(
                             "%s Warning: %s%s%s%s"
                             % (
                                 datetime.now().replace(microsecond=0),
                                 "Row %s: " % error[1] if error[1] else "",
                                 "field %s: " % error[2] if error[2] else "",
                                 "%s: " % error[3] if error[3] else "",
                                 error[4],
                             )
                         )
                         warningcount += 1
                     else:
                         logger.info(
                             "%s %s%s%s%s"
                             % (
                                 datetime.now().replace(microsecond=0),
                                 "Row %s: " % error[1] if error[1] else "",
                                 "field %s: " % error[2] if error[2] else "",
                                 "%s: " % error[3] if error[3] else "",
                                 error[4],
                             )
                         )
         # Records are committed. Launch notification generator now.
         NotificationFactory.launchWorker(database=self.database, url=None)
     except Exception:
         errorcount += 1
         logger.error(
             "%s Error: Invalid data format - skipping the file \n"
             % datetime.now().replace(microsecond=0)
         )
     return [errorcount, warningcount]
Exemple #2
0
    def loadCSVfile(self, model, file):
        errorcount = 0
        warningcount = 0
        datafile = EncodedCSVReader(file, delimiter=self.delimiter)
        try:
            with transaction.atomic(using=self.database):
                for error in parseCSVdata(
                    model, datafile, user=self.user, database=self.database
                ):
                    if error[0] == logging.ERROR:
                        logger.error(
                            "%s Error: %s%s%s%s"
                            % (
                                datetime.now().replace(microsecond=0),
                                "Row %s: " % error[1] if error[1] else "",
                                "field %s: " % error[2] if error[2] else "",
                                "%s: " % error[3] if error[3] else "",
                                error[4],
                            )
                        )
                        errorcount += 1
                    elif error[0] == logging.WARNING:
                        logger.warning(
                            "%s Warning: %s%s%s%s"
                            % (
                                datetime.now().replace(microsecond=0),
                                "Row %s: " % error[1] if error[1] else "",
                                "field %s: " % error[2] if error[2] else "",
                                "%s: " % error[3] if error[3] else "",
                                error[4],
                            )
                        )
                        warningcount += 1
                    else:
                        logger.info(
                            "%s %s%s%s%s"
                            % (
                                datetime.now().replace(microsecond=0),
                                "Row %s: " % error[1] if error[1] else "",
                                "field %s: " % error[2] if error[2] else "",
                                "%s: " % error[3] if error[3] else "",
                                error[4],
                            )
                        )

            # Records are committed. Launch notification generator now.
            NotificationFactory.launchWorker(database=self.database, url=None)

        except Exception:
            errorcount += 1
            logger.error(
                "%s Error: Invalid data format - skipping the file \n"
                % datetime.now().replace(microsecond=0)
            )
        return [errorcount, warningcount]