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

Course Audit Log (course.query_audit_log()) breaks, possibly due to API change #667

Open hanleybrand opened 2 months ago

hanleybrand commented 2 months ago

Describe the bug

The following code fails:

course_id = 666666 
course = canvas.get_course(course_id)
logged_events = course.query_audit_by_course()
event_list = [e for e in logged_events]

fails with AttributeError: 'str' object has no attribute 'update' because (I believe) the API has been changed to return a JSON object with three-keys, the basic structure is:

{
    "links":  {},
    "events": [],
    "linked": {}
}

with the second key containing what used to be returned by the API (a list of CourseEvent objects)

To Reproduce

Run the above four lines with canvasapi==3.3.0 after substituting a valid course_id

Expected behavior

[e for e in course.query_audit_by_course()] would produce a list of CourseEvent objects

Fix

I was able to fix the issue by adding _root='events', to the PaginatedList args in Course.query_audit_by_course()

    def query_audit_by_course(self, **kwargs):

        return PaginatedList(
            CourseEvent,
            self._requester,
            "GET",
            "audit/course/courses/{}".format(self.id),
            _root='events',
            _kwargs=combine_kwargs(**kwargs),
        )

Environment information