示例#1
0
    def GET(self, request, **kwargs):
        try:
            if 'gmail_id' in request.GET:
                gmail_id = request.GET.get('gmail_id', '').strip()
                person = get_person_by_gmail_id(gmail_id)
                return self.response_for_google_person(person)
            elif 'net_id' in request.GET:
                net_id = netid_from_request(request.GET)
                person = get_person_by_netid(net_id)
                return self.response_for_person(person)
            elif 'reg_id' in request.GET:
                reg_id = regid_from_request(request.GET)
                person = get_person_by_regid(reg_id)
                return self.response_for_person(person)
            else:
                return self.json_response('{"error":"Unrecognized user ID"}',
                                          status=400)

        except DataFailureException as err:
            data = json.loads(err.msg)
            return self.json_response('{"error":"%s %s"}' % (
                err.status, data["StatusDescription"]), status=400)

        except Exception as err:
            return self.json_response('{"error":"%s"}' % err, status=400)
    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))