Skip to content

Commit

Permalink
feat: returned required media name in a response of form validity (#871)
Browse files Browse the repository at this point in the history
* feat: returned required media name in a response of form validity

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: sujanadh <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 2, 2023
1 parent 0cd6cd9 commit 2dfb69f
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/backend/app/central/central_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,32 @@ async def test_form_validity(xform_content: str, form_type: str):
f.write(xform_content)

xls2xform_convert(xlsform_path=xlsform_path, xform_path=outfile, validate=False)
return {"message": "Your form is valid"}

namespaces = {
"h": "http://www.w3.org/1999/xhtml",
"odk": "http://www.opendatakit.org/xforms",
"xforms": "http://www.w3.org/2002/xforms",
}

import xml.etree.ElementTree as ET

with open(outfile, "r") as xml:
data = xml.read()

root = ET.fromstring(data)
instances = root.findall(".//xforms:instance[@src]", namespaces)

geojson_list = []
for inst in instances:
try:
if "src" in inst.attrib:
if (inst.attrib["src"].split("."))[1] == "geojson":
parts = (inst.attrib["src"].split("."))[0].split("/")
geojson_name = parts[-1]
geojson_list.append(geojson_name)
except Exception:
continue
return {"required media": geojson_list, "message": "Your form is valid"}
except Exception as e:
return JSONResponse(
content={"message": "Your form is invalid", "possible_reason": str(e)},
Expand Down

0 comments on commit 2dfb69f

Please sign in to comment.