Skip to content

Commit b6ad694

Browse files
committed
Enable uploading text comments and file in one line
1 parent 02d42cb commit b6ad694

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

canvasapi/submission.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def mark_unread(self, **kwargs):
153153
)
154154
return response.status_code == 204
155155

156-
def upload_comment(self, file: FileOrPathLike, **kwargs):
156+
def upload_comment(self, file: FileOrPathLike, text_comment=None, **kwargs):
157157
"""
158158
Upload a file to attach to this submission as a comment.
159159
@@ -177,7 +177,10 @@ def upload_comment(self, file: FileOrPathLike, **kwargs):
177177
).start()
178178

179179
if response[0]:
180-
self.edit(comment={"file_ids": [response[1]["id"]]})
180+
comment = {"file_ids": [response[1]["id"]]}
181+
if text_comment:
182+
comment["text_comment"] = text_comment
183+
self.edit(comment=comment)
181184
return response
182185

183186

tests/test_submission.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from canvasapi.submission import GroupedSubmission, Submission
1010
from tests import settings
1111
from tests.util import cleanup_file, register_uris
12+
from unittest.mock import patch
1213

1314

1415
@requests_mock.Mocker()
@@ -111,14 +112,24 @@ def test_upload_comment(self, m):
111112
)
112113

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

115117
try:
116118
with open(filename, "w+") as file:
117-
response = self.submission.upload_comment(file)
119+
with patch.object(self.submission, 'edit') as mock_edit:
120+
response = self.submission.upload_comment(file, text_comment=text_comment)
121+
122+
self.assertTrue(response[0])
123+
self.assertIsInstance(response[1], dict)
124+
self.assertIn("url", response[1])
125+
126+
# Verify if the edit method was called with the correct arguments
127+
mock_edit.assert_called_once()
128+
_, kwargs = mock_edit.call_args
129+
self.assertIn("comment", kwargs)
130+
self.assertIn("text_comment", kwargs["comment"])
131+
self.assertEqual(kwargs["comment"]["text_comment"], text_comment)
118132

119-
self.assertTrue(response[0])
120-
self.assertIsInstance(response[1], dict)
121-
self.assertIn("url", response[1])
122133
finally:
123134
cleanup_file(filename)
124135

0 commit comments

Comments
 (0)