def _process_independent_study_section(self, section): """ Generates the import data for an independent study section. This method will create course/section data for each instructor of the section, depending on whether section.independent_study_instructor_regid is set. """ if section is None: return if not section.is_independent_study: raise Exception("Not an independent study section: %s" % ( section.section_label())) match_independent_study = section.independent_study_instructor_regid for instructor in section.get_instructors(): if (match_independent_study is not None and match_independent_study != instructor.uwregid): continue section.independent_study_instructor_regid = instructor.uwregid if not self.data.add(CourseCSV(section=section)): continue self.data.add(TermCSV(section)) self.data.add(SectionCSV(section=section)) Course.objects.update_status(section) if is_active_section(section): 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)
def _process_linked_section(self, section, primary_instructors=[]): """ Generates the import data for a non-independent study linked section. 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 section.is_primary_section: raise Exception("Not a linked section: %s" % ( section.section_label())) if self.data.add(SectionCSV(section=section)): if is_active_section(section): instructors = section.get_instructors() instructors.extend(primary_instructors) for instructor in 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) Course.objects.update_status(section)
def add_registrations_by_section(self, section): try: for registration in get_registrations_by_section(section): self.add_student_enrollment_data(registration) except DataFailureException as ex: self.logger.info("Skip enrollments for section {}: {}".format( section.section_label(), ex))
def test_enrollments(self): user = PWS().get_person_by_netid('javerage') csv = Collector() self.assertEquals(len(csv.enrollments), 0) self.assertEquals( csv.add( EnrollmentCSV(section_id='abc', person=user, role='Student', status='active')), True) self.assertEquals(len(csv.enrollments), 1) section = get_section_by_label('2013,winter,DROP_T,100/B') for registration in get_registrations_by_section(section): self.assertEquals( csv.add(EnrollmentCSV(registration=registration)), True) self.assertEquals(len(csv.enrollments), 3) section = get_section_by_label('2013,spring,TRAIN,101/A') for user in section.get_instructors(): self.assertEquals( csv.add( EnrollmentCSV(section=section, instructor=user, status='active')), True) # Duplicate self.assertEquals( csv.add( EnrollmentCSV(section=section, instructor=user, status='active')), False) self.assertEquals(len(csv.enrollments), 5) self.assertEquals(csv.has_data(), True) # Ad-hoc enrollment self.assertEquals( csv.add( EnrollmentCSV(course_id='course_123', section_id='section_123', person=user, role='Observer', status='active')), True) self.assertEquals(len(csv.enrollments), 6) # Duplicate self.assertEquals( csv.add( EnrollmentCSV(course_id='course_123', section_id='section_123', person=user, role='Observer', status='active')), False)
def test_student_enrollment_csv(self): section = get_section_by_label('2013,winter,DROP_T,100/B') registrations = get_registrations_by_section(section) self.assertEquals(len(registrations), 2) reg0 = registrations[0] reg1 = registrations[1] self.assertEquals(str(EnrollmentCSV(registration=reg0)), (',,260A0DEC95CB11D78BAA000629C31437,student,,' '2013-winter-DROP_T-100-B--,active,\n')) self.assertEquals(str(EnrollmentCSV(registration=reg1)), (',,9136CCB8F66711D5BE060004AC494FFE,student,,' '2013-winter-DROP_T-100-B--,active,\n'))
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