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 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
Beispiel #3
0
    def get_section(self, section_id, params={}):
        """
        Return section resource for given canvas section id.

        https://canvas.instructure.com/doc/api/sections.html#method.sections.show
        """
        url = SECTIONS_API.format(section_id)
        return CanvasSection(data=self._get_resource(url, params=params))
Beispiel #4
0
    def get_sections_in_course(self, course_id, params={}):
        """
        Return list of sections for the passed course ID.

        https://canvas.instructure.com/doc/api/sections.html#method.sections.index
        """
        url = COURSES_API.format(course_id) + "/sections"

        sections = []
        for data in self._get_paged_resource(url, params=params):
            sections.append(CanvasSection(data=data))

        return sections
Beispiel #5
0
    def create_section(self, course_id, name, sis_section_id):
        """
        Create a canvas section in the given course id.

        https://canvas.instructure.com/doc/api/sections.html#method.sections.create
        """
        url = COURSES_API.format(course_id) + "/sections"
        body = {
            "course_section": {
                "name": name,
                "sis_section_id": sis_section_id
            }
        }

        return CanvasSection(data=self._post_resource(url, body))
Beispiel #6
0
    def update_section(self, section_id, name, sis_section_id):
        """
        Update a canvas section with the given section id.

        https://canvas.instructure.com/doc/api/sections.html#method.sections.update
        """
        url = SECTIONS_API.format(section_id)
        body = {"course_section": {}}

        if name:
            body["course_section"]["name"] = name

        if sis_section_id:
            body["course_section"]["sis_section_id"] = sis_section_id

        return CanvasSection(data=self._put_resource(url, body))
    def post(self, request, *args, **kwargs):
        try:
            course_id = kwargs['canvas_course_id']
            data = json.loads(request.body)
            logins = [x['login'] for x in data['logins']]

            users = []
            for user in AddUser.objects.users_in_course(
                    course_id, data['section_id'], data['role_base'], logins):
                if user.is_valid():
                    users.append(user)

            role = CanvasRole(role_id=data['role_id'],
                              label=data['role'],
                              base_role_type=data['role_base'])
            section = CanvasSection(section_id=data['section_id'],
                                    sis_section_id=data['section_sis_id'],
                                    course_id=course_id)
            section_only = data['section_only']
            notify_users = data['notify_users']
            imp = AddUsersImport(importer=self.blti.user_login_id,
                                 importer_id=self.blti.canvas_user_id,
                                 importing=len(users),
                                 course_id=course_id,
                                 role=role.label,
                                 section_id=section.sis_section_id)
            imp.save()

            connection.close()
            p = Process(target=self._api_import_users,
                        args=(imp.pk, users, role, section, section_only,
                              notify_users))
            p.start()

            return self.json_response(imp.json_data())
        except KeyError as ex:
            return self.error_response(
                400, message='Incomplete Request: {}'.format(ex))
        except Exception as ex:
            return self.error_response(400,
                                       message='Import Error: {}'.format(ex))
    def test_sis_id(self):
        section = CanvasSection()

        # Missing section ID
        self.assertEquals(section.sws_section_id(), None)
        self.assertEquals(section.sws_instructor_regid(), None)
        self.assertEquals(section.is_academic_sis_id(), False)

        # Valid section IDs
        section = CanvasSection(sis_section_id="2013-spring-PHYS-121-A--")
        self.assertEquals(section.sws_section_id(), "2013,spring,PHYS,121/A")
        self.assertEquals(section.sws_instructor_regid(), None)
        self.assertEquals(section.is_academic_sis_id(), True)

        section = CanvasSection(sis_section_id="2013-spring-PHYS-121-AB")
        self.assertEquals(section.sws_section_id(), "2013,spring,PHYS,121/AB")
        self.assertEquals(section.sws_instructor_regid(), None)
        self.assertEquals(section.is_academic_sis_id(), True)

        section = CanvasSection(sis_section_id="2013-autumn-GEN ST-199-A7--")
        self.assertEquals(section.sws_section_id(),
                          "2013,autumn,GEN ST,199/A7")

        section = CanvasSection(sis_section_id=(
            "2013-spring-PHYS-599-A-9136CCB8F66711D5BE060004AC494FFE--"))
        self.assertEquals(section.sws_section_id(), "2013,spring,PHYS,599/A")
        self.assertEquals(section.sws_instructor_regid(),
                          "9136CCB8F66711D5BE060004AC494FFE")
        self.assertEquals(section.is_academic_sis_id(), True)

        # Invalid section IDs
        section = CanvasSection(sis_section_id="2013-spring-PHYS-121-A")
        self.assertEquals(section.sws_section_id(), None)

        section = CanvasSection(sis_section_id="2013-spring-PHYS-121-A-")
        self.assertEquals(section.sws_section_id(), None)

        section = CanvasSection(sis_section_id=(
            "2013-spring-PHYS-599-A-9136CCB8F66711D5BE060004AC494FFE"))
        self.assertEquals(section.sws_section_id(), None)

        section = CanvasSection(sis_section_id=(
            "2013-spring-PHYS-599-A-9136CCB8F66711D5BE060004AC494FFE-"))
        self.assertEquals(section.sws_section_id(), None)

        section = CanvasSection(sis_section_id="course_123456_groups")
        self.assertEquals(section.sws_section_id(), None)
        self.assertEquals(section.sws_instructor_regid(), None)
        self.assertEquals(section.is_academic_sis_id(), False)
def valid_academic_section_sis_id(sis_id):
    if not CanvasSection(sis_section_id=sis_id).is_academic_sis_id():
        raise CoursePolicyException(
            "Invalid academic section SIS ID: {}".format(sis_id))