Skip to content

Commit

Permalink
fix: update logic to correct getting accepted members count from a gr…
Browse files Browse the repository at this point in the history
…oup (#2262)

Co-authored-by: Ubuntu <[email protected]>
  • Loading branch information
katrinan029 and Ubuntu authored Oct 8, 2024
1 parent 640e711 commit 4731169
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 51 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Unreleased
----------
* nothing unreleased

[4.27.2]
---------
* fix: updates `get_all_learners` to remove `_get_implicit_group_members`

[4.27.1]
---------
* chore: remove `replaces` sections from squashing migrations.
Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Your project description goes here.
"""

__version__ = "4.27.1"
__version__ = "4.27.2"
4 changes: 2 additions & 2 deletions enterprise/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,8 @@ class Meta:

def get_accepted_members_count(self, obj):
"Returns count for accepted members"
all_members = obj.get_all_learners().filter(status=GROUP_MEMBERSHIP_ACCEPTED_STATUS)
return len(all_members)
accepted_members = obj.get_all_learners().filter(status=GROUP_MEMBERSHIP_ACCEPTED_STATUS)
return len(accepted_members)


class EnterpriseGroupMembershipSerializer(serializers.ModelSerializer):
Expand Down
47 changes: 1 addition & 46 deletions enterprise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4444,48 +4444,6 @@ def _get_filtered_ecu_ids(self, user_query):
ecus = EnterpriseCustomerUser.objects.raw(sql_string, (customer_id, var_q))
return [ecu.id for ecu in ecus]

def _get_implicit_group_members(self, user_query=None, pending_users_only=False):
"""
Fetches all implicit members of a group, indicated by a (pending) enterprise customer user records.
"""
members = []
customer_users = []

# Regardless of user_query, we will need all pecus related to the group's customer
pending_customer_users = PendingEnterpriseCustomerUser.objects.filter(
enterprise_customer=self.enterprise_customer,
)

if user_query:
# Get all ecus relevant to the user query
if not pending_users_only:
customer_users = EnterpriseCustomerUser.objects.filter(
id__in=self._get_filtered_ecu_ids(user_query)
)
# pecu has user_email as a field, so we can filter directly
pending_customer_users = pending_customer_users.filter(user_email__icontains=user_query)
else:
if not pending_users_only:
# No filtering query so get all ecus related to the group's customer
customer_users = EnterpriseCustomerUser.objects.filter(
enterprise_customer=self.enterprise_customer,
active=True,
)
# Build an in memory array of all the implicit memberships
for ent_user in customer_users:
members.append(EnterpriseGroupMembership(
uuid=None,
enterprise_customer_user=ent_user,
group=self,
))
for pending_user in pending_customer_users:
members.append(EnterpriseGroupMembership(
uuid=None,
pending_enterprise_customer_user=pending_user,
group=self,
))
return members

def _get_explicit_group_members(self, user_query=None, fetch_removed=False, pending_users_only=False,):
"""
Fetch explicitly defined members of a group, indicated by an existing membership record
Expand Down Expand Up @@ -4521,10 +4479,7 @@ def get_all_learners(self,
are `memberDetails`, `memberStatus`, and `recentAction`. Ordering can be reversed by supplying a `-` at the
beginning of the sorting value ie `-memberStatus`.
"""
if self.applies_to_all_contexts:
members = self._get_implicit_group_members(user_query, pending_users_only)
else:
members = self._get_explicit_group_members(user_query, fetch_removed, pending_users_only)
members = self._get_explicit_group_members(user_query, fetch_removed, pending_users_only)
if sort_by:
lambda_keys = {
'member_details': lambda t: t.member_email,
Expand Down
3 changes: 1 addition & 2 deletions tests/test_enterprise/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8158,8 +8158,7 @@ def test_list_learners_filtered(self):
) + pending_user_query_string
response = self.client.get(url)

assert response.json().get('count') == 1
assert response.json().get('results')[0].get('pending_enterprise_customer_user_id') == pending_user.id
assert response.json().get('count') == 0

group.applies_to_all_contexts = False
group.save()
Expand Down

0 comments on commit 4731169

Please sign in to comment.