Skip to content

Commit

Permalink
feat: allow uploading attachments alongside submission
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Nov 21, 2024
1 parent fdfa022 commit 9d8cec5
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pyodk/_endpoints/submissions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import builtins
import logging
from collections.abc import Iterable
from datetime import datetime
from pathlib import Path
from typing import Any

from pyodk._endpoints import bases
from pyodk._endpoints.comments import Comment, CommentService
from pyodk._endpoints.submission_attachments import SubmissionAttachmentService
from pyodk._utils import validators as pv
from pyodk._utils.session import Session
from pyodk.errors import PyODKError
Expand Down Expand Up @@ -198,6 +201,8 @@ def create(
project_id: int | None = None,
device_id: str | None = None,
encoding: str = "utf-8",
# Here we must use imported typing.List to avoid conflict with .list method
attachments: builtins.list[str] | None = None,
) -> Submission:
"""
Create a Submission.
Expand All @@ -219,6 +224,7 @@ def create(
:param project_id: The id of the project this form belongs to.
:param device_id: An optional deviceID associated with the submission.
:param encoding: The encoding of the submission XML, default "utf-8".
:param attachments: A list of file paths to upload as attachments.
"""
try:
pid = pv.validate_project_id(project_id, self.default_project_id)
Expand All @@ -239,7 +245,26 @@ def create(
data=xml.encode(encoding=encoding),
)
data = response.json()
return Submission(**data)
submission = Submission(**data)
instance_id = submission.instanceId

# If there are attachments, upload each one
if attachments:
attachment_svc = SubmissionAttachmentService(session=self.session)
for attachment in attachments:
attachment_path = Path(attachment)
file_name = attachment_path.name
upload_success = attachment_svc.upload(
file_path_or_bytes=attachment,
instance_id=instance_id,
file_name=file_name,
form_id=fid,
project_id=pid,
)
if not upload_success:
log.error(f"Failed to upload attachment: {attachment}")

return submission

def _put(
self,
Expand Down

0 comments on commit 9d8cec5

Please sign in to comment.