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

#440: added util.py:obj_or_id_or_sis_str, use it in course.py:enroll… #522

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/canvasapi.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/dictionaries/ncdegroot.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 112 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/ruff.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion canvasapi/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
normalize_bool,
obj_or_id,
obj_or_str,
obj_or_id_or_sis_str,
)


Expand Down Expand Up @@ -695,7 +696,7 @@ def enroll_user(self, user, enrollment_type=None, **kwargs):
from canvasapi.enrollment import Enrollment
from canvasapi.user import User

kwargs["enrollment[user_id]"] = obj_or_id(user, "user", (User,))
kwargs["enrollment[user_id]"] = obj_or_id_or_sis_str(user, "user", (User,))

if enrollment_type:
warnings.warn(
Expand Down
4 changes: 2 additions & 2 deletions canvasapi/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from canvasapi.progress import Progress
from canvasapi.submission import GroupedSubmission, Submission
from canvasapi.user import User
from canvasapi.util import combine_kwargs, normalize_bool, obj_or_id
from canvasapi.util import combine_kwargs, normalize_bool, obj_or_id, obj_or_id_or_sis_str


class Section(CanvasObject):
Expand Down Expand Up @@ -94,7 +94,7 @@ def enroll_user(self, user, **kwargs):
:rtype: :class:`canvasapi.enrollment.Enrollment`
"""

kwargs["enrollment[user_id]"] = obj_or_id(user, "user", (User,))
kwargs["enrollment[user_id]"] = obj_or_id_or_sis_str(user, "user", (User,))

response = self._requester.request(
"POST",
Expand Down
44 changes: 44 additions & 0 deletions canvasapi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,50 @@ def obj_or_str(obj, attr, object_types):
raise TypeError("Parameter {} must be of type {}.".format(obj, obj_type_list))


def obj_or_id_or_sis_str(parameter, param_name, object_types):
"""
Accepts either an int (or long or str representation of an integer) or
a 'sis_*_id:[some id]' format string or an object. If it is an int or
a format string, return it.
If it is an object and the object is of correct type, return the object's id. Otherwise,
throw an exception.

:param parameter: int, str, long, or object
:param param_name: str
:param object_types: tuple
:rtype: int
"""
from canvasapi.user import User

try:
return int(parameter)
except (ValueError, TypeError):
# Special case where 'self' is a valid ID of a User object
if User in object_types and parameter == "self":
return parameter

if (
isinstance(parameter, str)
and parameter.startswith("sis_")
and "_id:" in parameter
):
# not foolproof
return parameter

for obj_type in object_types:
if isinstance(parameter, obj_type):
try:
return int(parameter.id)
except Exception:
break

obj_type_list = ",".join([obj_type.__name__ for obj_type in object_types])
message = "Parameter {} must be of type {} or int or a sis_*_id: format string".format(
param_name, obj_type_list
)
raise TypeError(message)


def get_institution_url(base_url):
"""
Clean up a given base URL.
Expand Down
17 changes: 17 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[tool.poetry]
name = "canvasapi"
version = "3.3.0"
description = "Fork to support sis_login_id"
authors = ["Nico de Groot <ndegroot0@gmail.com>"]
readme = "README.md"

[tool.poetry.dependencies]
python = ">=3.9,<4.0.0"

[tool.poetry.group.dev.dependencies]
poetry-core = "*"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Loading