Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve backpopulate job specificity #182

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
"""
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}")
Expand All @@ -71,8 +71,8 @@
Find all transactions that are missing email or title and backpopulate them
"""
if options.get('dry_run'):
self.dry_run = True
logger.info("Running in dry-run mode. No updates will occur.")

Check warning on line 75 in enterprise_subsidy/apps/transaction/management/commands/backpopulate_transaction_email_and_title.py

View check run for this annotation

Codecov / codecov/patch

enterprise_subsidy/apps/transaction/management/commands/backpopulate_transaction_email_and_title.py#L74-L75

Added lines #L74 - L75 were not covered by tests

if options.get('include_internal_subsidies'):
self.include_internal_subsidies = True
Expand All @@ -85,9 +85,13 @@
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)
15 changes: 13 additions & 2 deletions enterprise_subsidy/apps/transaction/tests/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Loading