-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: example script to create or update a form (#83)
- fix: template token copypasta in tests (token from MD template). - chg: use create_ignore_duplicate_error in tests, because that would avoid ignoring issues like the above (invalid form).
- Loading branch information
1 parent
3fd3985
commit d7a4f7f
Showing
5 changed files
with
91 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
docs/examples/create_or_update_form/create_or_update_form.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import sys | ||
from os import PathLike | ||
from pathlib import Path | ||
|
||
from pyodk.client import Client | ||
from pyodk.errors import PyODKError | ||
from requests import Response | ||
|
||
""" | ||
A script to create or update a form, optionally with attachments. | ||
Either use as a CLI script, or import create_or_update into another module. | ||
If provided, all files in the [attachments_dir] path will be uploaded with the form. | ||
""" | ||
|
||
|
||
def create_ignore_duplicate_error(client: Client, definition: PathLike | str | bytes): | ||
"""Create the form; ignore the error raised if it exists (409.3).""" | ||
try: | ||
client.forms.create(definition=definition) | ||
except PyODKError as err: | ||
if len(err.args) >= 2 and isinstance(err.args[1], Response): | ||
err_detail = err.args[1].json() | ||
err_code = err_detail.get("code") | ||
if err_code is not None and str(err_code) == "409.3": | ||
return | ||
raise | ||
|
||
|
||
def create_or_update(form_id: str, definition: str, attachments: str | None): | ||
"""Create (and publish) the form, optionally with attachments.""" | ||
with Client() as client: | ||
create_ignore_duplicate_error(client=client, definition=definition) | ||
attach = None | ||
if attachments is not None: | ||
attach = Path(attachments).iterdir() | ||
client.forms.update( | ||
definition=definition, | ||
form_id=form_id, | ||
attachments=attach, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
usage = """ | ||
Usage: | ||
python create_or_update_form.py form_id definition.xlsx | ||
python create_or_update_form.py form_id definition.xlsx attachments_dir | ||
""" | ||
if len(sys.argv) < 3: | ||
print(usage) | ||
sys.exit(1) | ||
fid = sys.argv[1] | ||
def_path = sys.argv[2] | ||
attach_path = None | ||
if len(sys.argv) == 4: | ||
attach_path = sys.argv[3] | ||
create_or_update(form_id=fid, definition=def_path, attachments=attach_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pyodk==1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters