Skip to content
Snippets Groups Projects

Filling XLSX: Everything except multiple choice.

Merged Daniel Hornung requested to merge f-json-table into dev
All threads resolved!
Compare and Show latest version
10 files
+ 161
7
Compare changes
  • Side-by-side
  • Inline
Files
10
@@ -262,6 +262,8 @@ out: union[dict, None]
context = TemplateFiller.Context()
context.fill_from_data(data)
explode_later: List[dict] = []
insertables: Dict[str, Any] = {}
for name, content in data.items():
path = current_path + [name]
@@ -275,16 +277,23 @@ out: union[dict, None]
if isinstance(content[0], dict):
# An array of objects: must go into exploded sheet
for entry in content:
self._handle_data(data=entry, current_path=path, context=next_context)
explode_later.append({"data": entry, "current_path": path,
"context": next_context})
continue
elif isinstance(content, dict):
if not current_path: # Special handling for top level
self._handle_data(content, current_path=path, context=next_context)
explode_later.append({"data": content, "current_path": path,
"context": next_context})
continue
insert = self._handle_data(content, current_path=path, context=next_context.copy(),
only_collect_insertables=True)
assert isinstance(insert, dict)
assert not any(key in insertables for key in insert)
# print(insert)
# from IPython import embed
# embed()
insertables.update(insert)
continue
else: # scalars
@@ -301,8 +310,9 @@ out: union[dict, None]
insertables[path_str] = value
if only_collect_insertables:
return insertables
if not current_path:
return None
# if not current_path:
# print("returning early")
# return None
# actual data insertion
insert_row = None
@@ -318,11 +328,21 @@ out: union[dict, None]
sheet.cell(row=insert_row+1, column=col_index+1, value=value)
# continue in exploded sheets
for exploded_content in explode_later:
print(f"Exploding: {exploded_content}")
self._handle_data(**exploded_content)
# Insert foreign keys
if insert_row is not None and sheet is not None and _is_exploded_sheet(sheet):
foreigns = _get_foreign_key_columns(sheet)
for index, path in ((f.index, f.path) for f in foreigns.values()):
value = context[path]
try:
value = context[path]
except KeyError as err:
print(f"\nCurrent context: {context._props}")
from IPython import embed
embed()
sheet.cell(row=insert_row+1, column=index+1, value=value)
return None
Loading