def setUp(self): self.canvas = Canvas(settings.BASE_URL, settings.API_KEY) self.account = Account(self.canvas._Canvas__requester, {"id": 1}) self.AccountReport = AccountReport( self.canvas._Canvas__requester, { "id": 1, "title": "Zero Activity", "parameters": { "enrollment_term_id": { "required": False, "description": "The canvas id of the term to get grades from", }, "start_at": { "required": False, "description": "The first date in the date range", }, "course_id": { "required": False, "description": "The course to report on", }, }, "report": "zero_activity_csv", "last_run": "null", }, )
def get_parent(self, **kwargs): """ Return the object that spawned this content migration. :rtype: :class:`canvasapi.group.Account`, or :class:`canvasapi.course.Course`, or :class:`canvasapi.course.Group`, or :class:`canvasapi.course.User` """ from canvasapi.account import Account from canvasapi.course import Course from canvasapi.group import Group from canvasapi.user import User response = self._requester.request( "GET", "{}s/{}".format(self._parent_type, self._parent_id), _kwargs=combine_kwargs(**kwargs), ) if self._parent_type == "group": return Group(self._requester, response.json()) elif self._parent_type == "course": return Course(self._requester, response.json()) elif self._parent_type == "account": return Account(self._requester, response.json()) elif self._parent_type == "user": return User(self._requester, response.json())
def get_account(self, account, use_sis_id=False, **kwargs): """ Retrieve information on an individual account. :calls: `GET /api/v1/accounts/:id \ <https://canvas.instructure.com/doc/api/accounts.html#method.accounts.show>`_ :param account: The object or ID of the account to retrieve. :type account: int, str or :class:`canvasapi.account.Account` :param use_sis_id: Whether or not account_id is an sis ID. Defaults to `False`. :type use_sis_id: bool :rtype: :class:`canvasapi.account.Account` """ if use_sis_id: account_id = account uri_str = 'accounts/sis_account_id:{}' else: account_id = obj_or_id(account, "account", (Account, )) uri_str = 'accounts/{}' response = self.__requester.request('GET', uri_str.format(account_id), _kwargs=combine_kwargs(**kwargs)) return Account(self.__requester, response.json())
def load_courses(account: Account, courses: List[Dict]) -> List[Course]: """ Load a list of courses via the Canvas API. Parameters ---------- account : Account An Account SDK object courses: List[Dict] A list of JSON-like course objects in a form suitable for submission to the Canvas course creation endpoint. Returns ------- List[Course] A list of Canvas SDK Course objects representing the created courses """ logger.info("Creating %s courses via Canvas API", len(courses)) result: List[Course] = [] for course in courses: result.append(account.create_course(**course)) logger.info("Successfully created %s courses", len(courses)) return result
def load_users(account: Account, users: List[Dict]) -> List[User]: """ Load a list of users via the Canvas API. Parameters ---------- account : Account An Account SDK object users: List[Dict] A list of JSON-like user objects in a form suitable for submission to the Canvas user creation endpoint. Returns ------- List[User] A list of Canvas SDK User objects representing the created users """ logger.info("Creating %s users via Canvas API", len(users)) result: List[User] = [] for user in users: try: result.append(account.create_user(user)) except Exception as ex: logger.exception(ex) time.sleep(10) logger.info("Successfully created %s users", len(result)) return result
def create_account(self, **kwargs): """ Create a new root account. :calls: `POST /api/v1/accounts \ <https://canvas.instructure.com/doc/api/accounts.html#method.accounts.create>`_ :rtype: :class:`canvasapi.account.Account` """ response = self.__requester.request('POST', 'accounts', **combine_kwargs(**kwargs)) return Account(self.__requester, response.json())
def rollback_loaded_users(account: Account, users: List[User]): """ Delete already loaded users via the Canvas API. **** Used during testing of the load functionality **** Parameters ---------- account : Account An Account SDK object users: List[User] A list of User SDK objects from a successful user load operation """ logger.info( "**** Rolling back %s users via Canvas API - for testing purposes", len(users), ) for user in users: account.delete_user(user) logger.info("**** Successfully deleted %s users", len(users))
def get_account(self, account_id): """ Retrieve information on an individual account. :calls: `GET /api/v1/accounts/:id \ <https://canvas.instructure.com/doc/api/accounts.html#method.accounts.show>`_ :param account_id: The ID of the account to retrieve. :type account_id: int :rtype: :class:`canvasapi.account.Account` """ response = self.__requester.request('GET', 'accounts/%s' % (account_id)) return Account(self.__requester, response.json())
def get_parent(self): """ Return the object that spawned this tool. :rtype: :class:`canvasapi.account.Account` or :class:`canvasapi.account.Course` """ from canvasapi.account import Account from canvasapi.course import Course response = self._requester.request( 'GET', '%ss/%s' % (self.parent_type, self.parent_id)) if self.parent_type == 'account': return Account(self._requester, response.json()) elif self.parent_type == 'course': return Course(self._requester, response.json())
def get_parent(self): """ Return the object that spawned this tool. :rtype: :class:`canvasapi.account.Account` or :class:`canvasapi.account.Course` """ from canvasapi.account import Account from canvasapi.course import Course response = self._requester.request( "GET", "{}s/{}".format(self.parent_type, self.parent_id)) if self.parent_type == "account": return Account(self._requester, response.json()) elif self.parent_type == "course": return Course(self._requester, response.json())
class TestAccountReport(unittest.TestCase): def setUp(self): self.canvas = Canvas(settings.BASE_URL, settings.API_KEY) self.account = Account(self.canvas._Canvas__requester, {"id": 1}) self.AccountReport = AccountReport( self.canvas._Canvas__requester, { "id": 1, "title": "Zero Activity", "parameters": { "enrollment_term_id": { "required": False, "description": "The canvas id of the term to get grades from", }, "start_at": { "required": False, "description": "The first date in the date range", }, "course_id": { "required": False, "description": "The course to report on", }, }, "report": "zero_activity_csv", "last_run": "null", }, ) # __str__() def test__str__(self, m): string = str(self.AccountReport) self.assertIsInstance(string, str) # get_features() def test_get_features(self, m): register_uris({"account": ["get_features"]}, m) features = self.account.get_features() self.assertIsInstance(features, PaginatedList) self.assertIsInstance(features[0], Feature) # get_enabled_features() def test_get_enabled_features(self, m): register_uris({"account": ["get_enabled_features"]}, m) features = self.account.get_enabled_features() self.assertIsInstance(features, PaginatedList) self.assertIsInstance(features[0], Feature) # get_feature_flag() def test_get_feature_flag(self, m): register_uris({"account": ["get_features", "get_feature_flag"]}, m) feature = self.account.get_features()[0] feature_flag = self.account.get_feature_flag(feature) self.assertIsInstance(feature_flag, FeatureFlag) self.assertEqual(feature_flag.feature, "epub_export")