Example #1
0
 def get_feature_flow(self, request, slug):
     """
     Overrides :obj:`FeatureFlowView` *get_feature_flow* method. Takes the slug,
     decrypts the hashed database id, and either retrieves the corresponding
     timetable or hits a 404.
     """
     timetable_id = hashids.decrypt(slug)[0]
     shared_timetable = get_object_or_404(SharedTimetable,
                                          id=timetable_id,
                                          school=request.subdomain)
     context = {
         "semester": shared_timetable.semester,
         "school": request.subdomain,
         "student": get_student(request),
     }
     return {
         "semester":
         shared_timetable.semester,
         "courses":
         CourseSerializer(shared_timetable.courses,
                          context=context,
                          many=True).data,
         "sharedTimetable":
         DisplayTimetableSerializer.from_model(shared_timetable).data,
     }
Example #2
0
def get_student_dict(school, student, semester):
    """ Return serialized representation of a student. """
    user_dict = {
        'timeAcceptedTos': None,
        'isLoggedIn': False,
        'timetables': [],
        'courses': []
    }
    if student is not None:
        user_dict = dict(user_dict, **StudentSerializer(student).data)
        user_dict['isLoggedIn'] = True

        timetables = student.personaltimetable_set.filter(
            school=school, semester=semester).order_by('-last_updated')
        courses = {
            course
            for timetable in timetables for course in timetable.courses.all()
        }
        context = {'semester': semester, 'school': school, 'student': student}
        user_dict['timetables'] = DisplayTimetableSerializer.from_model(
            timetables, many=True).data
        user_dict['courses'] = CourseSerializer(courses,
                                                context=context,
                                                many=True).data
    return user_dict
Example #3
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)
Example #4
0
 def get(self, request, sem_name, year):
     """Returns student's personal timetables"""
     sem, _ = Semester.objects.get_or_create(name=sem_name, year=year)
     student = Student.objects.get(user=request.user)
     timetables = student.personaltimetable_set.filter(
         school=request.subdomain, semester=sem).order_by("-last_updated")
     courses = {
         course
         for timetable in timetables for course in timetable.courses.all()
     }
     context = {
         "semester": sem,
         "school": request.subdomain,
         "student": student
     }
     return Response(
         {
             "timetables":
             DisplayTimetableSerializer.from_model(timetables,
                                                   many=True).data,
             "courses":
             CourseSerializer(courses, context=context, many=True).data,
         },
         status=status.HTTP_200_OK,
     )
Example #5
0
def get_student_tts(student, school, semester):
    """
    Returns serialized list of a student's :obj:`PersonalTimetable` objects
    ordered by last updated for passing to the frontend.
    """
    timetables = student.personaltimetable_set.filter(
        school=school, semester=semester).order_by('-last_updated')
    return DisplayTimetableSerializer.from_model(timetables, many=True).data
Example #6
0
def get_student_tts(student, school, semester):
    """
    Returns serialized list of a student's :obj:`PersonalTimetable` objects
    ordered by last updated for passing to the frontend.
    """
    timetables = student.personaltimetable_set.filter(
        school=school, semester=semester).order_by('-last_updated')
    return DisplayTimetableSerializer.from_model(timetables, many=True).data
Example #7
0
 def get(self, request, sem_name, year):
     """ Returns student's personal timetables """
     sem, _ = Semester.objects.get_or_create(name=sem_name, year=year)
     student = Student.objects.get(user=request.user)
     timetables = student.personaltimetable_set.filter(
         school=request.subdomain, semester=sem).order_by('-last_updated')
     courses = {course for timetable in timetables for course in timetable.courses.all()}
     context = {'semester': sem, 'school': request.subdomain, 'student': student}
     return Response({
         'timetables': DisplayTimetableSerializer.from_model(timetables, many=True).data,
         'courses': CourseSerializer(courses, context=context, many=True).data
     }, status=status.HTTP_200_OK)
Example #8
0
def get_student_dict(school, student, semester):
    """ Return serialized representation of a student. """
    user_dict = {'timeAcceptedTos': None, 'isLoggedIn': False, 'timetables': [], 'courses': []}
    if student is not None:
        user_dict = dict(user_dict, **StudentSerializer(student).data)
        user_dict['isLoggedIn'] = True

        timetables = student.personaltimetable_set.filter(
            school=school, semester=semester).order_by('-last_updated')
        courses = {course for timetable in timetables for course in timetable.courses.all()}
        context = {'semester': semester, 'school': school, 'student': student}
        user_dict['timetables'] = DisplayTimetableSerializer.from_model(timetables, many=True).data
        user_dict['courses'] = CourseSerializer(courses, context=context, many=True).data
    return user_dict
Example #9
0
 def get_feature_flow(self, request, slug):
     """
     Overrides :obj:`FeatureFlowView` *get_feature_flow* method. Takes the slug,
     decrypts the hashed database id, and either retrieves the corresponding
     timetable or hits a 404.
     """
     timetable_id = hashids.decrypt(slug)[0]
     shared_timetable = get_object_or_404(SharedTimetable,
                                          id=timetable_id,
                                          school=request.subdomain)
     context = {'semester': shared_timetable.semester, 'school': request.subdomain,
                'student': get_student(request)}
     return {
         'semester': shared_timetable.semester,
         'courses': CourseSerializer(shared_timetable.courses, context=context, many=True).data,
         'sharedTimetable': DisplayTimetableSerializer.from_model(shared_timetable).data
     }
Example #10
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)
Example #11
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)
Example #12
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)