Esempio n. 1
0
def get_classmates_in_course(request, school, sem_name, year, course_id):
    """
    Finds all classmates for the authenticated user who also have a
    timetable with the given course.
    """
    school = school.lower()
    sem, _ = Semester.objects.get_or_create(name=sem_name, year=year)
    json_data = {"current": [], "past": []}
    course = Course.objects.get(school=school, id=course_id)
    student = None
    is_logged_in = request.user.is_authenticated
    if is_logged_in and Student.objects.filter(user=request.user).exists():
        student = Student.objects.get(user=request.user)
    if student and student.user.is_authenticated and student.social_courses:
        json_data = get_classmates_from_course_id(school, student, course.id, sem)
    return HttpResponse(json.dumps(json_data), content_type="application/json")
Esempio n. 2
0
 def get_social_friends(self, request, sem_name, year):
     school = request.subdomain
     student = Student.objects.get(user=request.user)
     course_ids = list(
         map(int, request.query_params.getlist("course_ids[]")))
     semester, _ = Semester.objects.get_or_create(name=sem_name, year=year)
     # user opted in to sharing courses
     course_to_classmates = {}
     if student.social_courses:
         friends = student.friends.filter(social_courses=True)
         for course_id in course_ids:
             course_to_classmates[
                 course_id] = get_classmates_from_course_id(school,
                                                            student,
                                                            course_id,
                                                            semester,
                                                            friends=friends)
     return Response(course_to_classmates, status=status.HTTP_200_OK)
Esempio n. 3
0
    def get(self, request, sem_name, year):
        """
        Returns:
            **If the query parameter 'count' is present**
            Information regarding the number of friends only::

                {
                    "id": Course with the most friends,
                    "count": The maximum # of friends in a course,
                    "total_count": the total # in all classes on timetable,
                }

            **If the query parameter course_ids is present** a list of dictionaries representing past classmates and current classmates. These are students who the authenticated user is friends
            with and who has social courses enabled.::

                [{
                    "course_id":6137,
                    "past_classmates":[...],
                    "classmates":[...]
                }, ...]

            **Otherwise** a list of friends and non-friends alike who have social_all enabled to
            be dispalyed in the "find-friends" modal. Sorted by the number courses
            the authenticated user shares.::

                [{
                    "name": "...",
                    "is_friend": Whether or not the user is current user's friend,
                    "profile_url": link to FB profile,
                    "shared_courses": [...],
                    "peer": Info about the user,
                }, ...]
        """
        if request.query_params.get('count'):
            school = request.subdomain
            student = Student.objects.get(user=request.user)
            course_ids = map(int, request.query_params.getlist('course_ids[]'))
            semester, _ = Semester.objects.get_or_create(
                name=sem_name, year=year)
            total_count = 0
            count = 0
            most_friend_course_id = -1
            for course_id in course_ids:
                temp_count = get_friend_count_from_course_id(
                    school, student, course_id, semester)
                if temp_count > count:
                    count = temp_count
                    most_friend_course_id = course_id
                total_count += temp_count
            data = {
                "id": most_friend_course_id,
                "count": count,
                "total_count": total_count}
            return Response(data, status=status.HTTP_200_OK)
        elif request.query_params.getlist('course_ids[]'):
            school = request.subdomain
            student = Student.objects.get(user=request.user)
            course_ids = map(int, request.query_params.getlist('course_ids[]'))
            semester, _ = Semester.objects.get_or_create(
                name=sem_name, year=year)
            # user opted in to sharing courses
            course_to_classmates = {}
            if student.social_courses:
                friends = student.friends.filter(social_courses=True)
                for course_id in course_ids:
                    course_to_classmates[course_id] = \
                        get_classmates_from_course_id(school, student, course_id, semester,
                                                      friends=friends)
            return Response(course_to_classmates, status=status.HTTP_200_OK)
        else:
            school = request.subdomain
            student = Student.objects.get(user=request.user)
            semester, _ = Semester.objects.get_or_create(
                name=sem_name, year=year)
            current_tt = student.personaltimetable_set.filter(school=school,
                                                              semester=semester).order_by(
                'last_updated').last()
            if current_tt is None:
                return Response([], status=status.HTTP_200_OK)
            current_tt_courses = current_tt.courses.all()

            # The most recent TT per student with social enabled that has
            # courses in common with input student
            matching_tts = PersonalTimetable.objects.filter(student__social_all=True,
                                                            courses__id__in=current_tt_courses,
                                                            semester=semester) \
                .exclude(student=student) \
                .order_by('student', 'last_updated') \
                .distinct('student')

            friends = []
            for matching_tt in matching_tts:
                friend = matching_tt.student
                sections_in_common = matching_tt.sections.all() & current_tt.sections.all()
                courses_in_common = matching_tt.courses.all() & current_tt_courses

                shared_courses = []
                for course in courses_in_common:
                    shared_courses.append({
                        'course': model_to_dict(course,
                                                exclude=['unstopped_description', 'description',
                                                         'credits']),
                        # is there a section for this course that is in both timetables?
                        'in_section': (sections_in_common & course.section_set.all()).exists()
                    })

                friends.append({
                    'peer': model_to_dict(friend, exclude=['user', 'id', 'fbook_uid', 'friends']),
                    'is_friend': student.friends.filter(id=friend.id).exists(),
                    'shared_courses': shared_courses,
                    'profile_url': 'https://www.facebook.com/' + friend.fbook_uid,
                    'name': friend.user.first_name + ' ' + friend.user.last_name,
                    'large_img': 'https://graph.facebook.com/' + friend.fbook_uid +
                                 '/picture?width=700&height=700'
                })

            friends.sort(key=lambda friend: len(friend['shared_courses']), reverse=True)
            return Response(friends, status=status.HTTP_200_OK)
