How to use the canvasapi.util.obj_or_id function in canvasapi

To help you get started, we’ve selected a few canvasapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ucfopen / canvasapi / tests / test_util.py View on Github external
def test_obj_or_id_str_invalid(self, m):
        with self.assertRaises(TypeError):
            obj_or_id("1a", "user_id", (User,))
github ucfopen / canvasapi / canvasapi / canvas.py View on Github external
Retrieve a planner override for the current user

        :calls: `GET /api/v1/planner/overrides/:id \
        `_

        :param planner_override: The override or the ID of the planner override to retrieve.
        :type planner_override: int or :class:`canvasapi.planner.PlannerOverride`

        :rtype: :class:`canvasapi.planner.PlannerOverride`
        """
        from canvasapi.planner import PlannerOverride

        if isinstance(planner_override, int) or isinstance(
            planner_override, PlannerOverride
        ):
            planner_override_id = obj_or_id(
                planner_override, "planner_override", (PlannerOverride,)
            )
        else:
            raise RequiredFieldMissing(
                "planner_override is required as an object or as an int."
            )

        response = self.__requester.request(
            "GET",
            "planner/overrides/{}".format(planner_override_id),
            _kwargs=combine_kwargs(**kwargs),
        )

        return PlannerOverride(self.__requester, response.json())
github ucfopen / canvasapi / canvasapi / course.py View on Github external
def get_epub_export(self, epub, **kwargs):
        """
        Get information about a single epub export.

        :calls: `GET /api/v1/courses/:course_id/epub_exports/:id\
        `_

        :param epub: Object or ID of ePub Export
        :type epub: int or :class:`canvasapi.course_epub_export.CourseEpubExport`

        :rtype: :class:`canvasapi.course_epub_export.CourseEpubExport`
        """

        epub_id = obj_or_id(epub, "epub", (CourseEpubExport,))

        response = self._requester.request(
            "GET",
            "courses/{}/epub_exports/{}".format(self.id, epub_id),
            _kwargs=combine_kwargs(**kwargs),
        )

        return CourseEpubExport(self._requester, response.json())
github ucfopen / canvasapi / canvasapi / account.py View on Github external
def get_sis_import(self, sis_import, **kwargs):
        """
        Retrieve information on an individual SIS import from this account.

        :calls: `GET /api/v1/accounts/:account_id/sis_imports/:id \
        `_

        :param sis_import: The object or ID of the sis_import to retrieve.
        :type sis_import: int, str or :class:`canvasapi.sis_import.SisImport`

        :rtype: :class:`canvasapi.sis_import.SisImport`
        """
        sis_import_id = obj_or_id(sis_import, "sis_import", (SisImport,))

        response = self._requester.request(
            "GET",
            "accounts/{}/sis_imports/{}".format(self.id, sis_import_id),
            _kwargs=combine_kwargs(**kwargs),
        )

        response_json = response.json()
        response_json.update({"account_id": self.id})

        return SisImport(self._requester, response_json)
github ucfopen / canvasapi / canvasapi / outcome.py View on Github external
Import an outcome group as a subgroup into the current outcome group

        :calls: `POST /api/v1/global/outcome_groups/:id/import \
            `_
            or `POST /api/v1/accounts/:account_id/outcome_groups/:id/import \
            `_
            or `POST /api/v1/courses/:course_id/outcome_groups/:id/import \
            `_

        :param outcome: The object or ID of the outcome group to import.
        :type outcome: :class:`canvasapi.outcome.OutcomeGroup` or int

        :returns: Itself as an OutcomeGroup object.
        :rtype: :class:`canvasapi.outcome.OutcomeGroup`
        """
        source_outcome_group_id = obj_or_id(outcome_group, "outcome_group", (OutcomeGroup,))

        response = self._requester.request(
            'POST',
            '{}/outcome_groups/{}/import'.format(self.context_ref(), self.id),
            source_outcome_group_id=source_outcome_group_id
        )

        return OutcomeGroup(self._requester, response.json())
