ucfopen / canvasapi

Python API wrapper for Instructure's Canvas LMS. Easily manage courses, users, gradebooks, and more.
https://pypi.python.org/pypi/canvasapi
MIT License
561 stars 175 forks source link

Implement kwargs inside upload_comment to fix #631 #638

Closed bainco closed 11 months ago

bainco commented 11 months ago

-- delete -- see updated PR

bainco commented 11 months ago

In case anyone needs an immediate fix, you can patch this locally in whatever .py file you'd like:

from canvasapi.submission import Submission
from canvasapi.upload import FileOrPathLike, Uploader

def upload_comment(self, file: FileOrPathLike, **kwargs):
    """
    Upload a file to attach to this submission as a comment.
    :calls: `POST \
    /api/v1/courses/:course_id/assignments/:assignment_id/submissions/:user_id/comments/files \
    <https://canvas.instructure.com/doc/api/submission_comments.html#method.submission_comments_api.create_file>`_
    :param file: The file or path of the file to upload.
    :type file: file or str
    :returns: True if the file uploaded successfully, False otherwise, \
        and the JSON response from the API.
    :rtype: tuple
    """
    response = Uploader(
        self._requester,
        "courses/{}/assignments/{}/submissions/{}/comments/files".format(
            self.course_id, self.assignment_id, self.user_id
        ),
        file,
        **kwargs
    ).start()

    if response[0]:
        if "comment" in kwargs:
            kwargs["comment"] |= {"file_ids": [response[1]["id"]]}
        else:
            kwargs["comment"] = {"file_ids": [response[1]["id"]]}

        self.edit(**kwargs)

    return response

# monkey patching built-in Submission.upload_comment with the new version
Submission.upload_comment = upload_comment