Esempio n. 4
0
    def get(self, request, sem_name, year):
        """
        Returns:
            **If the query parameter 'count' is present**
            Information regarding the number of friends only::

                {
                    "id": Course with the most friends,
                    "count": The maximum # of friends in a course,
                    "total_count": the total # in all classes on timetable,
                }

            **If the query parameter course_ids is present** a list of dictionaries representing past classmates and current classmates. These are students who the authenticated user is friends
            with and who has social courses enabled.::

                [{
                    "course_id":6137,
                    "past_classmates":[...],
                    "classmates":[...]
                }, ...]

            **Otherwise** a list of friends and non-friends alike who have social_all enabled to
            be dispalyed in the "find-friends" modal. Sorted by the number courses
            the authenticated user shares.::

                [{
                    "name": "...",
                    "is_friend": Whether or not the user is current user's friend,
                    "profile_url": link to FB profile,
                    "shared_courses": [...],
                    "peer": Info about the user,
                }, ...]
        """
        if request.query_params.get('count'):
            school = request.subdomain
            student = Student.objects.get(user=request.user)
            course_ids = map(int, request.query_params.getlist('course_ids[]'))
            semester, _ = Semester.objects.get_or_create(
                name=sem_name, year=year)
            total_count = 0
            count = 0
            most_friend_course_id = -1
            for course_id in course_ids:
                temp_count = get_friend_count_from_course_id(
                    school, student, course_id, semester)
                if temp_count > count:
                    count = temp_count
                    most_friend_course_id = course_id
                total_count += temp_count
            data = {
                "id": most_friend_course_id,
                "count": count,
                "total_count": total_count}
            return Response(data, status=status.HTTP_200_OK)
        elif request.query_params.getlist('course_ids[]'):
            school = request.subdomain
            student = Student.objects.get(user=request.user)
            course_ids = map(int, request.query_params.getlist('course_ids[]'))
            semester, _ = Semester.objects.get_or_create(
                name=sem_name, year=year)
            # user opted in to sharing courses
            course_to_classmates = {}
            if student.social_courses:
                friends = student.friends.filter(social_courses=True)
                for course_id in course_ids:
                    course_to_classmates[course_id] = \
                        get_classmates_from_course_id(school, student, course_id, semester,
                                                      friends=friends)
            return Response(course_to_classmates, status=status.HTTP_200_OK)
        else:
            school = request.subdomain
            student = Student.objects.get(user=request.user)
            semester, _ = Semester.objects.get_or_create(
                name=sem_name, year=year)
            current_tt = student.personaltimetable_set.filter(school=school,
                                                              semester=semester).order_by(
                'last_updated').last()
            if current_tt is None:
                return Response([], status=status.HTTP_200_OK)
            current_tt_courses = current_tt.courses.all()

            # The most recent TT per student with social enabled that has
            # courses in common with input student
            matching_tts = PersonalTimetable.objects.filter(student__social_all=True,
                                                            courses__id__in=current_tt_courses,
                                                            semester=semester) \
                .exclude(student=student) \
                .order_by('student', 'last_updated') \
                .distinct('student')

            friends = []
            for matching_tt in matching_tts:
                friend = matching_tt.student
                sections_in_common = matching_tt.sections.all() & current_tt.sections.all()
                courses_in_common = matching_tt.courses.all() & current_tt_courses

                shared_courses = []
                for course in courses_in_common:
                    shared_courses.append({
                        'course': model_to_dict(course,
                                                exclude=['unstopped_description', 'description',
                                                         'credits']),
                        # is there a section for this course that is in both timetables?
                        'in_section': (sections_in_common & course.section_set.all()).exists()
                    })

                friends.append({
                    'peer': model_to_dict(friend, exclude=['user', 'id', 'fbook_uid', 'friends']),
                    'is_friend': student.friends.filter(id=friend.id).exists(),
                    'shared_courses': shared_courses,
                    'profile_url': 'https://www.facebook.com/' + friend.fbook_uid,
                    'name': friend.user.first_name + ' ' + friend.user.last_name,
                    'large_img': 'https://graph.facebook.com/' + friend.fbook_uid +
                                 '/picture?width=700&height=700'
                })

            friends.sort(key=lambda friend: len(friend['shared_courses']), reverse=True)
            return Response(friends, status=status.HTTP_200_OK)