Skip to content

Commit

Permalink
Merge pull request openedx#507 from Stanford-Online/dcadams/fix_enrol…
Browse files Browse the repository at this point in the history
…lment_api_performance

Performance fix for enrollment roster api.
  • Loading branch information
David Adams authored Aug 30, 2016
2 parents 8e4695a + a44232f commit 2e80acc
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions common/djangoapps/enrollment/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
)
from enrollment.serializers import CourseEnrollmentSerializer, CourseSerializer
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from student.auth import user_has_role
from student.models import (
CourseEnrollment, NonExistentCourseError, EnrollmentClosedError,
CourseFullError, AlreadyEnrolledError, CourseEnrollmentAttribute
)
from student.models import UserProfile
from student.models import anonymous_id_for_user
from student.roles import CourseStaffRole

from student.models import CourseAccessRole

log = logging.getLogger(__name__)
NON_STUDENT_ROLES = ['instructor', 'staff']


def get_course_enrollments(user_id):
Expand Down Expand Up @@ -298,27 +295,51 @@ def get_course_enrollment_info(course_id, include_expired=False):
else:
return CourseSerializer(course, include_expired=include_expired).data


def get_roster(course_id):
"""
Returns roster with PII of all enrollees in course
"""
course_key = CourseKey.from_string(course_id)
enrollments = CourseEnrollment.objects.filter(

# Select appropriate enrollment, user and profile fields for enrolled students.
enrollments = CourseEnrollment.objects.select_related(
'user'
).select_related(
'user__profile'
).filter(
course_id=course_key,
is_active=True,
).order_by('user__username')

roster = [
{
'anonymous_user_id': anonymous_id_for_user(enrollment.user, None),
'user_id': enrollment.user.id,
'username': enrollment.user.username,
'email': enrollment.user.email,
'mode': enrollment.mode,
'is_staff': user_has_role(enrollment.user, CourseStaffRole(course_key)),
'name': getattr(UserProfile.objects.get(user=enrollment.user), 'name', ''),
}
for enrollment in enrollments
]
).values(
'user__id',
'user__username',
'user__email',
'mode',
'user__profile__name',
).order_by(
'user__username'
)

# Find all user_ids with instructor or staff roles in course.
non_students = CourseAccessRole.objects.filter(
course_id=course_key,
role__in=NON_STUDENT_ROLES,
).values('user_id').distinct()

roster = []
for enrollment in enrollments:
is_staff = 0
for non_student in non_students:
if non_student.values()[0] == enrollment['user__id']:
is_staff = 1
break

roster.append({
'user_id': enrollment['user__id'],
'username': enrollment['user__username'],
'email': enrollment['user__email'],
'mode': enrollment['mode'],
'is_staff': is_staff,
'name': enrollment['user__profile__name'],
})
return roster

0 comments on commit 2e80acc

Please sign in to comment.