Skip to content
Open
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
31 changes: 24 additions & 7 deletions canvasgrader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CanvasGrader(object):
Currently provides ability to create assignments and upload grades.
"""

def __init__(self, base_uri, course_id, id_key, api_key=None):
def __init__(self, base_uri, course_id, id_key=None, api_key=None):
self.base_uri = base_uri
self.course_id = course_id
self.id_key = id_key
Expand Down Expand Up @@ -42,7 +42,7 @@ def create_assignment(self, name, points_possible, published=True):

return response.json()['id']

def grade_assignment(self, assignment_id, grades):
def grade_assignment(self, assignment_id, grades=None, comments=None):
"""Post grades for a given assignment id.

grades is a dictionary which holds grades for the given assignment_id.
Expand All @@ -53,12 +53,29 @@ def grade_assignment(self, assignment_id, grades):
not see an error returned by the API for bad keys.
"""

def make_id_key(sid):
id_key = str(sid)
if self.id_key:
# canvas expects keys in this format for SIS
# https://canvas.instructure.com/doc/api/file.object_ids.html
id_key = "{id_key}:{sid}".format(
id_key = self.id_key, sid = sid)
return id_key


grades_for_canvas = {}
for sid, grade in grades.items():
# canvas expects keys in this format
k = 'grade_data[{id_key}:{sid}][posted_grade]'.format(
id_key=self.id_key, sid=sid)
grades_for_canvas[k] = grade

if grades:
for sid, grade in grades.items():
k = 'grade_data[{id_key}][posted_grade]'.format(
id_key=make_id_key(sid))
grades_for_canvas[k] = grade

if comments:
for sid, comment in comments.items():
k = 'grade_data[{id_key}][text_comment]'.format(
id_key=make_id_key(sid))
grades_for_canvas[k] = comment

return self.session.post(
self.build_url('/assignments/{}/submissions/update_grades'.format(assignment_id)),
Expand Down