From 12a552b1d539e6098da98c514825234682397c4a Mon Sep 17 00:00:00 2001 From: Kira Miller <31229189+kiram15@users.noreply.github.com> Date: Thu, 21 Nov 2024 13:09:42 -0700 Subject: [PATCH] feat: creating new enterprise group members endpoint (#2270) * fix: adding sorting/filtering to members endpoint * fix: test fixes --- CHANGELOG.rst | 4 + enterprise/__init__.py | 2 +- enterprise/api/v1/serializers.py | 39 ++++++ enterprise/api/v1/urls.py | 6 + .../v1/views/enterprise_customer_members.py | 116 ++++++++++++++++++ tests/test_enterprise/api/test_serializers.py | 70 ++++++++++- 6 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 enterprise/api/v1/views/enterprise_customer_members.py diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 415d6f2f9..a44618d31 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -17,6 +17,10 @@ Unreleased ---------- * nothing unreleased +[4.33.1] +-------- +* feat: Creating enterprise customer members endpoint for admin portal + [4.33.0] -------- * feat: Updated pagination for reporting configurations. diff --git a/enterprise/__init__.py b/enterprise/__init__.py index 87be534dc..5c7d5784a 100644 --- a/enterprise/__init__.py +++ b/enterprise/__init__.py @@ -2,4 +2,4 @@ Your project description goes here. """ -__version__ = "4.33.0" +__version__ = "4.33.1" diff --git a/enterprise/api/v1/serializers.py b/enterprise/api/v1/serializers.py index 11791e076..632206a07 100644 --- a/enterprise/api/v1/serializers.py +++ b/enterprise/api/v1/serializers.py @@ -1909,6 +1909,45 @@ def get_role_assignments(self, obj): return None +class EnterpriseMembersSerializer(serializers.Serializer): + """ + Serializer for EnterpriseCustomerUser model with additions. + """ + class Meta: + model = models.EnterpriseCustomerUser + fields = ( + 'enterprise_customer_user', + 'enrollments', + ) + + enterprise_customer_user = serializers.SerializerMethodField() + enrollments = serializers.SerializerMethodField() + + def get_enrollments(self, obj): + """ + Fetch all of user's enterprise enrollments + """ + if user := obj: + user_id = user[0] + enrollments = models.EnterpriseCourseEnrollment.objects.filter( + enterprise_customer_user=user_id, + ) + return len(enrollments) + return 0 + + def get_enterprise_customer_user(self, obj): + """ + Return either the member's name and email if it's the case that the member is realized, otherwise just email + """ + if user := obj: + return { + "email": user[1], + "joined_org": user[2].strftime("%b %d, %Y"), + "name": user[3], + } + return None + + class DefaultEnterpriseEnrollmentIntentionSerializer(serializers.ModelSerializer): """ Serializer for the DefaultEnterpriseEnrollmentIntention model. diff --git a/enterprise/api/v1/urls.py b/enterprise/api/v1/urls.py index a11a89519..3d276bd93 100644 --- a/enterprise/api/v1/urls.py +++ b/enterprise/api/v1/urls.py @@ -17,6 +17,7 @@ enterprise_customer_branding_configuration, enterprise_customer_catalog, enterprise_customer_invite_key, + enterprise_customer_members, enterprise_customer_reporting, enterprise_customer_sso_configuration, enterprise_customer_support, @@ -211,6 +212,11 @@ ), name='enterprise-customer-support' ), + re_path( + r'^enterprise-customer-members/(?P[A-Za-z0-9-]+)$', + enterprise_customer_members.EnterpriseCustomerMembersViewSet.as_view({'get': 'get_members'}), + name='enterprise-customer-members' + ), ] urlpatterns += router.urls diff --git a/enterprise/api/v1/views/enterprise_customer_members.py b/enterprise/api/v1/views/enterprise_customer_members.py new file mode 100644 index 000000000..90a884ebb --- /dev/null +++ b/enterprise/api/v1/views/enterprise_customer_members.py @@ -0,0 +1,116 @@ +""" +Views for the ``enterprise-customer-members`` API endpoint. +""" + +from collections import OrderedDict + +from rest_framework import permissions, response, status +from rest_framework.pagination import PageNumberPagination + +from django.core.exceptions import ValidationError +from django.db import connection + +from enterprise import models +from enterprise.api.v1 import serializers +from enterprise.api.v1.views.base_views import EnterpriseReadOnlyModelViewSet +from enterprise.logging import getEnterpriseLogger + +LOGGER = getEnterpriseLogger(__name__) + + +class EnterpriseCustomerMembersPaginator(PageNumberPagination): + """Custom paginator for the enterprise customer members.""" + + page_size = 10 + + def get_paginated_response(self, data): + """Return a paginated style `Response` object for the given output data.""" + return response.Response( + OrderedDict( + [ + ("count", self.page.paginator.count), + ("num_pages", self.page.paginator.num_pages), + ("next", self.get_next_link()), + ("previous", self.get_previous_link()), + ("results", data), + ] + ) + ) + + def paginate_queryset(self, queryset, request, view=None): + """ + Paginate a queryset if required, either returning a page object, + or `None` if pagination is not configured for this view. + + """ + if isinstance(queryset, filter): + queryset = list(queryset) + + return super().paginate_queryset(queryset, request, view) + + +class EnterpriseCustomerMembersViewSet(EnterpriseReadOnlyModelViewSet): + """ + API views for the ``enterprise-customer-members`` API endpoint. + """ + queryset = models.EnterpriseCustomerUser.objects.all() + serializer_class = serializers.EnterpriseMembersSerializer + + permission_classes = (permissions.IsAuthenticated,) + paginator = EnterpriseCustomerMembersPaginator() + + def get_members(self, request, *args, **kwargs): + """ + Get all members associated with that enterprise customer + """ + enterprise_uuid = kwargs.get("enterprise_uuid", None) + # Raw sql is picky about uuid format + uuid_no_dashes = str(enterprise_uuid).replace("-", "") + users = [] + user_query = self.request.query_params.get("user_query", None) + + # On logistration, the name field of auth_userprofile is populated, but if it's not + # filled in, we check the auth_user model for it's first/last name fields + # https://2u-internal.atlassian.net/wiki/spaces/ENGAGE/pages/747143186/Use+of+full+name+in+edX#Data-on-Name-Field + query = """ + WITH users AS ( + SELECT + au.id, + au.email, + au.date_joined, + coalesce(NULLIF(aup.name, ''), concat(au.first_name, ' ', au.last_name)) as full_name + FROM enterprise_enterprisecustomeruser ecu + INNER JOIN auth_user as au on ecu.user_id = au.id + LEFT JOIN auth_userprofile as aup on au.id = aup.user_id + WHERE ecu.enterprise_customer_id = %s + ) SELECT * FROM users {user_query_filter} ORDER BY full_name; + """ + try: + with connection.cursor() as cursor: + if user_query: + like_user_query = f"%{user_query}%" + sql_to_execute = query.format( + user_query_filter="WHERE full_name LIKE %s OR email LIKE %s" + ) + cursor.execute( + sql_to_execute, + [uuid_no_dashes, like_user_query, like_user_query], + ) + else: + sql_to_execute = query.format(user_query_filter="") + cursor.execute(sql_to_execute, [uuid_no_dashes]) + users.extend(cursor.fetchall()) + + except ValidationError: + # did not find UUID match in either EnterpriseCustomerUser + return response.Response( + {"detail": "Could not find enterprise uuid {}".format(enterprise_uuid)}, + status=status.HTTP_404_NOT_FOUND, + ) + + # paginate the queryset + users_page = self.paginator.paginate_queryset(users, request, view=self) + + # serialize the paged dataset + serializer = serializers.EnterpriseMembersSerializer(users_page, many=True) + return self.paginator.get_paginated_response(serializer.data) diff --git a/tests/test_enterprise/api/test_serializers.py b/tests/test_enterprise/api/test_serializers.py index bdb214225..8cba7803c 100644 --- a/tests/test_enterprise/api/test_serializers.py +++ b/tests/test_enterprise/api/test_serializers.py @@ -19,6 +19,7 @@ EnterpriseCustomerReportingConfigurationSerializer, EnterpriseCustomerSerializer, EnterpriseCustomerUserReadOnlySerializer, + EnterpriseMembersSerializer, EnterpriseUserSerializer, ImmutableStateSerializer, ) @@ -455,7 +456,7 @@ def setUp(self): super().setUp() - # setup Enteprise Customer + # setup Enterprise Customer self.user_1 = factories.UserFactory() self.user_2 = factories.UserFactory() self.enterprise_customer_user_1 = factories.EnterpriseCustomerUserFactory(user_id=self.user_1.id) @@ -558,3 +559,70 @@ def test_serialize_pending_users(self): serialized_pending_admin_user = serializer.data self.assertEqual(expected_pending_admin_user, serialized_pending_admin_user) + + +class TestEnterpriseMembersSerializer(TestCase): + """ + Tests for EnterpriseMembersSerializer. + """ + def setUp(self): + super().setUp() + + # setup Enterprise Customer + self.user_1 = factories.UserFactory() + self.user_2 = factories.UserFactory() + self.enterprise_customer_user_1 = factories.EnterpriseCustomerUserFactory(user_id=self.user_1.id) + self.enterprise_customer_user_2 = factories.EnterpriseCustomerUserFactory(user_id=self.user_2.id) + self.enterprise_customer_1 = self.enterprise_customer_user_1.enterprise_customer + self.enterprise_customer_2 = self.enterprise_customer_user_2.enterprise_customer + + self.enrollment_1 = factories.EnterpriseCourseEnrollmentFactory( + enterprise_customer_user=self.enterprise_customer_user_1, + ) + self.enrollment_2 = factories.EnterpriseCourseEnrollmentFactory( + enterprise_customer_user=self.enterprise_customer_user_1, + ) + self.enrollment_3 = factories.EnterpriseCourseEnrollmentFactory( + enterprise_customer_user=self.enterprise_customer_user_2, + ) + + def test_serialize_users(self): + expected_user = { + 'enterprise_customer_user': { + 'email': self.user_1.email, + 'joined_org': self.user_1.date_joined.strftime("%b %d, %Y"), + 'name': (self.user_1.first_name + ' ' + self.user_1.last_name), + }, + 'enrollments': 2, + } + + serializer_input_1 = [ + self.user_1.id, + self.user_1.email, + self.user_1.date_joined, + self.user_1.first_name + ' ' + self.user_1.last_name, + ] + serializer = EnterpriseMembersSerializer(serializer_input_1) + serialized_user = serializer.data + + self.assertEqual(serialized_user, expected_user) + + expected_user_2 = { + 'enterprise_customer_user': { + 'email': self.user_2.email, + 'joined_org': self.user_2.date_joined.strftime("%b %d, %Y"), + 'name': self.user_2.first_name + ' ' + self.user_2.last_name, + }, + 'enrollments': 1, + } + + serializer_input_2 = [ + self.user_2.id, + self.user_2.email, + self.user_2.date_joined, + self.user_2.first_name + ' ' + self.user_2.last_name, + ] + + serializer = EnterpriseMembersSerializer(serializer_input_2) + serialized_user = serializer.data + self.assertEqual(serialized_user, expected_user_2)