canvasapi
canvasapi copied to clipboard
Error when a course access is restricted
Describe the bug
Several courses at my institution have access restricted after the semester. When a course is restricted, my access token doesn't have permission to get course details. This results in an AttributeError when trying to instantiate a Course object.
$ curl -H "Authorization: Bearer <my access token>" "<my institution url>/api/v1/courses" | jq .
[
{
// this course I can access
// the course details are listed here
},
{
// this course I cannot access
"id": 77520000000029520,
"access_restricted_by_date": true
},
...
]
>>> from canvasapi import Canvas
>>> API_URL = "my institution url"
>>> API_KEY = "my access key"
>>> canvas = Canvas(API_URL, API_KEY)
>>> courses = canvas.get_courses()
>>> for course in courses:
... print(course)
[course 1 name]
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "D:\Repos\unified-deadline\venv\Lib\site-packages\canvasapi\course.py", line 46, in __str__
return "{} {} ({})".format(self.course_code, self.name, self.id)
^^^^^^^^^^^^^^^^
File "D:\Repos\unified-deadline\venv\Lib\site-packages\canvasapi\canvas_object.py", line 14, in __getattribute__
return super(CanvasObject, self).__getattribute__(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Course' object has no attribute 'course_code'
Expected behavior
I think the wrapper should either ignore restricted courses or have a special model for them.
Environment information
- Python version: 3.11.2
- CanvasAPI version: 3.0.0
This came up in a recent discussion (#604) and the best way to avoid this is to request only active courses using the enrollment_state keyword.
I'm not sure I'd call this a bug, maybe an enhancement tag would be a better fit.