Skip to content

Commit

Permalink
fix: do not index xform path for form name
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Feb 26, 2024
1 parent 42c18b1 commit e02a2e5
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions osm_fieldwork/OdkCentral.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import zlib
from base64 import b64encode
from datetime import datetime
from pathlib import Path
from typing import Optional

import requests
Expand Down Expand Up @@ -833,28 +834,21 @@ def uploadMedia(
filespec (str): The filespec of the media file
convert_to_draft (bool): Whether to convert a published XForm to draft
"""
title = os.path.basename(os.path.splitext(filespec)[0])
datafile = f"{title}.geojson"

if xform.find("_") > 0:
xid = xform.split("_")[2]
else:
xid = xform
geojson_file = Path(filespec).name

if convert_to_draft:
url = f"{self.base}projects/{projectId}/forms/{xid}/draft"
url = f"{self.base}projects/{projectId}/forms/{xform}/draft"
result = self.session.post(url, verify=self.verify)
if result.status_code == 200:
log.debug(f"Modified {title} to draft")
log.debug(f"Modified {xform} to draft")
else:
status = eval(result._content)
log.error(f"Couldn't modify {title} to draft: {status['message']}")
log.error(f"Couldn't modify {xform} to draft: {status['message']}")

url = f"{self.base}projects/{projectId}/forms/{xid}/draft/attachments/{datafile}"
url = f"{self.base}projects/{projectId}/forms/{xform}/draft/attachments/{geojson_file}"
headers = {"Content-Type": "*/*"}
file = open(filespec, "rb")
media = file.read()
file.close()
with open(filespec, "rb") as file:
media = file.read()
result = self.session.post(url, data=media, headers=headers, verify=self.verify)
if result.status_code == 200:
log.debug(f"Uploaded {filespec} to Central")
Expand Down Expand Up @@ -974,12 +968,8 @@ def publishForm(
(int): The staus code from ODK Central
"""
version = datetime.now().strftime("%Y-%m-%dT%TZ")
if xform.find("_") > 0:
xid = xform.split("_")[2]
else:
xid = xform

url = f"{self.base}projects/{projectId}/forms/{xid}/draft/publish?version={version}"
url = f"{self.base}projects/{projectId}/forms/{xform}/draft/publish?version={version}"
result = self.session.post(url, verify=self.verify)
if result.status_code != 200:
status = eval(result._content)
Expand All @@ -999,12 +989,7 @@ def form_fields(self, projectId: int, xform: str):
dict: A json object containing the form fields.
"""
if xform.find("_") > 0:
xid = xform.split("_")[2]
else:
xid = xform

url = f"{self.base}projects/{projectId}/forms/{xid}/fields?odata=true"
url = f"{self.base}projects/{projectId}/forms/{xform}/fields?odata=true"
result = self.session.get(url, verify=self.verify)
return result.json()

Expand Down

0 comments on commit e02a2e5

Please sign in to comment.