Esempio n. 1
0
    def get(self, request, user_id):
        MAX_LIMIT = 50
        DEFAULT_ORDER = ['year', 'semester', 'arrange_order', 'id']
        PARAMS_STRUCTURE = [
            ("year", ParseType.INT, False, []),
            ("semester", ParseType.INT, False, []),
            ORDER_DEFAULT_CONFIG,
            OFFSET_DEFAULT_CONFIG,
            LIMIT_DEFAULT_CONFIG,
        ]

        year, semester, order, offset, limit = parse_params(
            request.GET, PARAMS_STRUCTURE)

        userprofile = request.user.userprofile
        if userprofile.id != int(user_id):
            return HttpResponse(status=401)

        timetables = userprofile.timetables.all()

        if year is not None:
            timetables = timetables.filter(year=year)
        if year is not None:
            timetables = timetables.filter(semester=semester)

        timetables = apply_order(timetables, order, DEFAULT_ORDER)
        timetables = apply_offset_and_limit(timetables, offset, limit,
                                            MAX_LIMIT)
        result = [t.to_json() for t in timetables]
        return JsonResponse(result, safe=False)
Esempio n. 2
0
    def get(self, request):
        MAX_LIMIT = 50
        DEFAULT_ORDER = ['-written_datetime', '-id']
        PARAMS_STRUCTURE = [
            ("lecture_year", ParseType.INT, False, []),
            ("lecture_semester", ParseType.INT, False, []),
            ("response_type", ParseType.STR, False, []),
            ORDER_DEFAULT_CONFIG,
            OFFSET_DEFAULT_CONFIG,
            LIMIT_DEFAULT_CONFIG,
        ]

        lecture_year, lecture_semester, response_type, order, offset, limit = \
            parse_params(request.GET, PARAMS_STRUCTURE)

        reviews = Review.objects.all()

        lecture_query = Q()
        if lecture_year is not None:
            lecture_query &= Q(lecture__year=lecture_year)
        if lecture_semester is not None:
            lecture_query &= Q(lecture__semester=lecture_semester)
        reviews = reviews.filter(lecture_query)

        reviews = reviews \
            .distinct()

        if response_type == "count":
            return JsonResponse(reviews.count(), safe=False)

        reviews = apply_order(reviews, order, DEFAULT_ORDER)
        reviews = apply_offset_and_limit(reviews, offset, limit, MAX_LIMIT)
        result = [r.to_json(user=request.user) for r in reviews]
        return JsonResponse(result, safe=False)
Esempio n. 3
0
    def get(self, request):
        DEFAULT_ORDER = ['year', 'semester']
        PARAMS_STRUCTURE = [
            ORDER_DEFAULT_CONFIG,
        ]

        order, = parse_params(request.GET, PARAMS_STRUCTURE)

        semesters = Semester.objects.all()

        semesters = apply_order(semesters, order, DEFAULT_ORDER)
        result = [semester.to_json() for semester in semesters]
        return JsonResponse(result, safe=False)
Esempio n. 4
0
    def get(self, request, course_id):
        DEFAULT_ORDER = ['year', 'semester', 'class_no']
        PARAMS_STRUCTURE = [
            ORDER_DEFAULT_CONFIG,
        ]

        order, = parse_params(request.GET, PARAMS_STRUCTURE)

        course = get_object_or_404(Course, id=course_id)
        lectures = course.lectures.filter(deleted=False)

        lectures = apply_order(lectures, order, DEFAULT_ORDER)
        result = [lecture.to_json() for lecture in lectures]
        return JsonResponse(result, safe=False)
Esempio n. 5
0
    def get(self, request):
        DEFAULT_ORDER = ['start_time', 'id']
        PARAMS_STRUCTURE = [
            ("time", ParseType.STR, False, []),
            ORDER_DEFAULT_CONFIG,
        ]

        time, order = parse_params(request.GET, PARAMS_STRUCTURE)

        notices = Notice.objects.all()

        if time:
            notices = notices.filter(start_time__lte=time, end_time__gte=time)

        notices = apply_order(notices, order, DEFAULT_ORDER)
        result = [n.to_json() for n in notices]
        return JsonResponse(result, safe=False)
Esempio n. 6
0
    def get(self, request, user_id):
        DEFAULT_ORDER = ['old_code']
        PARAMS_STRUCTURE = [
            ORDER_DEFAULT_CONFIG,
        ]

        order, = parse_params(request.GET, PARAMS_STRUCTURE)

        userprofile = request.user.userprofile
        if userprofile.id != int(user_id):
            return HttpResponse(status=401)
        courses = Course.objects.filter(
            lectures__in=userprofile.taken_lectures.all())

        courses = apply_order(courses, order, DEFAULT_ORDER)
        result = [course.to_json(user=request.user) for course in courses]
        return JsonResponse(result, safe=False)
