Skip to content

Commit

Permalink
Add "empty" and "current timestamp" as options to CSV mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
stijn-uva committed Jul 12, 2024
1 parent 0b57457 commit 9bd9da5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 8 additions & 3 deletions datasources/upload/import_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ def validate_query(query, request, user):
# mapping for each column
column_mapping = {}
if tool_format.get("allow_user_mapping", False):
magic_mappings = {
"id": {"__4cat_auto_sequence": "[generate sequential IDs]"},
"thread_id": {"__4cat_auto_sequence": "[generate sequential IDs]"},
"empty": {"__4cat_empty_value": "[empty]"},
"timestamp": {"__4cat_now": "[current date and time]"}
}
if incomplete_mapping:
raise QueryNeedsFurtherInputException({
"mapping-info": {
Expand All @@ -279,8 +285,7 @@ def validate_query(query, request, user):
"type": UserInput.OPTION_CHOICE,
"options": {
"": "",
**({"__4cat_auto_sequence": "[generate sequential IDs]"} if mappable_column in (
"id", "thread_id") else {}),
**magic_mappings.get(mappable_column, magic_mappings["empty"]),
**{column: column for column in fields}
},
"default": mappable_column if mappable_column in fields else "",
Expand All @@ -294,7 +299,7 @@ def validate_query(query, request, user):
for field in tool_format["columns"]:
mapping_field = "option-mapping-%s" % field
provided_field = request.form.get(mapping_field)
if (provided_field not in fields and provided_field != "__4cat_auto_sequence") or not provided_field:
if (provided_field not in fields and not provided_field.startswith("__4cat")) or not provided_field:
missing_mapping.append(field)
else:
column_mapping["mapping-" + field] = request.form.get(mapping_field)
Expand Down
5 changes: 5 additions & 0 deletions datasources/upload/import_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ def map_csv_items(reader, columns, dataset, parameters):
"""
# write to the result file
indexes = {}
now_timestmap = str(int(datetime.datetime.now().timestamp()))
for row in reader:
mapped_row = {}
for field in columns:
Expand All @@ -337,6 +338,10 @@ def map_csv_items(reader, columns, dataset, parameters):
indexes[field] = 1
mapped_row[field] = indexes[field]
indexes[field] += 1
elif mapping == "__4cat_empty_value":
mapped_row[field] = ""
elif mapping == "__4cat_now":
mapped_row[field] = now_timestmap
else:
# actual mapping
mapped_row[field] = row[mapping]
Expand Down

0 comments on commit 9bd9da5

Please sign in to comment.