コード例 #1
0
    def response_for_person(self, person):
        response = {
            'is_valid': True,
            'display_name': person.full_name if (
                isinstance(person, Person)) else person.display_name,
            'net_id': person.uwnetid,
            'reg_id': person.uwregid,
            'gmail_id': None,
            'added_date': None,
            'provisioned_date': None,
            'priority': 'normal',
            'queue_id': None,
            'person_url': None,
            'enrollment_url': None
        }

        if can_view_source_data():
            response['person_url'] = '%s/person/%s.json' % (
                '/restclients/view/pws/identity/v1', person.uwregid)
            response['enrollment_url'] = '%s/enrollment.json?reg_id=%s' % (
                '/restclients/view/sws/student/v5', person.uwregid)

        try:
            user = User.objects.get(reg_id=person.uwregid)
            response.update(user.json_data())

        except User.DoesNotExist:
            pass

        return self.json_response(json.dumps(response))
コード例 #2
0
    def GET(self, request, **kwargs):
        try:
            course = Course.objects.get(
                course_id=self._normalize(kwargs['course_id']))
            json_data = course.json_data(can_view_source_data())
            return self.json_response(json.dumps(json_data))

        except Exception as err:
            return self.json_response(
                '{"error":"Course not found (%s)"}' % err, status=404)
コード例 #3
0
    def GET(self, request, **kwargs):
        try:
            sis_id = kwargs.get('sis_id')
            canvas_id = re.match(r'^\d+$', sis_id)
            if canvas_id:
                course = get_course_by_id(canvas_id.group(0))
            else:
                course = get_course_by_sis_id(sis_id)

            course_rep = {
                'course_id': course.course_id,
                'sis_course_id': course.sis_course_id,
                'sws_course_id': course.sws_course_id(),
                'account_id': course.account_id,
                'term': {
                    'term_id': course.term.term_id,
                    'sis_term_id': course.term.sis_term_id,
                    'name': course.term.name
                },
                'course_name': course.name,
                'course_url': "%s/courses/%s" % (
                    getattr(settings, 'RESTCLIENTS_CANVAS_HOST', ''),
                    course.course_id),
                'workflow_state': course.workflow_state,
                'public_syllabus': course.public_syllabus,
                'syllabus_body': course.syllabus_body
            }

            if course.sis_course_id is not None:
                try:
                    model = Course.objects.get(course_id=course.sis_course_id)
                    course_rep.update(model.json_data(can_view_source_data()))
                except Course.DoesNotExist:
                    pass

            return self.json_response(json.dumps(course_rep))
        except Exception as e:
            return self.json_response(
                '{"error": "Unable to retrieve course data: %s"' % (e) + ' }',
                status=400)
コード例 #4
0
    def PUT(self, request, **kwargs):
        try:
            course = Course.objects.get(
                course_id=self._normalize(kwargs['course_id']))
        except Exception as err:
            return self.json_response(
                '{"error":"Course not found (%s)"}' % err, status=404)

        if course.queue_id is not None:
            return self.json_response(
                '{"error":"Course already being provisioned"}', status=409)

        body = request.read()
        try:
            new_values = json.loads(body)
        except Exception as err:
            return self.json_response(
                '{"error":"Unable to parse JSON (%s)" }' % err, status=400)

        try:
            # only priority PUTable right now
            param = new_values.get('priority', '').lower()
            new_priority = None
            for key, val in dict(PRIORITY_CHOICES).iteritems():
                if val == param:
                    new_priority = key
                    break

            if new_priority is not None:
                course.priority = new_priority
                course.save()
            else:
                raise Exception("Invalid priority: '%s'" % param)

            json_data = course.json_data(can_view_source_data())
            return self.json_response(json.dumps(json_data))
        except Exception as err:
            return self.json_response('{"error":"%s"}' % err, status=400)
コード例 #5
0
    def GET(self, request, **kwargs):
        json_rep = {
            'courses': []
        }

        filt_kwargs = None

        if 'queue_id' in request.GET:
            queue_id = request.GET.get('queue_id', '').strip()
            if re.match(r'^[0-9]+$', str(queue_id)):
                filt_kwargs = {'queue_id': queue_id}
            else:
                err = 'invalid queue_id: %s' % queue_id
                self._log.error(err)
                return self.json_response('{"error":"%s"}' % err, status=400)
        else:
            provisioned_error = request.GET.get('provisioned_error')
            if provisioned_error:
                filt_kwargs = {
                    'provisioned_error': self._is_true(provisioned_error),
                    'queue_id__isnull': True
                }

        if filt_kwargs:
            try:
                filt_kwargs['priority__gt'] = PRIORITY_NONE
                course_list = list(Course.objects.filter(
                    **filt_kwargs).order_by('course_id'))

                include_sws_url = can_view_source_data()
                for course in course_list:
                    json_data = course.json_data(include_sws_url)
                    json_rep['courses'].append(json_data)

                return self.json_response(json.dumps(json_rep))
            except Exception as err:
                self._log.error('course kwargs search fail: %s' + err)
                return self.json_response('{"error":"%s"}' % err, status=400)

        net_id = None
        reg_id = None
        try:
            if 'net_id' in request.GET:
                net_id = netid_from_request(request.GET)
            elif 'reg_id' in request.GET:
                reg_id = regid_from_request(request.GET)
            else:
                self._criteria[2]['required'] = True

            filter_terms = self._validCourseFilter(request)
            filter_prefix = '-'.join(filter_terms)
            course_list = list(Course.objects.filter(
                course_id__startswith=filter_prefix).order_by('course_id'))
        except CourseInvalidException as err:
            return self.json_response('{"error":"%s"}' % err, status=400)
        except Exception as err:
            self._log.error('course filter fail: %s' % err)
            return self.json_response('{"error":"%s"}' % err, status=400)

        if (net_id is not None or reg_id is not None) and len(course_list):
            try:
                if net_id is not None:
                    instructor = get_person_by_netid(net_id)
                else:
                    instructor = get_person_by_regid(reg_id)

                year = request.GET.get('year')
                quarter = request.GET.get('quarter')
                term = get_term_by_year_and_quarter(year, quarter)

                white_list = []
                for section in get_sections_by_instructor_and_term(
                        instructor, term):
                    white_list.append('-'.join([
                        section.term.canvas_sis_id(),
                        section.curriculum_abbr.upper(),
                        section.course_number,
                        section.section_id.upper()]))

            except Exception as err:
                self._log.error('section search fail: %s' % err)
                return self.json_response('{"error":"%s"}' % err, status=400)

        include_sws_url = can_view_source_data()
        for course in course_list:
            if 'white_list' in locals() and course.course_id not in white_list:
                continue

            json_data = course.json_data(include_sws_url)
            json_rep['courses'].append(json_data)

        return self.json_response(json.dumps(json_rep))