Exemplo n.º 1
0
    def test_courses(self):
        with self.settings(
                RESTCLIENTS_CANVAS_DAO_CLASS='restclients.dao_implementation.canvas.File'):
            canvas = Courses()

            courses = canvas.get_courses_in_account_by_sis_id(
                'uwcourse:seattle:arts-&-sciences:amath:amath',
                {'published': True})

            self.assertEquals(len(courses), 7, "Too few courses")

            course = courses[2]

            self.assertEquals(course.course_id, 141414, "Has proper course id")
            self.assertEquals(course.sis_course_id, "2013-spring-AMATH-403-A")
            self.assertEquals(course.sws_course_id(), "2013,spring,AMATH,403/A")
            self.assertEquals(course.name, "AMATH 403 A: Methods For Partial Differential Equations")
            self.assertEquals(course.account_id, 333333, "Has proper account id")
            self.assertEquals(course.course_url, "https://canvas.uw.edu/courses/141414", "Has proper course url")
    def handle(self, *args, **options):
        if options['print'] or options['dry_run']:
            print "section_id,canvas_enrollments,sws_enrollments,delta"

        canvas_courses = CanvasCourses(per_page=50)
        canvas_sections = CanvasSections(per_page=50)
        canvas_accounts = CanvasAccounts(per_page=50)
        for account in canvas_accounts.get_all_sub_accounts_by_sis_id(options['root_account']):

            if options['print']:
                print '# Account: "%s" (%s, %s)' % (account.name,
                                                    account.sis_account_id,
                                                    account.account_id)

            if (account.sis_account_id is not None
                and re.match(r'^(([^:]+:){4}|curriculum-).*$', str(account.sis_account_id))):

                n_courses = 0
                n_sections = 0
                n_bad_sections = 0

                if options['all_courses']:
                    courses = canvas_courses.get_courses_in_account_by_sis_id(account.sis_account_id)
                else:
                    courses = canvas_courses.get_published_courses_in_account_by_sis_id(account.sis_account_id)

                for course in courses:
                    if (course.sis_course_id is not None
                        and re.match('^%s-' % options['term'], course.sis_course_id)
                        and not self._is_independent_study(course.sis_course_id)):
                        n_courses += 1
                        sections = canvas_sections.get_sections_with_students_in_course_by_sis_id(course.sis_course_id) 
                        for section in sections:
                            if section.sis_section_id is not None:
                                section_id = section.sis_section_id
                                n_sections += 1

                                try:
                                    s = self.get_section_by_id(section_id)
                                except DataFailureException, err:
                                    print '# BAD SECTION: %s' % err
                                    continue

                                enrollments = (s.current_enrollment + s.auditors)
                                delta = len(section.students) - enrollments
                                if delta >= options['threshold']:
                                    n_bad_sections += 1
                                    if options['print'] or options['dry_run']:
                                        print "%s,%s,%s,%s" % (section_id, len(section.students), enrollments, delta)

                                    if not options['dry_run']:
                                        try:
                                            section_model_id = re.sub(r'--$', '', section_id)
                                            section_model = Course.objects.get(course_id=section_model_id)
                                        except Course.DoesNotExist:
                                            section_model = Course(course_id=section_model_id)

                                        if not section_model.queue_id and section_model.priority < PRIORITY_HIGH:
                                            section_model.priority = PRIORITY_HIGH
                                            section_model.save()

                if options['print']:
                    if n_courses and n_sections and n_bad_sections:
                        print('# %s of %s (%.3s%%) sections in %s courses for %s'
                              % (n_bad_sections, n_sections,
                                 ((n_bad_sections/float(n_sections)) * 100),
                                 n_courses, options['term']))