From 886227c88b309d23bf602fedd4d5836e22528f3b Mon Sep 17 00:00:00 2001 From: Phillip Shiu Date: Fri, 23 Jun 2023 14:08:51 -0400 Subject: [PATCH] temp: fix EncodeError for username in expire_and_create_entitlements (#32564) On running the management command, we get the folowing error: kombu.exceptions.EncodeError: Object of type User is not JSON serializable Pass a string of the username instead in the parameters of the tasks created by the expire_and_create_entitlements management command. --- .../commands/expire_and_create_entitlements.py | 7 ++----- common/djangoapps/entitlements/tasks.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/common/djangoapps/entitlements/management/commands/expire_and_create_entitlements.py b/common/djangoapps/entitlements/management/commands/expire_and_create_entitlements.py index 21e041cd20..5497282eaa 100644 --- a/common/djangoapps/entitlements/management/commands/expire_and_create_entitlements.py +++ b/common/djangoapps/entitlements/management/commands/expire_and_create_entitlements.py @@ -9,15 +9,12 @@ from math import ceil from textwrap import dedent from django.core.management import BaseCommand -from django.contrib.auth import get_user_model from common.djangoapps.entitlements.tasks import expire_and_create_entitlements from common.djangoapps.entitlements.models import CourseEntitlement logger = logging.getLogger(__name__) # pylint: disable=invalid-name -User = get_user_model() - class Command(BaseCommand): """ @@ -69,7 +66,7 @@ class Command(BaseCommand): logger.info('Looking for entitlements which may be expirable.') - support_user = User.objects.get(username=options.get('username')) + support_username = options.get('username') current_date = date.today() exceptional_courses = options.get('exceptional_course_uuids') @@ -105,6 +102,6 @@ class Command(BaseCommand): start = batch_num * batch_size end = min(start + batch_size, entitlements_to_expire, entitlements_count) entitlement_ids = [entitlement.id for entitlement in entitlements[start:end]] - expire_and_create_entitlements.delay(entitlement_ids, support_user) + expire_and_create_entitlements.delay(entitlement_ids, support_username) logger.info('Done. Successfully enqueued %d tasks.', num_batches) diff --git a/common/djangoapps/entitlements/tasks.py b/common/djangoapps/entitlements/tasks.py index fa7ecc1be9..3b528dd646 100644 --- a/common/djangoapps/entitlements/tasks.py +++ b/common/djangoapps/entitlements/tasks.py @@ -4,6 +4,7 @@ This file contains celery tasks for entitlements-related functionality. from celery import shared_task from celery.utils.log import get_task_logger from django.conf import settings # lint-amnesty, pylint: disable=unused-import +from django.contrib.auth import get_user_model from edx_django_utils.monitoring import set_code_owner_attribute from common.djangoapps.entitlements.models import CourseEntitlement, CourseEntitlementSupportDetail @@ -16,6 +17,8 @@ LOGGER = get_task_logger(__name__) # unwanted behavior: infinite retries. MAX_RETRIES = 11 +User = get_user_model() + @shared_task(bind=True, ignore_result=True) @set_code_owner_attribute @@ -64,7 +67,7 @@ def expire_old_entitlements(self, start, end, logid='...'): @shared_task(bind=True, ignore_result=True) @set_code_owner_attribute -def expire_and_create_entitlements(self, entitlement_ids, support_user): +def expire_and_create_entitlements(self, entitlement_ids, support_username): """ Expire entitlements older than one year. @@ -76,13 +79,15 @@ def expire_and_create_entitlements(self, entitlement_ids, support_user): Args: entitlement_ids (List): A list of entitlement ids to expire. - support_user (django.contrib.auth.models.user): The username to attribute - the entitlement expiration and recreation to. + support_username (str): The username to attribute the entitlement + expiration and recreation to. Returns: None """ + support_user = User.objects.get(username=support_username) + first_entitlement_id = entitlement_ids[0] last_entitlement_id = entitlement_ids[-1]