Exemplo n.º 1
0
    def build(self, wb, commit=False):
        objects = []
        # find worksheet
        ws_name = self.model_mapping.sheet_name
        ws = util_xls.get_sheet(wb, ws_name)
        if ws is None and self.model_mapping.mandatory:
            raise MissingDataSheetException
        else:
            # worksheet found. Parse rows
            table_data = self.model_mapping.get_table_data(ws)
            if len(table_data.rows) == 0 and self.model_mapping.mandatory:
                message = "No data found for {model}".format(model=self.model._meta.verbose_name)
                raise NoDataForModelException(message)
            if len(table_data.rows) > 1 and self.model_mapping.unique:
                message = "Too many rows for {model}. It should only contains one row (or column)" \
                    .format(model=self.model._meta.verbose_name)
                raise TooManyDataForModelException(message)
            else:
                for row_data in table_data.by_rows():
                    obj = self._create_obj(row_data)
                    if commit:
                        obj.save()
                    objects.append(obj)

        return objects
Exemplo n.º 2
0
 def parse_workbook(wb):
     mapping = DATASHEET_META_MAPPING
     meta_ws = util_xls.get_sheet(wb, mapping.sheet_name)
     if meta_ws is None:
         raise NoMetaDataSheetException("Meta sheet not found.")
     key = util_model.get_field_verbose_name(Visit, 'name')
     reading_direction = 'right' if mapping.transpose else 'down'
     visit_name = util_xls.get_value_for_key(meta_ws, key, direction=reading_direction)
     key = util_model.get_field_verbose_name(Site, 'site_code')
     site_code = util_xls.get_value_for_key(meta_ws, key, direction=reading_direction)
     return MetaData(visit_name, site_code)
Exemplo n.º 3
0
    def validate(self, wb):
        errors = []
        # find worksheet
        ws_name = self.model_mapping.sheet_name
        ws = util_xls.get_sheet(wb, ws_name)
        if ws is None:
            error = Error.objects.get(label='missing_data_datasheet')
            error.params = {'sheet_name': ws_name}
            errors.append(error)
        else:
            # worksheet found. Validate data
            table_data = self.model_mapping.get_table_data(ws)
            if len(table_data.rows) == 0 and self.model_mapping.mandatory:
                error = ErrorManager.get(label="no_data_for_model")
                error.params = {'model': ws_name}
                errors.append(error)
            if len(table_data.rows) > 1 and self.model_mapping.unique:
                error = ErrorManager.get(label="too_many_data_for_model")
                error.params = {'model': ws_name}
                errors.append(error)
            else:
                row_index = 0

                for row_data in table_data.by_rows():
                    row_index += 1
                    errors += self.validate_row(row_data, row_index, ws)

                    if callable(self.model_mapping.extra_validation_func):
                        try:
                            self.model_mapping.extra_validation_func(row_data)
                        except Exception as e:
                            error = Error.objects.get(label="extra_validation_error")
                            params = {
                                'datasheet': ws.title,
                                'row': row_index + 1,  # add 1 to match the worksheet number
                                'msg': str(e)
                            }
                            error.params = params
                            errors.append(error)

        return errors