github ucfopen / canvasapi / canvasapi / course.py View on Github external
:param user: The object or ID of the related user
        :type user: :class:`canvasapi.user.User` or int

        :rtype: `bool`
        """
        from canvasapi.assignment import Assignment
        from canvasapi.user import User

        warnings.warn(
            "`Course.mark_submission_as_unread()` is deprecated and will be "
            "removed in a future version. Use `Submission.mark_unread()` instead.",
            DeprecationWarning,
        )

        assignment_id = obj_or_id(assignment, "assignment", (Assignment,))
        user_id = obj_or_id(user, "user", (User,))

        submission = Submission(
            self._requester,
            {"course_id": self.id, "assignment_id": assignment_id, "user_id": user_id},
        )
        return submission.mark_unread(**kwargs)
github ucfopen / canvasapi / canvasapi / course.py View on Github external
:type assignment: :class:`canvasapi.assignment.Assignment` or int
        :param user: The object or ID of the related user
        :type user: :class:`canvasapi.user.User` or int

        :rtype: `bool`
        """
        from canvasapi.assignment import Assignment
        from canvasapi.user import User

        warnings.warn(
            "`Course.mark_submission_as_read()` is deprecated and will be "
            "removed in a future version. Use `Submission.mark_read()` instead.",
            DeprecationWarning,
        )

        assignment_id = obj_or_id(assignment, "assignment", (Assignment,))
        user_id = obj_or_id(user, "user", (User,))

        submission = Submission(
            self._requester,
            {"course_id": self.id, "assignment_id": assignment_id, "user_id": user_id},
        )
        return submission.mark_read(**kwargs)
github ucfopen / canvasapi / canvasapi / course.py View on Github external
def get_quiz(self, quiz):
        """
        Return the quiz with the given id.

        :calls: `GET /api/v1/courses/:course_id/quizzes/:id \
        `_

        :param quiz: The object or ID of the quiz to retrieve.
        :type quiz: :class:`canvasapi.quiz.Quiz` or int

        :rtype: :class:`canvasapi.quiz.Quiz`
        """
        from canvasapi.quiz import Quiz

        quiz_id = obj_or_id(quiz, "quiz", (Quiz,))

        response = self._requester.request(
            "GET", "courses/{}/quizzes/{}".format(self.id, quiz_id)
        )
        quiz_json = response.json()
        quiz_json.update({"course_id": self.id})

        return Quiz(self._requester, quiz_json)
github ucfopen / canvasapi / canvasapi / group.py View on Github external
def get_content_migration(self, content_migration, **kwargs):
        """
        Retrive a content migration by its ID

        :calls: `GET /api/v1/groups/:group_id/content_migrations/:id \
        `_

        :param content_migration: The object or ID of the content migration to retrieve.
        :type content_migration: int, str or :class:`canvasapi.content_migration.ContentMigration`

        :rtype: :class:`canvasapi.content_migration.ContentMigration`
        """
        from canvasapi.content_migration import ContentMigration

        migration_id = obj_or_id(
            content_migration, "content_migration", (ContentMigration,)
        )

        response = self._requester.request(
            "GET",
            "groups/{}/content_migrations/{}".format(self.id, migration_id),
            _kwargs=combine_kwargs(**kwargs),
        )

        response_json = response.json()
        response_json.update({"group_id": self.id})

        return ContentMigration(self._requester, response_json)
github ucfopen / canvasapi / canvasapi / course.py View on Github external
"""
        Create a new user enrollment for a course or a section.

        :calls: `POST /api/v1/courses/:course_id/enrollments \
        `_

        :param user: The object or ID of the user to enroll in this course.
        :type user: :class:`canvasapi.user.User` or int
        :param enrollment_type: The type of enrollment.
        :type enrollment_type: str
        :rtype: :class:`canvasapi.enrollment.Enrollment`
        """
        from canvasapi.enrollment import Enrollment
        from canvasapi.user import User

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

        response = self._requester.request(
            "POST",
            "courses/{}/enrollments".format(self.id),
            _kwargs=combine_kwargs(**kwargs),
        )

        return Enrollment(self._requester, response.json())