Skip to content

Commit

Permalink
feat: adding new api endpoint to handle user retirement
Browse files Browse the repository at this point in the history
  • Loading branch information
DonatoBD authored and igobranco committed Dec 26, 2024
1 parent 0c9a809 commit 36336ca
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ecommerce/extensions/api/v2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
url(r'^tracking_id/{}/$'.format(USERNAME_PATTERN), retirement_views.EcommerceIdView.as_view(), name='tracking_id')
]

ECOMMERCE_RETIREMENT_URLS = [
url(r'^retire/$', retirement_views.EcommerceUserRetireView.as_view(), name='retire_user')
]

COUPON_URLS = [
url(r'^coupon_reports/(?P<coupon_id>[\d]+)/$', CouponReportCSVView.as_view(), name='coupon_reports'),
url(r'^categories/$', coupon_views.CouponCategoriesListView.as_view(), name='coupons_categories'),
Expand Down Expand Up @@ -119,6 +123,7 @@
url(r'^publication/', include((ATOMIC_PUBLICATION_URLS, 'publication'))),
url(r'^refunds/', include((REFUND_URLS, 'refunds'))),
url(r'^retirement/', include((RETIREMENT_URLS, 'retirement'))),
url(r'^user/', include((ECOMMERCE_RETIREMENT_URLS, 'ecommerce_retirement'))),
url(r'^user_management/', include((USER_MANAGEMENT_URLS, 'user_management'))),
url(r'^assignment-email/', include((ASSIGNMENT_EMAIL_URLS, 'assignment-email'))),
url(r'^webhooks/', include((WEBHOOKS_URLS, 'webhooks'))),
Expand Down
51 changes: 51 additions & 0 deletions ecommerce/extensions/api/v2/views/retirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
Endpoints to facilitate retirement actions
"""
from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication
from django.conf import settings
from rest_framework import permissions, status
from rest_framework.response import Response
from rest_framework.views import APIView
from social_django.models import UserSocialAuth
from user_util import user_util

from ecommerce.core.models import User
from ecommerce.extensions.analytics.utils import ECOM_TRACKING_ID_FMT
Expand Down Expand Up @@ -51,3 +54,51 @@ def get(self, _, username):
status=status.HTTP_404_NOT_FOUND,
data={'message': 'Invalid user.'}
)

class EcommerceUserRetireView(APIView):
"""
Provides API endpoint for retiring a ecommerce's user.
"""
authentication_classes = (JwtAuthentication,)
permission_classes = (permissions.IsAuthenticated, permissions.IsAdminUser)

def post(self, request):
"""
POST /api/v2/user/retire/
```
{
'username': 'user_to_retire'
}
```
Retires the user with the given username. This includes
retiring this username, retiring the email address, and
deleting the social_auth associated with the lms user.
"""
try:
username = request.data['username']
if not username:
raise User.DoesNotExist()

user = User.objects.get(username=username)

# Delete social_auth asssociated with the lms user
UserSocialAuth.objects.filter(uid=username).delete()

# Generate retired email based on retirement settings
user.email = user_util.get_retired_email(user.email, settings.RETIRED_USER_SALTS, settings.RETIRED_EMAIL_FMT)
user.username = user_util.get_retired_username(username, settings.RETIRED_USER_SALTS, settings.RETIRED_USERNAME_FMT)
user.save()

return Response(
{
'id': user.pk,
'ecommerce_tracking_id': ECOM_TRACKING_ID_FMT.format(user.pk),
}
)
except User.DoesNotExist:
return Response(
status=status.HTTP_404_NOT_FOUND,
data={'message': 'User does not exist on ecommerce service.'}
)
14 changes: 14 additions & 0 deletions ecommerce/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,3 +891,17 @@
OfferUsageEmailTypes.LOW_BALANCE: BRAZE_OFFER_LOW_BALANCE_CAMPAIGN,
OfferUsageEmailTypes.OUT_OF_BALANCE: BRAZE_OFFER_NO_BALANCE_CAMPAIGN
}

############### Settings for Retirement #####################
# See annotations in lms/envs/common.py for details.
RETIRED_USERNAME_PREFIX = 'retired__user_'
# See annotations in lms/envs/common.py for details.
RETIRED_EMAIL_PREFIX = 'retired__user_'
# See annotations in lms/envs/common.py for details.
RETIRED_EMAIL_DOMAIN = 'retired.invalid'
# See annotations in lms/envs/common.py for details.
RETIRED_USERNAME_FMT = 'retired__user_{}'
# See annotations in lms/envs/common.py for details.
RETIRED_EMAIL_FMT = 'retired__user_{}@retired.invalid'
# See annotations in lms/envs/common.py for details.
RETIRED_USER_SALTS = ['abc', '123']
1 change: 1 addition & 0 deletions requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ social-auth-app-django
sorl-thumbnail
stripe
unicodecsv
user-util==1.1.0
xss-utils
zeep

2 changes: 2 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ urllib3==1.26.18
# botocore
# cybersource-rest-client-python
# requests
user-util==1.1.0
# via -r requirements/base.txt
vine==5.1.0
# via
# amqp
Expand Down
2 changes: 2 additions & 0 deletions requirements/production.txt
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,8 @@ urllib3==1.26.18
# botocore
# cybersource-rest-client-python
# requests
user-util==1.1.0
# via -r requirements/base.txt
vine==5.1.0
# via
# amqp
Expand Down

0 comments on commit 36336ca

Please sign in to comment.