Ejemplo n.º 1
0
def add_to_schedule(request, id):
    try:
        schedule = Schedule.objects.get(id=id)
        if schedule.user == request.user:
            data = [int(crn) for crn in dict(request.data)['courses']]
            courses = [Course.objects.get(crn=crn) for crn in data]

            for course in courses:
                if course not in schedule.courses.all():
                    schedule.courses.add(course)
                    if not course.is_full():
                        available, _course = is_available(
                            schedule.courses.all(), course)

                        if not available:
                            messages.warning(
                                request._request,
                                "Course #{} overlaps #{}. Your schedule is created anyway but please mind this."
                                .format(course.crn, _course.crn, course.crn))
                    else:
                        msg = "Course {} is full, your schedule is created anyway but please mind this.".format(
                            course.crn)

                        notification = Notification()
                        notification.user = request.user
                        notification.msg = msg
                        notification.save()

            return Response({'success': 'attempt is accomplished'})
        else:
            raise Exception("Unauthorized attempt")
    except Exception as error:
        return Response({'error': error})
Ejemplo n.º 2
0
    def retrieve(self, request, *args, **kwargs):
        if request.user == self.get_object().user:
            instance = self.get_object()
            serializer = self.get_serializer(instance).data

            serializer['courses'] = CourseSerializer(instance.courses.all(),
                                                     many=True).data
            for data in serializer['courses']:
                query = Course.objects.get(crn=data['crn'])

                available, _ = is_available(instance.courses.all(), query)

                if not available:
                    data['overlaps'] = True
                else:
                    data['overlaps'] = False

                data['lectures'] = LectureSerializer(query.lecture_set.all(),
                                                     many=True).data
                data['prerequisites'] = PrerequisiteSerializer(
                    query.prerequisites.all(), many=True).data

            return Response(serializer)

        return Response({'Unauthorized attempt'}, status=HTTP_401_UNAUTHORIZED)
Ejemplo n.º 3
0
    def post(self, request, format=None):
        crn = request.data['crn']
        instance = get_object_or_404(Course, crn=crn)
        serializer = CourseSerializer(instance).data
        schedule = request.user.schedule_set.latest()
        available, _ = is_available(schedule.courses.all(), instance)

        if not available:
            serializer['overlaps'] = True

        serializer['lectures'] = LectureSerializer(instance.lecture_set.all(),
                                                   many=True).data
        serializer['prerequisites'] = PrerequisiteSerializer(
            instance.prerequisites.all(), many=True).data

        return Response(serializer)