def get_enrollments_for_regid(self,
                                  regid,
                                  params={},
                                  include_courses=False):
        """
        Return a list of enrollments for the passed user regid.

        https://canvas.instructure.com/doc/api/enrollments.html#method.enrollments_api.index
        """
        sis_user_id = self._sis_id(regid, sis_field="user")
        url = USERS_API.format(sis_user_id) + "/enrollments"

        courses = Courses() if include_courses else None

        enrollments = []
        for datum in self._get_paged_resource(url, params=params):
            enrollment = CanvasEnrollment(data=datum)
            if include_courses:
                course_id = datum["course_id"]
                course = courses.get_course(course_id)

                if course.sis_course_id is not None:
                    enrollment.course = course
                    # the following 3 lines are not removed
                    # to be backward compatible.
                    enrollment.course_name = course.name
            enrollments.append(enrollment)
        return enrollments
    def test_course_with_params(self):
        canvas = Courses()
        course1 = canvas.get_course(149650, params={"include": ["term"]})

        self.assertEquals(course1.term.term_id, 810,
                          "Course contains term data")
        self.assertEquals(course1.syllabus_body, None,
                          "Course doesn't contain syllabus_body")

        course2 = canvas.get_course(149650,
                                    params={"include": ["syllabus_body"]})

        self.assertEquals(course2.syllabus_body, "Syllabus",
                          "Course contains syllabus_body")
        self.assertEquals(course1.term.term_id, 810,
                          "Course contains term data")
    def handle(self, *args, **options):
        file_path = options.get('file_path')

        outfile = open('submissions.csv', 'wb')
        csv.register_dialect('unix_newline', lineterminator='\n')
        writer = csv.writer(outfile, dialect='unix_newline')

        course_client = Courses()
        sub_client = Submissions()

        file_total = 0
        with open(file_path, 'rb') as csvfile:
            writer.writerow([
                'course_id', 'assignment_id', 'term_id', 'filename',
                'content_type', 'size', 'url'
            ])

            reader = csv.reader(csvfile)
            for row in reader:
                if not len(row):
                    continue

                course_id = row[0]
                assignment_id = row[1]
                assignment_total = 0

                if course_id == 'course_id':
                    continue

                try:
                    course = course_client.get_course(course_id)

                    if course.term.sis_term_id is None:  # Default term
                        print('Skipping: %s' % course.name)
                        continue

                    subs = sub_client.get_submissions_by_course_and_assignment(
                        course_id, assignment_id)

                except DataFailureException as ex:
                    if ex.status == 404:
                        continue
                    else:
                        raise

                for submission in subs:
                    for attachment in submission.attachments:
                        writer.writerow([
                            course_id, assignment_id, course.term.sis_term_id,
                            attachment.display_name.encode('utf-8'),
                            attachment.content_type, attachment.size,
                            attachment.url
                        ])
                        assignment_total += 1

                file_total += assignment_total
                print('%s: %s, total: %s' %
                      (course.sis_course_id, assignment_total, file_total))

        outfile.close()
    def test_course(self):
        canvas = Courses()

        course = canvas.get_course(149650)

        self.assertEquals(course.course_id, 149650, "Has proper course id")
        self.assertEquals(course.course_url,
                          "https://canvas.uw.edu/courses/149650",
                          "Has proper course url")
        self.assertEquals(course.sis_course_id, "2013-spring-PHYS-121-A")
        self.assertEquals(course.sws_course_id(), "2013,spring,PHYS,121/A")
        self.assertEquals(course.sws_instructor_regid(), None)
        self.assertEquals(course.is_academic_sis_id(), True)
        self.assertEquals(course.account_id, 84378, "Has proper account id")
        self.assertEquals(course.term.sis_term_id, "2013-spring",
                          "SIS term id")
        self.assertEquals(course.term.term_id, 810, "Term id")
        self.assertEquals(course.public_syllabus, False, "public_syllabus")
        self.assertEquals(course.workflow_state, "unpublished",
                          "workflow_state")
        self.assertEquals(course.grading_standard_id, 25,
                          "grading_standard_id")
        self.assertTrue(course.is_unpublished)