Commit 0cfcd935 by Vedran Karačić

Merge pull request #734 from edx/remove-mngmt-cmd

Remove voucher management command
parents ceab5ccd db444687
""" This command fixes SINGLE_USE vouchers."""
import logging
from django.core.management import BaseCommand
from oscar.core.loading import get_model
logger = logging.getLogger(__name__)
Voucher = get_model('voucher', 'Voucher')
class Command(BaseCommand):
"""
Fix all SINGLE_USE vouchers.
Some SINGLE_USE vouchers may have been created with the wrong max_applications set.
This command sets their max_global_applications to the default value.
"""
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
def handle(self, *args, **options):
vouchers = Voucher.objects.filter(usage=Voucher.SINGLE_USE, offers__max_global_applications=1)
for v in vouchers:
offer = v.offers.first()
logger.info('Modifying Voucher %s.', v.name)
offer.max_global_applications = None
offer.save()
# encoding: utf-8
"""Contains the tests for fix_single_use command."""
from django.core.management import call_command
from mock import patch
from oscar.core.loading import get_model
from ecommerce.extensions.test.factories import prepare_voucher
from ecommerce.tests.mixins import CouponMixin
from ecommerce.tests.testcases import TestCase
Voucher = get_model('voucher', 'Voucher')
COUPON_CODE = 'COUPONTEST'
class FixSingleUseVoucherTests(CouponMixin, TestCase):
"""Tests the fix_single_use command."""
def setUp(self):
super(FixSingleUseVoucherTests, self).setUp()
def test_command_called_successfully(self):
""" Verify command runs. """
with patch('ecommerce.extensions.voucher.management.commands.fix_single_use.Command') as mock_call_command:
call_command('fix_single_use')
self.assertTrue(mock_call_command.called)
def assert_command_execution(self, usage, max_usage, expected_before, expected_after):
""" Assert offer max_global_applications value changed after the command call. """
voucher, __ = prepare_voucher(code=COUPON_CODE, usage=usage, max_usage=max_usage)
offer_before = voucher.offers.first()
self.assertEqual(offer_before.max_global_applications, expected_before)
call_command('fix_single_use')
offer_after = voucher.offers.first()
self.assertEqual(offer_after.max_global_applications, expected_after)
def test_offer_changed(self):
""" Verify the offer has changed. """
self.assert_command_execution(Voucher.SINGLE_USE, 1, 1, None)
def test_offer_unchanged(self):
""" Verify voucher other than SINGLE_USE are left unaffected. """
self.assert_command_execution(Voucher.ONCE_PER_CUSTOMER, 1, 1, 1)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment