Esempio n. 1
0
    def post(self, request):
        """Generate best timetables given the user's selected courses"""
        school = request.subdomain
        params = request.data
        student = get_student(request)
        course_ids = list(params["courseSections"].keys())
        courses = [Course.objects.get(id=cid) for cid in course_ids]
        locked_sections = params["courseSections"]

        self.set_params_semester(params)
        save_analytics_timetable(courses, params["semester"], school,
                                 get_student(request))
        self.update_courses_and_locked_sections(params, course_ids, courses,
                                                locked_sections)
        opt_course_ids = params.get("optionCourses", [])
        max_optional = params.get("numOptionCourses", len(opt_course_ids))
        optional_courses = [
            Course.objects.get(id=cid) for cid in opt_course_ids
        ]
        optional_course_subsets = [
            subset for subset_size in range(max_optional, -1, -1)
            for subset in itertools.combinations(optional_courses, subset_size)
        ]
        custom_events = params.get("customSlots", [])
        preferences = params["preferences"]
        with_conflicts = preferences.get("try_with_conflicts", False)
        sort_metrics = [(m["metric"], m["order"])
                        for m in preferences.get("sort_metrics", [])
                        if m["selected"]]

        timetables = [
            timetable for opt_courses in optional_course_subsets
            for timetable in courses_to_timetables(
                courses + list(opt_courses),
                locked_sections,
                params["semester"],
                sort_metrics,
                params["school"],
                custom_events,
                with_conflicts,
                opt_course_ids,
            )
        ]

        context = self.create_context(request, params, student)
        courses = list(courses + optional_courses)
        response = self.create_response(courses, locked_sections, timetables,
                                        context)
        return Response(response, status=status.HTTP_200_OK)
Esempio n. 2
0
    def post(self, request):
        """Generate best timetables given the user's selected courses"""
        school = request.subdomain
        params = request.data
        student = get_student(request)

        try:
            params['semester'] = Semester.objects.get_or_create(**params['semester'])[0]
        except TypeError: # handle deprecated cached semesters from frontend
            params['semester'] = Semester.objects.get(name="Fall", year="2016") \
                if params['semester'] == "F" \
                else Semester.objects.get(name="Spring", year="2017")

        course_ids = params['courseSections'].keys()
        courses = [Course.objects.get(id=cid) for cid in course_ids]
        locked_sections = params['courseSections']

        save_analytics_timetable(courses, params['semester'], school, get_student(request))

        for updated_course in params.get('updated_courses', []):
            cid = str(updated_course['course_id'])
            locked_sections[cid] = locked_sections.get(cid, {})
            if cid not in course_ids:
                courses.append(Course.objects.get(id=int(cid)))

            for locked_section in filter(bool, updated_course['section_codes']):
                update_locked_sections(locked_sections, cid, locked_section, params['semester'])

        # temp optional course implementation
        opt_course_ids = params.get('optionCourses', [])
        max_optional = params.get('numOptionCourses', len(opt_course_ids))
        optional_courses = [Course.objects.get(id=cid) for cid in opt_course_ids]
        optional_course_subsets = [subset for subset_size in range(max_optional, -1, -1)
                                   for subset in itertools.combinations(optional_courses,
                                                                        subset_size)]

        custom_events = params.get('customSlots', [])
        preferences = params['preferences']
        with_conflicts = preferences.get('try_with_conflicts', False)
        sort_metrics = [(m['metric'], m['order']) for m in preferences.get('sort_metrics', [])
                        if m['selected']]

        # TODO move sorting to view level so that result is sorted
        timetables = [timetable for opt_courses in optional_course_subsets
                                for timetable in courses_to_timetables(courses + list(opt_courses),
                                                                       locked_sections,
                                                                       params['semester'],
                                                                       sort_metrics,
                                                                       params['school'],
                                                                       custom_events,
                                                                       with_conflicts,
                                                                       opt_course_ids)]

        context = {'semester': params['semester'], 'school': request.subdomain, 'student': student}
        courses = [course for course in courses + optional_courses]
        response = {
            'timetables': DisplayTimetableSerializer(timetables, many=True).data,
            'new_c_to_s': locked_sections,
            'courses': CourseSerializer(courses, context=context, many=True).data
        }
        return Response(response, status=status.HTTP_200_OK)