Skip to content

Commit

Permalink
fix: final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kiram15 committed Oct 29, 2024
1 parent 44d6a9a commit 9061fe9
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 36 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.29.1]
--------
* feat: Creating enterprise customer members endpoint for admin portal

[4.29.0]
--------
* feat: Create django admin for default enrollments
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.29.0"
__version__ = "4.29.1"
11 changes: 4 additions & 7 deletions enterprise/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,7 @@ def get_role_assignments(self, obj):
else:
return None


class EnterpriseMembersSerializer(serializers.Serializer):
"""
Serializer for EnterpriseCustomerUser model with additions.
Expand All @@ -1903,24 +1904,20 @@ class Meta:
model = models.EnterpriseCustomerUser
fields = (
'enterprise_customer_user',
'user_email',
'enrollments',
'created',
)
enterprise_customer_user = UserSerializer(source="user", required=False, default=None)
user_email = serializers.EmailField()
enrollments = serializers.SerializerMethodField()

enterprise_customer_user = UserSerializer(source="user", required=False, default=None)
enrollments = serializers.SerializerMethodField()

def get_enrollments(self, obj):
"""
Fetch all of user's enterprise enrollments
"""
import pdb;
pdb.set_trace()
if hasattr(obj, 'user_id'):
user_id = obj.user_id
enrollments = models.EnterpriseCourseEnrollment.objects.filter(
enterprise_customer_user=user_id,
)
return len(enrollments)
return 0
7 changes: 4 additions & 3 deletions enterprise/api/v1/views/enterprise_customer_members.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from collections import OrderedDict

from rest_framework import filters, permissions, response, status
from rest_framework import permissions, response, status
from rest_framework.pagination import PageNumberPagination

from django.contrib import auth
Expand All @@ -20,6 +20,7 @@

LOGGER = getEnterpriseLogger(__name__)


class EnterpriseCustomerMembersPaginator(PageNumberPagination):
"""Custom paginator for the enterprise customer members."""

Expand Down Expand Up @@ -77,7 +78,7 @@ def filter_queryset_by_user_query(self, queryset):

def get_members(self, request, *args, **kwargs):
"""
Filter down the queryset of groups available to the requesting uuid.
Get all members associated with that enterprise customer
"""
enterprise_uuid = kwargs.get("enterprise_uuid", None)
users = []
Expand All @@ -102,5 +103,5 @@ def get_members(self, request, *args, **kwargs):
users_page = self.paginator.paginate_queryset(users, request, view=self)

# serialize the paged dataset
serializer = serializers.EnterpriseMembersSerializer(users_page, many=True)
serializer = serializers.EnterpriseMembersSerializer(users_page, many=True)
return self.paginator.get_paginated_response(serializer.data)
57 changes: 40 additions & 17 deletions tests/test_enterprise/api/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,12 @@ def test_serialize_pending_users(self):

self.assertEqual(expected_pending_admin_user, serialized_pending_admin_user)

@mark.django_db

class TestEnterpriseMembersSerializer(TestCase):
"""
Tests for EnterpriseMembersSerializer.
"""
def setUp(self):
"""
Perform operations common for all tests.
"""
super().setUp()

# setup Enterprise Customer
Expand All @@ -574,27 +574,50 @@ def setUp(self):
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.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_1 = factories.EnterpriseCourseEnrollmentFactory(
self.enrollment_2 = factories.EnterpriseCourseEnrollmentFactory(
enterprise_customer_user=self.enterprise_customer_user_1,
)
self.enrollment_1 = factories.EnterpriseCourseEnrollmentFactory(
self.enrollment_3 = factories.EnterpriseCourseEnrollmentFactory(
enterprise_customer_user=self.enterprise_customer_user_2,
)

def test_serialize_users(self):
for customer_user in [
(self.enterprise_customer_user_1),
(self.enterprise_customer_user_2),
]:
user = customer_user.user
serializer = EnterpriseMembersSerializer(customer_user)
print("serializer ", serializer)
print("data ", serializer.data)
serialized_user = serializer.data
expected_user = {
'enterprise_customer_user': OrderedDict([
('id', self.user_1.id),
('username', self.user_1.username),
('first_name', self.user_1.first_name),
('last_name', self.user_1.last_name),
('email', self.user_1.email),
('is_staff', self.user_1.is_staff),
('is_active', self.user_1.is_active),
('date_joined', self.user_1.date_joined.strftime("%Y-%m-%dT%H:%M:%SZ"))
]),
'enrollments': 2,
}
serializer = EnterpriseMembersSerializer(self.enterprise_customer_user_1)
serialized_user = serializer.data
self.assertEqual(serialized_user, expected_user)

expected_user_2 = {
'enterprise_customer_user': OrderedDict([
('id', self.user_2.id),
('username', self.user_2.username),
('first_name', self.user_2.first_name),
('last_name', self.user_2.last_name),
('email', self.user_2.email),
('is_staff', self.user_2.is_staff),
('is_active', self.user_2.is_active),
('date_joined', self.user_2.date_joined.strftime("%Y-%m-%dT%H:%M:%SZ"))
]),
'enrollments': 1,
}

self.assertEqual('', serialized_user)
serializer = EnterpriseMembersSerializer(self.enterprise_customer_user_2)
serialized_user = serializer.data
self.assertEqual(serialized_user, expected_user_2)
11 changes: 3 additions & 8 deletions tests/test_enterprise/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9755,23 +9755,18 @@ def test_get_enterprise_org_members(self):
"""
user = factories.UserFactory()
enterprise_customer = factories.EnterpriseCustomerFactory(uuid=FAKE_UUIDS[0])
user = factories.EnterpriseCustomerUserFactory(
factories.EnterpriseCustomerUserFactory(
user_id=user.id,
enterprise_customer=enterprise_customer
)
enrollment = factories.EnterpriseCourseEnrollment(
enterprise_customer_user=user,
)

# Test invalid UUID
url = reverse(self.ECM_ENDPOINT, kwargs={self.ECM_KWARG: 123})
response = self.client.get(settings.TEST_SERVER + url)
self.assertEqual(response.status_code, 404)

expected_json = {'this-is-going-to-fail': False}
# Test valid UUID
url = reverse(self.ECM_ENDPOINT, kwargs={self.ECM_KWARG: enterprise_customer.uuid})
response = self.client.get(settings.TEST_SERVER + url)

print('response content ', response.content)
assert expected_json == response.json().get('results')[0]
data = response.json().get('results')[0]
assert data['enrollments'] == 0

0 comments on commit 9061fe9

Please sign in to comment.