Esempio n. 7
0
    def get(self, request, lecture_id):
        MAX_LIMIT = 100
        DEFAULT_ORDER = ['-written_datetime', '-id']
        PARAMS_STRUCTURE = [
            ORDER_DEFAULT_CONFIG,
            OFFSET_DEFAULT_CONFIG,
            LIMIT_DEFAULT_CONFIG,
        ]

        order, offset, limit = parse_params(request.GET, PARAMS_STRUCTURE)

        lecture = get_object_or_404(Lecture, id=lecture_id)
        reviews = lecture.reviews.all()

        reviews = apply_order(reviews, order, DEFAULT_ORDER)
        reviews = apply_offset_and_limit(reviews, offset, limit, MAX_LIMIT)
        result = [review.to_json() for review in reviews]
        return JsonResponse(result, safe=False)
Esempio n. 8
0
    def get(self, request):
        MAX_LIMIT = 300
        DEFAULT_ORDER = ['year', 'semester', 'old_code', 'class_no']
        PARAMS_STRUCTURE = [
            ("year", ParseType.INT, False, []),
            ("semester", ParseType.INT, False, []),
            ("day", ParseType.INT, False, []),
            ("begin", ParseType.INT, False, []),
            ("end", ParseType.INT, False, []),
            ("department", ParseType.LIST_STR, False, []),
            ("type", ParseType.LIST_STR, False, []),
            ("level", ParseType.LIST_STR, False, []),
            ("group", ParseType.LIST_STR, False, []),
            ("keyword", ParseType.STR, False, []),
            ORDER_DEFAULT_CONFIG,
            OFFSET_DEFAULT_CONFIG,
            LIMIT_DEFAULT_CONFIG,
        ]

        year, semester, day, begin, end, \
        department, type_, level, group, keyword, \
        order, offset, limit = \
            parse_params(request.GET, PARAMS_STRUCTURE)

        lectures = Lecture.objects.filter(deleted=False).exclude(
            Lecture.get_query_for_research())

        lectures = services.filter_by_semester(lectures, year, semester)
        lectures = services.filter_by_time(lectures, day, begin, end)
        lectures = services.filter_by_department(lectures, department)
        lectures = services.filter_by_type(lectures, type_)
        lectures = services.filter_by_level(lectures, level)
        lectures = services.filter_by_group(lectures, group)
        lectures = services.filter_by_keyword(lectures, keyword)

        # lectures = lectures
        # .select_related('course', 'department') \
        # .prefetch_related('classtimes', 'examtimes', 'professors') \

        lectures = apply_order(lectures, order, DEFAULT_ORDER)
        lectures = apply_offset_and_limit(lectures, offset, limit, MAX_LIMIT)
        result = [lecture.to_json(nested=False) for lecture in lectures]
        return JsonResponse(result, safe=False)
Esempio n. 9
0
    def get(self, request, user_id):
        MAX_LIMIT = 300
        DEFAULT_ORDER = ['-written_datetime', '-id']
        PARAMS_STRUCTURE = [
            ORDER_DEFAULT_CONFIG,
            OFFSET_DEFAULT_CONFIG,
            LIMIT_DEFAULT_CONFIG,
        ]

        order, offset, limit = parse_params(request.GET, PARAMS_STRUCTURE)

        profile = request.user.userprofile
        if profile.id != int(user_id):
            return HttpResponse(status=401)
        reviews = Review.objects.filter(votes__userprofile=profile)

        reviews = apply_order(reviews, order, DEFAULT_ORDER)
        reviews = apply_offset_and_limit(reviews, offset, limit, MAX_LIMIT)
        result = [r.to_json(user=request.user) for r in reviews]
        return JsonResponse(result, safe=False)
Esempio n. 10
0
    def get(self, request):
        MAX_LIMIT = 150
        DEFAULT_ORDER = ['old_code']
        PARAMS_STRUCTURE = [
            ("department", ParseType.LIST_STR, False, []),
            ("type", ParseType.LIST_STR, False, []),
            ("level", ParseType.LIST_STR, False, []),
            ("group", ParseType.LIST_STR, False, []),
            ("keyword", ParseType.STR, False, []),
            ("term", ParseType.STR, False, []),
            ORDER_DEFAULT_CONFIG,
            OFFSET_DEFAULT_CONFIG,
            LIMIT_DEFAULT_CONFIG,
        ]

        department, type_, level, group, keyword, \
        term, order, offset, limit = \
            parse_params(request.GET, PARAMS_STRUCTURE)

        courses = Course.objects.all()

        courses = services.filter_by_department(courses, department)
        courses = services.filter_by_type(courses, type_)
        courses = services.filter_by_level(courses, level)
        courses = services.filter_by_group(courses, group)
        courses = services.filter_by_keyword(courses, keyword)
        courses = services.filter_by_term(courses, term)

        courses = courses.distinct()
        # .select_related('department') \
        # .prefetch_related('related_courses_prior',
        #                   'related_courses_posterior',
        #                   'professors',
        #                   'read_users_courseuser')

        courses = apply_order(courses, order, DEFAULT_ORDER)
        courses = apply_offset_and_limit(courses, offset, limit, MAX_LIMIT)
        result = [c.to_json(user=request.user) for c in courses]
        return JsonResponse(result, safe=False)