예제 #1
0
    def create_or_update_timetable(self, request):
        school = request.subdomain
        has_conflict = request.data["has_conflict"]
        name = request.data["name"]
        semester, _ = Semester.objects.get_or_create(
            **request.data["semester"])
        student = Student.objects.get(user=request.user)
        params = {
            "school": school,
            "name": name,
            "semester": semester,
            "student": student,
        }
        tt_id = request.data.get("id")  # id is None if this is a new timetable
        if PersonalTimetable.objects.filter(~Q(id=tt_id), **params):
            return Response(status=status.HTTP_409_CONFLICT)

        personal_timetable = (PersonalTimetable.objects.create(
            **params) if tt_id is None else PersonalTimetable.objects.get(
                id=tt_id))
        slots = request.data["slots"]
        self.update_tt(personal_timetable, name, has_conflict, slots)
        self.update_events(personal_timetable, request.data["events"])

        response = {
            "timetables":
            get_student_tts(student, school, semester),
            "saved_timetable":
            DisplayTimetableSerializer.from_model(personal_timetable).data,
        }
        response_status = (status.HTTP_201_CREATED
                           if tt_id is None else status.HTTP_200_OK)
        return Response(response, status=response_status)
예제 #2
0
    def delete(self, request, sem_name, year, tt_name):
        """ Deletes a PersonalTimetable by name/year/term."""
        school = request.subdomain
        name = tt_name
        semester = Semester.objects.get(name=sem_name, year=year)
        student = Student.objects.get(user=request.user)

        to_delete = PersonalTimetable.objects.filter(
            student=student, name=name, school=school, semester=semester)
        for tt in to_delete:
            tt.events.all().delete()
        to_delete.delete()

        # TODO: should respond with deleted object
        return Response({'timetables': get_student_tts(student,school, semester)},
                        status=status.HTTP_200_OK)
예제 #3
0
    def delete(self, request, sem_name, year, tt_name):
        """ Deletes a PersonalTimetable by name/year/term."""
        school = request.subdomain
        name = tt_name
        semester = Semester.objects.get(name=sem_name, year=year)
        student = Student.objects.get(user=request.user)

        to_delete = PersonalTimetable.objects.filter(
            student=student, name=name, school=school, semester=semester)
        for tt in to_delete:
            tt.events.all().delete()
        to_delete.delete()

        # TODO: should respond with deleted object
        return Response({'timetables': get_student_tts(student,school, semester)},
                        status=status.HTTP_200_OK)
예제 #4
0
 def duplicate_timetable(self, request):
     school = request.subdomain
     name = request.data["source"]
     semester = Semester.objects.get(**request.data["semester"])
     student = Student.objects.get(user=request.user)
     new_name = request.data["name"]
     duplicate = get_object_or_404(
         PersonalTimetable,
         student=student,
         name=name,
         school=school,
         semester=semester,
     )
     courses, sections, events = self.get_duplicate_m2m_fields(duplicate)
     self.create_duplicated_timetable(new_name, duplicate, courses,
                                      sections, events)
     response = {
         "timetables":
         get_student_tts(student, school, semester),
         "saved_timetable":
         DisplayTimetableSerializer.from_model(duplicate).data,
     }
     return Response(response, status=status.HTTP_201_CREATED)
