Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when a course access is restricted #616

Open
gnahtb opened this issue Apr 30, 2023 · 1 comment
Open

Error when a course access is restricted #616

gnahtb opened this issue Apr 30, 2023 · 1 comment
Labels

Comments

@gnahtb
Copy link

gnahtb commented Apr 30, 2023

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
@gnahtb gnahtb added the bug label Apr 30, 2023
@bennettscience
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants