|
2 | 2 |
|
3 | 3 | from import_export import resources
|
4 | 4 | from import_export.fields import Field, widgets
|
| 5 | +from import_export.results import RowResult |
5 | 6 |
|
6 | 7 | from datasources.models import SourceSubdivision
|
7 | 8 | from datasources.resources import process_links
|
@@ -152,7 +153,35 @@ def process_base(row) -> None:
|
152 | 153 | row["base"] = base_signal.id
|
153 | 154 |
|
154 | 155 |
|
155 |
| -class SignalBaseResource(resources.ModelResource): |
| 156 | +class ModelResource(resources.ModelResource): |
| 157 | + def get_field_names(self): |
| 158 | + names = [] |
| 159 | + for field in self.get_fields(): |
| 160 | + names.append(self.get_field_name(field)) |
| 161 | + return names |
| 162 | + |
| 163 | + def import_row(self, row, instance_loader, **kwargs): |
| 164 | + # overriding import_row to ignore errors and skip rows that fail to import |
| 165 | + # without failing the entire import |
| 166 | + import_result = super(ModelResource, self).import_row( |
| 167 | + row, instance_loader, **kwargs |
| 168 | + ) |
| 169 | + |
| 170 | + if import_result.import_type in [RowResult.IMPORT_TYPE_ERROR, RowResult.IMPORT_TYPE_INVALID]: |
| 171 | + import_result.diff = [row.get(name, "") for name in self.get_field_names()] |
| 172 | + |
| 173 | + # Add a column with the error message |
| 174 | + import_result.diff.append( |
| 175 | + "Errors: {}".format([err.error for err in import_result.errors]) |
| 176 | + ) |
| 177 | + # clear errors and mark the record to skip |
| 178 | + import_result.errors = [] |
| 179 | + import_result.import_type = RowResult.IMPORT_TYPE_SKIP |
| 180 | + |
| 181 | + return import_result |
| 182 | + |
| 183 | + |
| 184 | +class SignalBaseResource(ModelResource): |
156 | 185 | """
|
157 | 186 | Resource class for importing Signals base.
|
158 | 187 | """
|
@@ -180,7 +209,7 @@ def before_import_row(self, row, **kwargs) -> None:
|
180 | 209 | process_base(row)
|
181 | 210 |
|
182 | 211 |
|
183 |
| -class SignalResource(resources.ModelResource): |
| 212 | +class SignalResource(ModelResource): |
184 | 213 | """
|
185 | 214 | Resource class for importing and exporting Signal models
|
186 | 215 | """
|
|
0 commit comments