-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: backpopulate transaction email and content title
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
..._subsidy/apps/transaction/management/commands/backpopulate_transaction_email_and_title.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
Management command to backpopulate transaction email and title | ||
""" | ||
import logging | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.db.models import Q | ||
from openedx_ledger.models import Transaction | ||
|
||
from enterprise_subsidy.apps.subsidy.models import Subsidy | ||
from enterprise_subsidy.apps.transaction.utils import batch_by_pk | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Management command for backpopulating transaction email and title | ||
./manage.py backpopulate_transaction_email_and_title | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.dry_run = False | ||
|
||
def add_arguments(self, parser): | ||
""" | ||
Entry point for subclassed commands to add custom arguments. | ||
""" | ||
parser.add_argument( | ||
'--dry-run', | ||
action='store_true', | ||
dest='dry_run', | ||
help=( | ||
'If set, no updates will occur; will instead log ' | ||
'the actions that would have been taken.' | ||
), | ||
) | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
Send it | ||
""" | ||
if options.get('dry_run'): | ||
self.dry_run = True | ||
logger.info("Running in dry-run mode. No updates will occur.") | ||
Check warning on line 47 in enterprise_subsidy/apps/transaction/management/commands/backpopulate_transaction_email_and_title.py Codecov / codecov/patchenterprise_subsidy/apps/transaction/management/commands/backpopulate_transaction_email_and_title.py#L46-L47
|
||
|
||
incomplete_only_filter = Q(lms_user_email__isnull=True) | Q(content_title__isnull=True) | ||
for items_batch in batch_by_pk(Transaction, extra_filter=incomplete_only_filter): | ||
for item in items_batch: | ||
logger.info(f"Processing transaction {item.uuid}") | ||
subsidy = Subsidy.objects.filter(ledger=item.ledger).first() | ||
if not subsidy: | ||
logger.info(f"Skipping transaction {item.uuid} as no subsidy found") | ||
continue | ||
Check warning on line 56 in enterprise_subsidy/apps/transaction/management/commands/backpopulate_transaction_email_and_title.py Codecov / codecov/patchenterprise_subsidy/apps/transaction/management/commands/backpopulate_transaction_email_and_title.py#L55-L56
|
||
logger.info(f"Found subsidy {subsidy.uuid} for transaction {item.uuid}") | ||
lms_user_email = subsidy.email_for_learner(item.lms_user_id) | ||
content_title = subsidy.title_for_content(item.content_key) | ||
logger.info(f"Found {lms_user_email} {content_title} for transaction {item.uuid}") | ||
if not self.dry_run: | ||
item.lms_user_email = lms_user_email | ||
item.content_title = content_title | ||
item.save() | ||
logger.info(f"Updated transaction {item.uuid} with {lms_user_email} {content_title}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,13 @@ def setUp(self): | |
external_fulfillment_provider=self.unknown_provider, | ||
transaction=self.unknown_transaction, | ||
) | ||
self.transaction_to_backpopulate = TransactionFactory( | ||
ledger=self.ledger, | ||
lms_user_email=None, | ||
content_title=None, | ||
quantity=100, | ||
fulfillment_identifier=self.fulfillment_identifier | ||
) | ||
|
||
@mock.patch('enterprise_subsidy.apps.api_client.base_oauth.OAuthAPIClient', return_value=mock.MagicMock()) | ||
def test_write_reversals_from_enterprise_unenrollment_with_existing_reversal(self, mock_oauth_client): | ||
|
@@ -889,3 +896,32 @@ def test_write_reversals_from_geag_enterprise_unenrollments_unknown_provider( | |
with self.assertRaises(FulfillmentException): | ||
call_command('write_reversals_from_enterprise_unenrollments') | ||
assert Reversal.objects.count() == 0 | ||
|
||
@mock.patch("enterprise_subsidy.apps.subsidy.models.Subsidy.lms_user_client") | ||
@mock.patch("enterprise_subsidy.apps.content_metadata.api.ContentMetadataApi.get_content_summary") | ||
def test_backpopulate_transaction_email_and_title( | ||
self, | ||
mock_get_content_summary, | ||
mock_lms_user_client, | ||
): | ||
""" | ||
Test that the backpopulate_transaction_email_and_title management command backpopulates the email and title | ||
""" | ||
expected_email_address = '[email protected]' | ||
mock_lms_user_client.return_value.best_effort_user_data.return_value = { | ||
'email': expected_email_address, | ||
} | ||
expected_content_title = 'a content title' | ||
mock_get_content_summary.return_value = { | ||
'content_uuid': 'a content uuid', | ||
'content_key': 'a content key', | ||
'content_title': expected_content_title, | ||
'source': 'edX', | ||
'mode': 'verified', | ||
'content_price': 10000, | ||
'geag_variant_id': None, | ||
} | ||
call_command('backpopulate_transaction_email_and_title') | ||
self.transaction_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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters