Skip to content

Commit

Permalink
Upload multiple geojson for janakpur (#858)
Browse files Browse the repository at this point in the history
* create xform with two different geojson files fields

* api to generate qr codes and other media files for janakpur pilot project

* upload data extracts according to the category

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

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

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
nrjadkry and pre-commit-ci[bot] authored Sep 28, 2023
1 parent 8139e34 commit 4bf7456
Show file tree
Hide file tree
Showing 3 changed files with 480 additions and 1 deletion.
136 changes: 136 additions & 0 deletions src/backend/app/central/central_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,139 @@ def convert_csv(
csvin.finishGeoJson()

return True


def create_odk_xform_for_janakpur(
project_id: int,
xform_id: str,
filespec: str,
odk_credentials: project_schemas.ODKCentral = None,
create_draft: bool = False,
upload_media=True,
convert_to_draft_when_publishing=True,
):
"""Create an XForm on a remote ODK Central server."""
title = os.path.basename(os.path.splitext(filespec)[0])
# result = xform.createForm(project_id, title, filespec, True)
# Pass odk credentials of project in xform

if not odk_credentials:
odk_credentials = project_schemas.ODKCentral(
odk_central_url=settings.ODK_CENTRAL_URL,
odk_central_user=settings.ODK_CENTRAL_USER,
odk_central_password=settings.ODK_CENTRAL_PASSWD,
)
try:
xform = get_odk_form(odk_credentials)
except Exception as e:
log.error(e)
raise HTTPException(
status_code=500, detail={"message": "Connection failed to odk central"}
) from e

result = xform.createForm(project_id, xform_id, filespec, create_draft)

if result != 200 and result != 409:
return result

# This modifies an existing published XForm to be in draft mode.
# An XForm must be in draft mode to upload an attachment.
if upload_media:
# Upload buildings file
building_file = f"/tmp/buildings_{title}.geojson"

result = xform.uploadMedia(
project_id, title, building_file, convert_to_draft_when_publishing
)

# Upload roads file
road_file = f"/tmp/roads_{title}.geojson"
result = xform.uploadMedia(
project_id, title, road_file, convert_to_draft_when_publishing
)

result = xform.publishForm(project_id, title)
return result


def generate_updated_xform_for_janakpur(
xlsform: str,
xform: str,
form_type: str,
):
"""Update the version in an XForm so it's unique."""
name = os.path.basename(xform).replace(".xml", "")

print("Name in form = ", name)

outfile = xform
if form_type != "xml":
try:
xls2xform_convert(xlsform_path=xlsform, xform_path=outfile, validate=False)
except Exception as e:
log.error(f"Couldn't convert {xlsform} to an XForm!", str(e))
raise HTTPException(status_code=400, detail=str(e)) from e

if os.path.getsize(outfile) <= 0:
log.warning(f"{outfile} is empty!")
raise HTTPException(status=400, detail=f"{outfile} is empty!") from None

xls = open(outfile, "r")
data = xls.read()
xls.close()
else:
xls = open(xlsform, "r")
data = xls.read()
xls.close()

tmp = name.split("_")
tmp[0]
tmp[1]
id = tmp[2].split(".")[0]

buildings_extract = f"jr://file/buildings_{name}.geojson"
roads_extract = f"jr://file/roads_{name}.geojson"

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

root = ET.fromstring(data)
head = root.find("h:head", namespaces)
model = head.find("xforms:model", namespaces)
instances = model.findall("xforms:instance", namespaces)

index = 0
for inst in instances:
try:
if "src" in inst.attrib:
print("SRC = Present")
if (inst.attrib["src"]) == "jr://file/buildings.geojson": # FIXME
print("INST attribs = ", inst.attrib["src"])
inst.attrib["src"] = buildings_extract

if (inst.attrib["src"]) == "jr://file/roads.geojson": # FIXME
inst.attrib["src"] = roads_extract

# Looking for data tags
data_tags = inst.findall("xforms:data", namespaces)
if data_tags:
for dt in data_tags:
dt.attrib["id"] = id
except Exception:
continue
index += 1

# Save the modified XML
newxml = ET.tostring(root)

# write the updated XML file
outxml = open(outfile, "w")
outxml.write(newxml.decode())
outxml.close()

return outfile
Loading

0 comments on commit 4bf7456

Please sign in to comment.