예제 #5
0
    def post(self, request):
        """
        Duplicates a personal timetable if a 'source' is provided. Else, creates
        a personal timetable based on the courses, custom events, preferences, etc.
        which are provided.
        """
        if 'source' in request.data:  # duplicate existing timetable
            school = request.subdomain
            name = request.data['source']
            semester = Semester.objects.get(**request.data['semester'])
            student = Student.objects.get(user=request.user)
            new_name = request.data['name']

            duplicate = get_object_or_404(PersonalTimetable, student=student, name=name,
                                          school=school, semester=semester)
            # save manytomany relationships before copying
            courses, sections = duplicate.courses.all(), duplicate.sections.all()
            events = duplicate.events.all()
            for event in events:  # create duplicates of each event to allow for safe delete
                event.pk = None
                event.save()

            duplicate.pk = None  # creates duplicate of object
            duplicate.name = new_name
            duplicate.save()
            duplicate.courses = courses
            duplicate.sections = sections
            duplicate.events = events

            response = {
                'timetables': get_student_tts(student, school, semester),
                'saved_timetable': DisplayTimetableSerializer.from_model(duplicate).data
            }
            return Response(response, status=status.HTTP_201_CREATED)
        else:
            school = request.subdomain
            has_conflict = request.data['has_conflict']
            name = request.data['name']
            semester, _ = Semester.objects.get_or_create(**request.data['semester'])
            student = Student.objects.get(user=request.user)
            params = {
                'school': school,
                'name': name,
                'semester': semester,
                'student': student
            }

            slots = request.data['slots']
            # id is None if this is a new timetable
            tt_id = request.data.get('id')

            if PersonalTimetable.objects.filter(~Q(id=tt_id), **params):
                return Response(status=status.HTTP_409_CONFLICT)

            personal_timetable = PersonalTimetable.objects.create(**params) if tt_id is None else \
                PersonalTimetable.objects.get(id=tt_id)
            self.update_tt(personal_timetable, name, has_conflict, slots)
            self.update_events(personal_timetable, request.data['events'])

            response = {
                'timetables': get_student_tts(student, school, semester),
                'saved_timetable': DisplayTimetableSerializer.from_model(personal_timetable).data
            }
            response_status = status.HTTP_201_CREATED if tt_id is None else status.HTTP_200_OK
            return Response(response, status=response_status)
예제 #6
0
    def post(self, request):
        """
        Duplicates a personal timetable if a 'source' is provided. Else, creates
        a personal timetable based on the courses, custom events, preferences, etc.
        which are provided.
        """
        if 'source' in request.data:  # duplicate existing timetable
            school = request.subdomain
            name = request.data['source']
            semester = Semester.objects.get(**request.data['semester'])
            student = Student.objects.get(user=request.user)
            new_name = request.data['name']

            duplicate = get_object_or_404(PersonalTimetable, student=student, name=name,
                                          school=school, semester=semester)
            # save manytomany relationships before copying
            courses, sections = duplicate.courses.all(), duplicate.sections.all()
            events = duplicate.events.all()
            for event in events:  # create duplicates of each event to allow for safe delete
                event.pk = None
                event.save()

            duplicate.pk = None  # creates duplicate of object
            duplicate.name = new_name
            duplicate.save()
            duplicate.courses = courses
            duplicate.sections = sections
            duplicate.events = events

            response = {
                'timetables': get_student_tts(student, school, semester),
                'saved_timetable': DisplayTimetableSerializer.from_model(duplicate).data
            }
            return Response(response, status=status.HTTP_201_CREATED)
        else:
            school = request.subdomain
            has_conflict = request.data['has_conflict']
            name = request.data['name']
            semester, _ = Semester.objects.get_or_create(**request.data['semester'])
            student = Student.objects.get(user=request.user)
            params = {
                'school': school,
                'name': name,
                'semester': semester,
                'student': student
            }

            slots = request.data['slots']
            # id is None if this is a new timetable
            tt_id = request.data.get('id')

            if PersonalTimetable.objects.filter(~Q(id=tt_id), **params):
                return Response(status=status.HTTP_409_CONFLICT)

            personal_timetable = PersonalTimetable.objects.create(**params) if tt_id is None else \
                PersonalTimetable.objects.get(id=tt_id)
            self.update_tt(personal_timetable, name, has_conflict, slots)
            self.update_events(personal_timetable, request.data['events'])

            response = {
                'timetables': get_student_tts(student, school, semester),
                'saved_timetable': DisplayTimetableSerializer.from_model(personal_timetable).data
            }
            response_status = status.HTTP_201_CREATED if tt_id is None else status.HTTP_200_OK
            return Response(response, status=response_status)