-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: creating new enterprise group members endpoint (#2270)
* fix: adding sorting/filtering to members endpoint * fix: test fixes
- Loading branch information
Showing
6 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
Your project description goes here. | ||
""" | ||
|
||
__version__ = "4.33.0" | ||
__version__ = "4.33.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters