Skip to content

Commit

Permalink
feat: add new endpoint to unlink the logged in user
Browse files Browse the repository at this point in the history
  • Loading branch information
jajjibhai008 committed Nov 1, 2024
1 parent 760694e commit d2b7647
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 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.31.0]
--------
* feat: add new endpoint to unlink the logged in user.

[4.30.1]
--------
* fix: serialize best_mode_for_course_run field in DefaultEnterpriseEnrollmentIntentionSerializer.
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.30.1"
__version__ = "4.31.0"
15 changes: 15 additions & 0 deletions enterprise/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,21 @@ class EnterpriseCustomerUnlinkUsersSerializer(serializers.Serializer):
)


class EnterpriseCustomerUnlinkSelfUserSerializer(serializers.Serializer):
"""
Serializer for the ``EnterpriseCustomerViewSet`` unlink_self action.
"""

user_email = serializers.EmailField(
allow_blank=False,
)

is_relinkable = serializers.BooleanField(
required=False,
default=False,
)


class EnterpriseCatalogQuerySerializer(serializers.ModelSerializer):
"""
Serializer for the ``EnterpriseCatalogQuery`` model.
Expand Down
26 changes: 25 additions & 1 deletion enterprise/api/v1/views/enterprise_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
HTTP_202_ACCEPTED,
HTTP_400_BAD_REQUEST,
HTTP_409_CONFLICT,
HTTP_422_UNPROCESSABLE_ENTITY,
)

from django.contrib import auth
Expand Down Expand Up @@ -460,7 +461,6 @@ def unlink_users(self, request, pk=None): # pylint: disable=unused-argument
"""
Unlinks users with the given emails from the enterprise.
"""

serializer = serializers.EnterpriseCustomerUnlinkUsersSerializer(
data=request.data
)
Expand All @@ -487,3 +487,27 @@ def unlink_users(self, request, pk=None): # pylint: disable=unused-argument
raise UnlinkUserFromEnterpriseError(msg) from exc

return Response(status=HTTP_200_OK)

@action(methods=['post'], detail=True, permission_classes=[permissions.IsAuthenticated])
def unlink_self(self, request, pk=None): # pylint: disable=unused-argument
"""
Unlink request user from the enterprise.
"""
user_email = request.user.email
enterprise_customer = self.get_object()

try:
models.EnterpriseCustomerUser.objects.unlink_user(
enterprise_customer=enterprise_customer, user_email=user_email, is_relinkable=True
)
except (models.EnterpriseCustomerUser.DoesNotExist, models.PendingEnterpriseCustomerUser.DoesNotExist):
msg = "[UNLINK_SELF] User with email {} does not exist in enterprise {}.".format(
user_email, enterprise_customer
)
LOGGER.warning(msg)
return Response(status=HTTP_422_UNPROCESSABLE_ENTITY)
except Exception as exc:
msg = "[UNLINK_SELF] Could not unlink {} from {}".format(user_email, enterprise_customer)
raise UnlinkUserFromEnterpriseError(msg) from exc

return Response(status=HTTP_200_OK)

0 comments on commit d2b7647

Please sign in to comment.