Skip to content

Commit 9bd9da5

Browse files
committed
Add "empty" and "current timestamp" as options to CSV mapping
1 parent 0b57457 commit 9bd9da5

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

datasources/upload/import_csv.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ def validate_query(query, request, user):
268268
# mapping for each column
269269
column_mapping = {}
270270
if tool_format.get("allow_user_mapping", False):
271+
magic_mappings = {
272+
"id": {"__4cat_auto_sequence": "[generate sequential IDs]"},
273+
"thread_id": {"__4cat_auto_sequence": "[generate sequential IDs]"},
274+
"empty": {"__4cat_empty_value": "[empty]"},
275+
"timestamp": {"__4cat_now": "[current date and time]"}
276+
}
271277
if incomplete_mapping:
272278
raise QueryNeedsFurtherInputException({
273279
"mapping-info": {
@@ -279,8 +285,7 @@ def validate_query(query, request, user):
279285
"type": UserInput.OPTION_CHOICE,
280286
"options": {
281287
"": "",
282-
**({"__4cat_auto_sequence": "[generate sequential IDs]"} if mappable_column in (
283-
"id", "thread_id") else {}),
288+
**magic_mappings.get(mappable_column, magic_mappings["empty"]),
284289
**{column: column for column in fields}
285290
},
286291
"default": mappable_column if mappable_column in fields else "",
@@ -294,7 +299,7 @@ def validate_query(query, request, user):
294299
for field in tool_format["columns"]:
295300
mapping_field = "option-mapping-%s" % field
296301
provided_field = request.form.get(mapping_field)
297-
if (provided_field not in fields and provided_field != "__4cat_auto_sequence") or not provided_field:
302+
if (provided_field not in fields and not provided_field.startswith("__4cat")) or not provided_field:
298303
missing_mapping.append(field)
299304
else:
300305
column_mapping["mapping-" + field] = request.form.get(mapping_field)

datasources/upload/import_formats.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ def map_csv_items(reader, columns, dataset, parameters):
326326
"""
327327
# write to the result file
328328
indexes = {}
329+
now_timestmap = str(int(datetime.datetime.now().timestamp()))
329330
for row in reader:
330331
mapped_row = {}
331332
for field in columns:
@@ -337,6 +338,10 @@ def map_csv_items(reader, columns, dataset, parameters):
337338
indexes[field] = 1
338339
mapped_row[field] = indexes[field]
339340
indexes[field] += 1
341+
elif mapping == "__4cat_empty_value":
342+
mapped_row[field] = ""
343+
elif mapping == "__4cat_now":
344+
mapped_row[field] = now_timestmap
340345
else:
341346
# actual mapping
342347
mapped_row[field] = row[mapping]

0 commit comments

Comments
 (0)