From 618f8cbda396c8952251039b15884148ef457397 Mon Sep 17 00:00:00 2001 From: John Nagro Date: Wed, 22 Nov 2023 14:05:37 +0000 Subject: [PATCH] feat: improve backpopulate job specificity --- .../backpopulate_transaction_email_and_title.py | 10 +++++++--- .../apps/transaction/tests/test_management.py | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/enterprise_subsidy/apps/transaction/management/commands/backpopulate_transaction_email_and_title.py b/enterprise_subsidy/apps/transaction/management/commands/backpopulate_transaction_email_and_title.py index 77ff3cf2..d93940c4 100644 --- a/enterprise_subsidy/apps/transaction/management/commands/backpopulate_transaction_email_and_title.py +++ b/enterprise_subsidy/apps/transaction/management/commands/backpopulate_transaction_email_and_title.py @@ -53,11 +53,11 @@ def process_transaction(self, subsidy, txn): """ logger.info(f"Processing {subsidy.uuid} transaction {txn.uuid}") - if txn.lms_user_email is None: + if txn.lms_user_email is None and txn.lms_user_id is not None: lms_user_email = subsidy.email_for_learner(txn.lms_user_id) txn.lms_user_email = lms_user_email logger.info(f"Found {lms_user_email} for {subsidy.uuid} transaction {txn.uuid}") - if txn.content_title is None: + if txn.content_title is None and txn.content_key is not None: content_title = subsidy.title_for_content(txn.content_key) txn.content_title = content_title logger.info(f"Found {content_title} for {subsidy.uuid} transaction {txn.uuid}") @@ -85,9 +85,13 @@ def handle(self, *args, **options): for subsidies in batch_by_pk(Subsidy, extra_filter=subsidy_filter): for subsidy in subsidies: logger.info(f"Processing subsidy {subsidy.uuid}") + subsidy_filter = Q(ledger=subsidy.ledger) - incomplete_only_filter = Q(lms_user_email__isnull=True) | Q(content_title__isnull=True) + incomplete_email = Q(lms_user_email__isnull=True) & Q(lms_user_id__isnull=False) + incomplete_title = Q(content_title__isnull=True) & Q(content_key__isnull=False) + incomplete_only_filter = incomplete_email | incomplete_title txn_filter = subsidy_filter & incomplete_only_filter + for txns in batch_by_pk(Transaction, extra_filter=txn_filter): for txn in txns: self.process_transaction(subsidy, txn) diff --git a/enterprise_subsidy/apps/transaction/tests/test_management.py b/enterprise_subsidy/apps/transaction/tests/test_management.py index 96ea9e03..96293c3a 100644 --- a/enterprise_subsidy/apps/transaction/tests/test_management.py +++ b/enterprise_subsidy/apps/transaction/tests/test_management.py @@ -88,8 +88,13 @@ def setUp(self): ledger=self.internal_ledger, lms_user_email=None, content_title=None, - quantity=100, - fulfillment_identifier=self.fulfillment_identifier + ) + self.transaction_not_to_backpopulate = TransactionFactory( + ledger=self.ledger, + lms_user_id=None, + lms_user_email=None, + content_key=None, + content_title=None, ) @mock.patch('enterprise_subsidy.apps.api_client.base_oauth.OAuthAPIClient', return_value=mock.MagicMock()) @@ -933,10 +938,13 @@ def test_backpopulate_transaction_email_and_title( call_command('backpopulate_transaction_email_and_title') self.transaction_to_backpopulate.refresh_from_db() self.internal_transaction_to_backpopulate.refresh_from_db() + self.transaction_not_to_backpopulate.refresh_from_db() assert self.transaction_to_backpopulate.lms_user_email == expected_email_address assert self.transaction_to_backpopulate.content_title == expected_content_title assert self.internal_transaction_to_backpopulate.lms_user_email is None assert self.internal_transaction_to_backpopulate.content_title is None + assert self.transaction_not_to_backpopulate.lms_user_email is None + assert self.transaction_not_to_backpopulate.content_title is None @mock.patch("enterprise_subsidy.apps.subsidy.models.Subsidy.lms_user_client") @mock.patch("enterprise_subsidy.apps.content_metadata.api.ContentMetadataApi.get_content_summary") @@ -965,7 +973,10 @@ def test_backpopulate_transaction_email_and_title_include_internal( call_command('backpopulate_transaction_email_and_title', include_internal_subsidies=True) self.transaction_to_backpopulate.refresh_from_db() self.internal_transaction_to_backpopulate.refresh_from_db() + self.transaction_not_to_backpopulate.refresh_from_db() assert self.transaction_to_backpopulate.lms_user_email == expected_email_address assert self.transaction_to_backpopulate.content_title == expected_content_title assert self.internal_transaction_to_backpopulate.lms_user_email == expected_email_address assert self.internal_transaction_to_backpopulate.content_title == expected_content_title + assert self.transaction_not_to_backpopulate.lms_user_email is None + assert self.transaction_not_to_backpopulate.content_title is None