Skip to content

Commit 9d61e0f

Browse files
authored
[Fixes #13618] Reduced volume of data in upsert CSV error file (#13624)
* [Fixes #13618] Handle the Volume of error file on upsert
1 parent 1a8503a commit 9d61e0f

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

geonode/upload/handlers/common/tests_vector.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,14 @@ def test_upsert_data_should_fail_if_upsertkey_is_not_provided(self, upsert_funct
525525
"Was not possible to find the upsert key, upsert is aborted",
526526
)
527527

528+
def test_get_error_file_csv_headers(self):
529+
handler = BaseVectorFileHandler()
530+
mock_validator = MagicMock()
531+
mock_validator.restrictions = {"type": {"restrictions": {"enumeration": ["type1"]}}}
532+
with patch("geonode.upload.handlers.common.vector.feature_validators_registry.HANDLERS", [mock_validator]):
533+
headers = handler._BaseVectorFileHandler__get_csv_headers()
534+
self.assertEqual(headers, ["fid", "type", "error"])
535+
528536

529537
class TestUpsertBaseVectorHandler(TransactionImporterBaseTestSupport):
530538
"""

geonode/upload/handlers/common/vector.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,12 +1179,21 @@ def _validate_single_feature(self, exec_obj, OriginalResource, upsert_key, layer
11791179
# cleaning up the feature from memory
11801180
self._create_error_log(exec_obj, layers, errors)
11811181

1182+
def __get_csv_headers(self):
1183+
constrained_attributes = []
1184+
for handler in feature_validators_registry.HANDLERS:
1185+
if hasattr(handler, "restrictions"):
1186+
constrained_attributes.extend(handler.restrictions.keys())
1187+
return ["fid"] + constrained_attributes + ["error"]
1188+
11821189
def _create_error_log(self, exec_obj, layers, errors):
11831190
logger.error(
11841191
"Error found during the upsert process, no update/create will be perfomed. The error log is going to be created..."
11851192
)
11861193
errors_to_print = errors[: settings.UPSERT_LIMIT_ERROR_LOG]
1187-
fieldnames = errors_to_print[0].keys()
1194+
1195+
fieldnames = self.__get_csv_headers()
1196+
11881197
log_name = f'error_{layers[0].GetName()}_{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}.csv'
11891198

11901199
with tempfile.TemporaryDirectory() as temp_dir_str:
@@ -1194,7 +1203,7 @@ def _create_error_log(self, exec_obj, layers, errors):
11941203
csv_file_path = subfolder_path / log_name
11951204
with open(csv_file_path, "w", newline="", encoding="utf-8") as csvfile:
11961205
# Create a DictWriter object. It maps dictionaries to output rows.
1197-
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
1206+
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction="ignore")
11981207
writer.writeheader()
11991208
writer.writerows(errors_to_print)
12001209

0 commit comments

Comments
 (0)