Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable uploading text comments and file in one line #640

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions canvasapi/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def mark_unread(self, **kwargs):
)
return response.status_code == 204

def upload_comment(self, file: FileOrPathLike, **kwargs):
def upload_comment(self, file: FileOrPathLike, text_comment=None, **kwargs):
"""
Upload a file to attach to this submission as a comment.

Expand All @@ -177,7 +177,10 @@ def upload_comment(self, file: FileOrPathLike, **kwargs):
).start()

if response[0]:
self.edit(comment={"file_ids": [response[1]["id"]]})
comment = {"file_ids": [response[1]["id"]]}
if text_comment:
comment["text_comment"] = text_comment
self.edit(comment=comment)
return response


Expand Down
19 changes: 15 additions & 4 deletions tests/test_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from canvasapi.submission import GroupedSubmission, Submission
from tests import settings
from tests.util import cleanup_file, register_uris
from unittest.mock import patch


@requests_mock.Mocker()
Expand Down Expand Up @@ -111,14 +112,24 @@ def test_upload_comment(self, m):
)

filename = "testfile_submission_{}".format(uuid.uuid4().hex)
text_comment = "Here is my file comment"

try:
with open(filename, "w+") as file:
response = self.submission.upload_comment(file)
with patch.object(self.submission, 'edit') as mock_edit:
response = self.submission.upload_comment(file, text_comment=text_comment)

self.assertTrue(response[0])
self.assertIsInstance(response[1], dict)
self.assertIn("url", response[1])

# Verify if the edit method was called with the correct arguments
mock_edit.assert_called_once()
_, kwargs = mock_edit.call_args
self.assertIn("comment", kwargs)
self.assertIn("text_comment", kwargs["comment"])
self.assertEqual(kwargs["comment"]["text_comment"], text_comment)

self.assertTrue(response[0])
self.assertIsInstance(response[1], dict)
self.assertIn("url", response[1])
finally:
cleanup_file(filename)

Expand Down