def _process_instructor_enrollment(self, enrollment):
        if enrollment.section.is_independent_study:
            if enrollment.is_active():
                self.data.add(SectionCSV(section=enrollment.section))
                self.add_teacher_enrollment_data(enrollment.section,
                                                 enrollment.person,
                                                 enrollment.status)
            else:
                enrollment.section.is_withdrawn = True

            # Add or remove independent study course
            self.data.add(CourseCSV(section=enrollment.section))

        elif len(enrollment.section.linked_section_urls):
            # Add/remove primary instructor for each linked section
            for url in enrollment.section.linked_section_urls:
                try:
                    linked_section = get_section_by_url(url)
                    self.data.add(SectionCSV(section=linked_section))
                    self.add_teacher_enrollment_data(linked_section,
                                                     enrollment.person,
                                                     enrollment.status)
                except DataFailureException as err:
                    continue

        else:
            self.data.add(SectionCSV(section=enrollment.section))
            self.add_teacher_enrollment_data(
                enrollment.section, enrollment.person, enrollment.status)
Esempio n. 2
0
    def _process_xlists_for_section(self, section):
        """
        Generates the full xlist import data for the passed primary section.
        """
        if not section.is_primary_section or section.is_independent_study:
            raise CoursePolicyException(
                "Not a primary section: {}".format(section.section_label()))

        course_id = section.canvas_course_sis_id()

        try:
            model = Course.objects.get(course_id=course_id)
        except Course.DoesNotExist:
            return

        existing_xlist_id = model.xlist_id
        new_xlist_id = None

        if len(section.joint_section_urls):
            joint_sections = [section]
            for url in section.joint_section_urls:
                try:
                    joint_sections.append(get_section_by_url(url))
                except Exception as err:
                    self.logger.info("Unable to xlist section {}: {}".format(
                        url, err))

            try:
                new_xlist_id = canvas_xlist_id(joint_sections)
            except Exception as err:
                self.logger.info(
                    "Unable to generate xlist_id for {}: {}".format(
                        course_id, err))

        if existing_xlist_id is None and new_xlist_id is None:
            return

        if existing_xlist_id != new_xlist_id:
            model.xlist_id = new_xlist_id
            model.save()

        linked_section_ids = []
        for url in section.linked_section_urls:
            try:
                linked_section = get_section_by_url(url)
                linked_section_ids.append(
                    linked_section.canvas_section_sis_id())
            except DataFailureException:
                pass

        if not len(section.linked_section_urls):
            # Use the dummy section
            linked_section_ids.append(section.canvas_section_sis_id())

        for linked_section_id in linked_section_ids:
            if (existing_xlist_id is not None and
                    existing_xlist_id != new_xlist_id):
                self.data.add(XlistCSV(existing_xlist_id, linked_section_id,
                                       status='deleted'))

            if (new_xlist_id is not None and new_xlist_id != course_id):
                self.data.add(XlistCSV(new_xlist_id, linked_section_id,
                                       status='active'))
    def _process_primary_section(self, section):
        """
        Generates the import data for a non-independent study primary section.
        Primary sections are added to courses, linked (secondary) sections are
        added to sections.
        """
        if section is None:
            return

        if section.is_independent_study:
            raise Exception("Independent study section: %s" % (
                section.section_label()))

        if not section.is_primary_section:
            raise Exception("Not a primary section: %s" % (
                section.section_label()))

        if not self.data.add(CourseCSV(section=section)):
            return

        self.data.add(TermCSV(section))
        Course.objects.update_status(section)

        course_id = section.canvas_course_sis_id()
        primary_instructors = section.get_instructors()
        if len(section.linked_section_urls):
            for url in section.linked_section_urls:
                try:
                    linked_section = get_section_by_url(url)
                    Course.objects.add_to_queue(linked_section, self.queue_id)
                except:
                    continue

                # Add primary section instructors to each linked section
                self._process_linked_section(linked_section,
                                             primary_instructors)
        else:
            self.data.add(SectionCSV(section=section))

            if is_active_section(section):
                for instructor in primary_instructors:
                    self.add_teacher_enrollment_data(section, instructor)

                if self.include_enrollment:
                    for registration in get_registrations_by_section(section):
                        self.add_student_enrollment_data(registration)

        # Check for linked sections already in the Course table
        for linked_course_id in Course.objects.get_linked_course_ids(
                course_id):
            try:
                linked_section = self.get_section_resource_by_id(
                    linked_course_id)
                Course.objects.add_to_queue(linked_section, self.queue_id)
                self._process_linked_section(linked_section,
                                             primary_instructors)
            except Exception as ex:
                Course.objects.remove_from_queue(linked_course_id, ex)
                continue

        # Iterate over joint sections
        for url in section.joint_section_urls:
            try:
                joint_section = get_section_by_url(url)
                model = Course.objects.add_to_queue(joint_section,
                                                    self.queue_id)
            except:
                continue

            try:
                self._process_primary_section(joint_section)
            except Exception as ex:
                Course.objects.remove_from_queue(model.course_id, ex)

        # Joint sections already joined to this section in the Course table
        for joint_course_id in Course.objects.get_joint_course_ids(course_id):
            try:
                joint_section = self.get_section_resource_by_id(
                    joint_course_id)
                Course.objects.add_to_queue(joint_section, self.queue_id)
                self._process_primary_section(joint_section)

            except Exception as ex:
                Course.objects.remove_from_queue(joint_course_id, ex)

        self._process_xlists_for_section(section)

        # Find any sections that are manually cross-listed to this course,
        # so we can update enrollments for those
        for s in get_sis_sections_for_course(course_id):
            try:
                course_model_id = re.sub(r'--$', '', s.sis_section_id)
                course = Course.objects.get(course_id=course_model_id,
                                            queue_id__isnull=True)
                self._process(course)
            except Course.DoesNotExist:
                pass
    def _process_xlists_for_section(self, section):
        """
        Generates the full xlist import data for the passed primary section.
        """
        if not section.is_primary_section:
            raise Exception(
                "Not a primary section %s:" % section.section_label())

        if section.is_independent_study:
            raise Exception(
                "Independent study section %s:" % section.section_label())

        course_id = section.canvas_course_sis_id()

        try:
            model = Course.objects.get(course_id=course_id)
        except Course.DoesNotExist:
            return

        existing_xlist_id = model.xlist_id
        new_xlist_id = None

        if len(section.joint_section_urls):
            joint_sections = [section]
            for url in section.joint_section_urls:
                try:
                    joint_sections.append(get_section_by_url(url))
                except DataFailureException:
                    pass

            try:
                new_xlist_id = canvas_xlist_id(joint_sections)
            except Exception as err:
                self.logger.info("Unable to generate xlist_id for %s: %s" % (
                    course_id, err))

        if existing_xlist_id is None and new_xlist_id is None:
            return

        if existing_xlist_id != new_xlist_id:
            model.xlist_id = new_xlist_id
            model.save()

        linked_section_ids = []
        for url in section.linked_section_urls:
            try:
                linked_section = get_section_by_url(url)
                linked_section_ids.append(
                    linked_section.canvas_section_sis_id())
            except:
                pass

        if not len(section.linked_section_urls):
            # Use the dummy section
            linked_section_ids.append(section.canvas_section_sis_id())

        for linked_section_id in linked_section_ids:
            if (existing_xlist_id is not None and
                    existing_xlist_id != new_xlist_id):
                self.data.add(XlistCSV(existing_xlist_id, linked_section_id,
                                       status='deleted'))

            if (new_xlist_id is not None and new_xlist_id != course_id):
                self.data.add(XlistCSV(new_xlist_id, linked_section_id,
                                       status='active'))