Inserting a grade for a student into a course

It is relatively easy to insert a grade for an assignment for a student, the following function handles this:

def assign_grade_for_assignment(course_id, assignment_id, user_id, grade, comment):
    # Use the Canvas API to assign a grade for an assignment
    #PUT /api/v1/courses/:course_id/assignments/:assignment_id/submissions/:user_id

    # Request Parameters:
    # comment[text_comment]		string	Add a textual comment to the submission.
    # comment[group_comment]		boolean	Whether or not this comment should be sent to the entire group (defaults to false). Ignored if this is not a group assignment or if no text_comment is provided.
    # comment[media_comment_id]		string	Add an audio/video comment to the submission.
    # comment[media_comment_type]		string	The type of media comment being added.
    # comment[file_ids][]		integer	Attach files to this comment that were previously uploaded using the Submission Comment API's files action
    # include[visibility]		string	Whether this assignment is visible to the owner of the submission
    # submission[posted_grade]		string	Assign a score to the submission, updating both the “score” and “grade” fields on the submission record. This parameter can be passed in a few different formats:
    # submission[excuse]		boolean	    Sets the “excused” status of an assignment.
    # submission[late_policy_status]		string	Sets the late policy status to either “late”, “missing”, “none”, or null.
    # submission[seconds_late_override]		integer	Sets the seconds late if late policy status is “late”
    # rubric_assessment		RubricAssessment	Assign a rubric assessment to this assignment submission. The sub-parameters here depend on the rubric for the assignment. The general format is, for each row in the rubric:

    url = baseUrl + '%s/assignments/%s/submissions/%s' % (course_id, assignment_id, user_id)
    
    if comment:
            payload={'submission[posted_grade]': grade,
                     'comment[text_comment]': comment
            }
    else:
        payload={'submission[posted_grade]': grade,
        }

    r = requests.put(url, headers = header, data=payload)
    #write_to_log("result of put assign_grade_for_assignment: " + r.text)
    if r.status_code == requests.codes.ok:
        page_response=r.json()
        return True
    return False

A nice feature is being able to insert a comment into the record (this will be display by the gradebook or speedgrader). As long as it is not too long you can basically put a string there containing what you would like to record as a comment for this student's result for this assignment.