This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Added management command to update voucher name
fix: sorted imports Fix: Support create-mobile-skus to run again if failed (#4129) * fix: Support create-mobile-skus to run again if we found an error previously fix: Enable enrollment code purchase with mobile seats (#4130) * fix: Enable enrollment code purchase with mobile seats * test: Add unit test --------- Co-authored-by: Abdul Moeez Zahid <[email protected]> fix: PEP8 conventions fix: import issue chore: added more test cases fix: added new line at the end of file fix: blank line issue fix: long line issue fix: pylint issues fix: import issues fix: removed extra blank line fix: updated test fix: issues in command fix:: isort issue
- Loading branch information
1 parent
d8a1218
commit b405ec9
Showing
12 changed files
with
201 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Empty file.
Empty file.
Empty file.
67 changes: 67 additions & 0 deletions
67
ecommerce/extensions/voucher/management/commands/tests/test_update_voucher_names.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,67 @@ | ||
from datetime import timedelta | ||
from unittest import mock | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
|
||
from ecommerce.extensions.voucher.models import Voucher | ||
from ecommerce.extensions.voucher.tasks import update_voucher_names | ||
|
||
|
||
class ManagementCommandTests(TestCase): | ||
def setUp(self): | ||
self.data = { | ||
'name': 'Test voucher', | ||
'code': 'TESTCODE', | ||
'start_datetime': timezone.now() , | ||
'end_datetime': timezone.now() + timedelta(days=7) | ||
} | ||
voucher = Voucher.objects.create(**self.data) | ||
|
||
@mock.patch('ecommerce.extensions.voucher.tasks.update_voucher_names.delay') | ||
def test_update_voucher_names_command(self, mock_delay): | ||
|
||
call_command('update_voucher_names') | ||
# Assert that the Celery task is scheduled | ||
self.assertTrue(mock_delay.called) | ||
|
||
@mock.patch('ecommerce.extensions.voucher.models.Voucher.objects.all') | ||
def test_update_voucher_names_task(self, mock_all): | ||
# Mock Voucher objects | ||
start_datetime = timezone.now() | ||
end_datetime = timezone.now() + timedelta(days=7) | ||
|
||
mock_vouchers = [ | ||
Voucher(id=1, name='Name1', code='SASAFR', | ||
start_datetime=start_datetime, end_datetime=start_datetime ), | ||
Voucher(id=2, name='Name2', code='EWRRFEC', | ||
start_datetime=start_datetime, end_datetime=end_datetime), | ||
] | ||
mock_all.return_value = mock_vouchers | ||
|
||
# Call the Celery task | ||
update_voucher_names(mock_vouchers) | ||
# Assert that the names are updated as expected | ||
self.assertEqual(mock_vouchers[0].name, '1 - Name1') | ||
self.assertEqual(mock_vouchers[1].name, '2 - Name2') | ||
|
||
@mock.patch('ecommerce.extensions.voucher.tasks.update_voucher_names.delay') | ||
def test_voucher_name_update_once(self, mock_delay): | ||
original_name = 'Original Name' | ||
code = 'ABC123XSD' | ||
start_datetime = timezone.now() | ||
end_datetime = start_datetime + timedelta(days=7) | ||
voucher = Voucher.objects.create(name=original_name, | ||
code=code, | ||
start_datetime=start_datetime, | ||
end_datetime=end_datetime | ||
) | ||
|
||
call_command('update_voucher_names') | ||
call_command('update_voucher_names') | ||
|
||
updated_voucher = Voucher.objects.get(id=voucher.id) | ||
|
||
self.assertEqual(mock_delay.call_count, 2) | ||
self.assertEqual(updated_voucher.name, f"{voucher.id} - {original_name}") |
35 changes: 35 additions & 0 deletions
35
ecommerce/extensions/voucher/management/commands/update_voucher_names.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,35 @@ | ||
# ecommerce/extensions/vouchers/management/commands/update_voucher_names.py | ||
import logging | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from ecommerce.extensions.voucher.models import Voucher | ||
from ecommerce.extensions.voucher.tasks import update_voucher_names | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Update voucher names asynchronously' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('--batch-size', type=int, default=1000, help='Number of vouchers to process in each batch') | ||
|
||
def handle(self, *args, **options): | ||
batch_size = options['batch_size'] | ||
|
||
total_vouchers = Voucher.objects.count() | ||
processed_vouchers = 0 | ||
|
||
logger.info("Total number of vouchers: %d", total_vouchers) | ||
|
||
while processed_vouchers < total_vouchers: | ||
vouchers = Voucher.objects.all()[processed_vouchers:processed_vouchers + batch_size] | ||
try: | ||
# Call the Celery task asynchronously for each batch | ||
update_voucher_names.delay(vouchers) | ||
except Exception as exc: # pylint: disable=broad-except | ||
logger.exception("Error updating voucher names: %s", exc) | ||
|
||
processed_vouchers += len(vouchers) | ||
logger.info("Processed %d out of %d vouchers", processed_vouchers, total_vouchers) |
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,25 @@ | ||
import logging | ||
|
||
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 ecommerce.extensions.voucher.models import Voucher | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@shared_task(bind=True, ignore_result=True) | ||
def update_voucher_names(self, vouchers): | ||
for voucher in vouchers: | ||
if not f"{voucher.id} -" in voucher.name: | ||
updated_name = f"{voucher.id} - {voucher.name}" | ||
try: | ||
if len(updated_name) > 128: | ||
logger.warning("Name length exceeds 128 characters for voucher id %d. Truncating...", voucher.id) | ||
updated_name = updated_name[:128] | ||
|
||
voucher.name = updated_name | ||
voucher.save() | ||
except Exception as exc: # pylint: disable=broad-except | ||
logger.exception("Error updating voucher name %d: %s", voucher.id, exc) |