def test_sws_url(self):
        sdb_course = Course(course_type=Course.SDB_TYPE,
                            course_id = '2013-summer-TRAIN-101-A')
        self.assertEquals(sdb_course.sws_url(), '/restclients/view/sws/student/v5/course/2013,summer,TRAIN,101/A.json')

        adhoc_course = Course(course_type=Course.ADHOC_TYPE,
                              course_id = 'course_12345')
        self.assertEquals(adhoc_course.sws_url(), None)
    def test_types(self):
        sdb_course = Course(course_type=Course.SDB_TYPE)
        self.assertEquals(sdb_course.is_sdb(), True)
        self.assertEquals(sdb_course.is_adhoc(), False)

        adhoc_course = Course(course_type=Course.ADHOC_TYPE)
        self.assertEquals(adhoc_course.is_sdb(), False)
        self.assertEquals(adhoc_course.is_adhoc(), True)
    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']))