Skip to content

Commit

Permalink
make write_csv pickup all possible keys from all dicts as a header, a…
Browse files Browse the repository at this point in the history
…dd support for new export format to main
  • Loading branch information
northdpole committed Aug 11, 2024
1 parent 71c96a4 commit 8814dea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
5 changes: 3 additions & 2 deletions application/utils/spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,9 @@ def prepare_spreadsheet(

def write_csv(docs: List[Dict[str, Any]]) -> io.StringIO:
data = io.StringIO()
fieldnames: List[str] = list(docs[0].keys())
writer: csv.DictWriter = csv.DictWriter(data, fieldnames=fieldnames) # type: ignore
fieldnames = {}
[fieldnames.update(d) for d in docs]
writer: csv.DictWriter = csv.DictWriter(data, fieldnames=fieldnames.keys()) # type: ignore
writer.writeheader()
writer.writerows(docs)
return data
Expand Down
23 changes: 17 additions & 6 deletions application/web/web_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def extend_cre_with_tag_links(
others = list(frozenset(others))
for o in others:
o.links = []
cre.add_link(defs.Link(ltype=defs.LinkTypes.Related, document=o))
if not cre.link_exists(o) and o.id != cre.id:
cre.add_link(defs.Link(ltype=defs.LinkTypes.Related, document=o))
return cre


Expand Down Expand Up @@ -137,7 +138,9 @@ def find_cre(creid: str = None, crename: str = None) -> Any: # refer
return f"<pre>{mdutils.cre_to_md([cre])}</pre>"

elif opt_format == SupportedFormats.CSV.value:
docs = sheet_utils.prepare_spreadsheet(docs=[cre])
docs = sheet_utils.ExportSheet().prepare_spreadsheet(
docs=[cre], storage=database
)
return write_csv(docs=docs).getvalue().encode("utf-8")

elif opt_format == SupportedFormats.OSCAL.value:
Expand Down Expand Up @@ -209,7 +212,9 @@ def find_node_by_name(name: str, ntype: str = defs.Credoctypes.Standard.value) -
return f"<pre>{mdutils.cre_to_md(nodes)}</pre>"

elif opt_format == SupportedFormats.CSV.value:
docs = sheet_utils.prepare_spreadsheet(docs=nodes)
docs = sheet_utils.ExportSheet().prepare_spreadsheet(
docs=nodes, storage=database
)
return write_csv(docs=docs).getvalue().encode("utf-8")

elif opt_format == SupportedFormats.OSCAL.value:
Expand Down Expand Up @@ -242,7 +247,9 @@ def find_document_by_tag() -> Any:
if opt_format == SupportedFormats.Markdown.value:
return f"<pre>{mdutils.cre_to_md(documents)}</pre>"
elif opt_format == SupportedFormats.CSV.value:
docs = sheet_utils.prepare_spreadsheet(docs=documents)
docs = sheet_utils.ExportSheet().prepare_spreadsheet(
docs=documents, storage=database
)
return write_csv(docs=docs).getvalue().encode("utf-8")
elif opt_format == SupportedFormats.OSCAL.value:
return jsonify(json.loads(oscal_utils.list_to_oscal(documents)))
Expand Down Expand Up @@ -372,7 +379,9 @@ def text_search() -> Any:
if opt_format == SupportedFormats.Markdown.value:
return f"<pre>{mdutils.cre_to_md(documents)}</pre>"
elif opt_format == SupportedFormats.CSV.value:
docs = sheet_utils.prepare_spreadsheet(docs=documents)
docs = sheet_utils.ExportSheet().prepare_spreadsheet(
docs=documents, storage=database
)
return write_csv(docs=docs).getvalue().encode("utf-8")
elif opt_format == SupportedFormats.OSCAL.value:
return jsonify(json.loads(oscal_utils.list_to_oscal(documents)))
Expand Down Expand Up @@ -402,7 +411,9 @@ def find_root_cres() -> Any:
if opt_format == SupportedFormats.Markdown.value:
return f"<pre>{mdutils.cre_to_md(documents)}</pre>"
elif opt_format == SupportedFormats.CSV.value:
docs = sheet_utils.prepare_spreadsheet(docs=documents)
docs = sheet_utils.ExportSheet().prepare_spreadsheet(
docs=documents, storage=database
)
return write_csv(docs=docs).getvalue().encode("utf-8")
elif opt_format == SupportedFormats.OSCAL.value:
return jsonify(json.loads(oscal_utils.list_to_oscal(documents)))
Expand Down

0 comments on commit 8814dea

Please sign in to comment.