Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #34 from open-craft/agrendalath/MCKIN-8417-optimiz…
Browse files Browse the repository at this point in the history
…e-OrganizationsViewSet.users-API

[MCKIN-8417] WIP: Optimize OrganizationsViewSet.users API
  • Loading branch information
UmanShahzad authored Oct 9, 2018
2 parents 3d892ca + e4d97a0 commit 6010f2e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions edx_solutions_organizations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.conf import settings
from django.contrib.auth.models import User, Group
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Sum, F, Count
from django.db.models import Sum, F, Count, Prefetch
from django.db import IntegrityError
from django.utils.translation import ugettext as _
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
Expand Down Expand Up @@ -146,8 +146,11 @@ def users(self, request, pk):
if course_key:
users = users.filter(courseenrollment__course_id__exact=course_key,
courseenrollment__is_active=True)
if str2bool(include_grades):
users = users.prefetch_related('studentgradebook_set')

if str2bool(include_grades):
users = users.prefetch_related(
Prefetch('studentgradebook_set', queryset=StudentGradebook.objects.filter(course_id=course_key))
)

if str2bool(include_course_counts):
enrollments = CourseEnrollment.objects.filter(user__in=users).values('user').order_by().annotate(total=Count('user'))
Expand All @@ -171,7 +174,7 @@ def users(self, request, pk):

if str2bool(include_grades) and course_key:
user_grades = {'grade': 0, 'proforma_grade': 0, 'complete_status': False}
gradebook = user.studentgradebook_set.filter(course_id=course_key)
gradebook = user.studentgradebook_set.all()
if gradebook:
user_grades['grade'] = gradebook[0].grade
user_grades['proforma_grade'] = gradebook[0].proforma_grade
Expand All @@ -195,9 +198,8 @@ def users(self, request, pk):
organization = self.get_object()
users_to_be_deleted = organization.users.filter(id__in=user_ids)
total_users = len(users_to_be_deleted)
for user in users_to_be_deleted:
organization.users.remove(user)
if total_users > 0:
organization.users.remove(*users_to_be_deleted)
return Response({
"detail": _("{users_removed} user(s) removed from organization").format(users_removed=total_users)
}, status=status.HTTP_200_OK)
Expand Down

0 comments on commit 6010f2e

Please sign in to comment.