def instructor_regid_from_section_id(section_id): canvas_section = CanvasSection(sis_section_id=section_id) reg_id = canvas_section.sws_instructor_regid() if reg_id is None: canvas_course = CanvasCourse(sis_course_id=section_id) reg_id = canvas_course.sws_instructor_regid() return reg_id
def test_course(self): course = CanvasCourse() course.sis_id = "2013-winter-CHEM-121" sws_id = course.sws_course_id() self.assertEquals( sws_id, None, "Invalid SIS ID leads to an sws_id of None")
def section_label_from_section_id(section_id): canvas_section = CanvasSection(sis_section_id=section_id) section_label = canvas_section.sws_section_id() if section_label is None: valid_academic_course_sis_id(section_id) canvas_course = CanvasCourse(sis_course_id=section_id) section_label = canvas_course.sws_course_id() return section_label
def create_course(self, account_id, course_name): """ Create a canvas course with the given subaccount id and course name. https://canvas.instructure.com/doc/api/courses.html#method.courses.create """ url = ACCOUNTS_API.format(account_id) + "/courses" body = {"course": {"name": course_name}} return CanvasCourse(data=self._post_resource(url, body))
def update_sis_id(self, course_id, sis_course_id): """ Updates the SIS ID for the course identified by the passed course ID. https://canvas.instructure.com/doc/api/courses.html#method.courses.update """ url = COURSES_API.format(course_id) body = {"course": {"sis_course_id": sis_course_id}} return CanvasCourse(data=self._put_resource(url, body))
def get_course_sections(course, user_id): sections = [] canvas = Sections(as_user=user_id) for section in canvas.get_sections_in_course(course.course_id): if not valid_group_section(section.sis_section_id): sections.append({ 'id': section.section_id, 'sis_id': section.sis_section_id, 'name': section.name }) if not len(sections): canvas_course = CanvasCourse(sis_course_id=course.sis_course_id) if canvas_course.is_academic_sis_id(): raise MissingSectionException( 'Adding users to this course not allowed') else: sections.append({'id': 0, 'sis_id': '', 'name': course.name}) return sections
def get_course(self, course_id, params={}): """ Return course resource for given canvas course id. https://canvas.instructure.com/doc/api/courses.html#method.courses.show """ include = params.get("include", []) if "term" not in include: include.append("term") params["include"] = include url = COURSES_API.format(course_id) return CanvasCourse(data=self._get_resource(url, params=params))
def get_courses_in_account(self, account_id, params={}): """ Returns a list of courses for the passed account ID. https://canvas.instructure.com/doc/api/accounts.html#method.accounts.courses_api """ if "published" in params: params["published"] = "true" if params["published"] else "" url = ACCOUNTS_API.format(account_id) + "/courses" courses = [] for data in self._get_paged_resource(url, params=params): courses.append(CanvasCourse(data=data)) return courses
def get_subject_guide(sis_course_id, campus): if not CanvasCourse(sis_course_id=sis_course_id).is_academic_sis_id(): return get_default_subject_guide(campus=campus) try: subject_guide = get_subject_guide_for_canvas_course_sis_id( sis_course_id) # Library service only returns Seattle campus default guide if (subject_guide.is_default_guide and subject_guide.default_guide_campus.lower() != campus): subject_guide = get_default_subject_guide(campus=campus) return subject_guide except DataFailureException: return get_default_subject_guide(campus=campus)
def get_courses_for_regid(self, regid, params={}): """ Return a list of courses for the passed regid. https://canvas.instructure.com/doc/api/courses.html#method.courses.index """ self._as_user = regid data = self._get_resource("/api/v1/courses", params=params) self._as_user = None courses = [] for datum in data: if "sis_course_id" in datum: courses.append(CanvasCourse(data=datum)) else: courses.append(self.get_course(datum["id"], params)) return courses
def get(self, request, *args, **kwargs): course_id = kwargs['canvas_course_id'] user_id = self.blti.canvas_user_id course_name = self.blti.course_long_name sis_course_id = self.blti.course_sis_id try: course = CanvasCourse(course_id=course_id, sis_course_id=sis_course_id, name=course_name) sections = get_course_sections(course, user_id) except MissingSectionException as err: msg = 'Adding users to this course not allowed' return self.error_response(401, message=msg) except DataFailureException as err: return self.error_response(500, message=err.msg) except Exception as err: return self.error_response(500, message=traceback.format_exc(err)) return self.json_response( {'sections': sorted(sections, key=lambda k: k['name'])})
def test_sis_id(self): course = CanvasCourse() self.assertEquals(course.sws_course_id(), None) self.assertEquals(course.sws_instructor_regid(), None) self.assertEquals(course.is_academic_sis_id(), False) course = CanvasCourse(sis_course_id="2013-spring-PHYS-121-A") self.assertEquals(course.sws_course_id(), "2013,spring,PHYS,121/A") self.assertEquals(course.sws_instructor_regid(), None) self.assertEquals(course.is_academic_sis_id(), True) course = CanvasCourse(sis_course_id="2013-autumn-GEN ST-199-A7") self.assertEquals(course.sws_course_id(), "2013,autumn,GEN ST,199/A7") self.assertEquals(course.sws_instructor_regid(), None) self.assertEquals(course.is_academic_sis_id(), True) course = CanvasCourse(sis_course_id=( "2013-spring-PHYS-599-A-9136CCB8F66711D5BE060004AC494FFE")) self.assertEquals(course.sws_course_id(), "2013,spring,PHYS,599/A") self.assertEquals(course.sws_instructor_regid(), "9136CCB8F66711D5BE060004AC494FFE") self.assertEquals(course.is_academic_sis_id(), True) course = CanvasCourse(sis_course_id="course_123456") self.assertEquals(course.sws_course_id(), None) self.assertEquals(course.sws_instructor_regid(), None) self.assertEquals(course.is_academic_sis_id(), False)
def valid_academic_course_sis_id(sis_id): if not CanvasCourse(sis_course_id=sis_id).is_academic_sis_id(): raise CoursePolicyException( "Invalid academic course SIS ID: {}".format(sis_id))
def test_get_course_sections(self, mock_method): course = CanvasCourse(course_id='123', sis_course_id='789', name='xyz') r = get_course_sections(course, '456') mock_method.assert_called_with('123')
def test_sections_constructor(self, mock_object): course = CanvasCourse(course_id='123', sis_course_id='789', name='xyz') r = get_course_sections(course, '456') mock_object.assert_called_with(as_user='******')