Commit 2e2f74fb by Vedran Karačić Committed by GitHub

Merge pull request #1001 from edx/vkaracic/SOL-2124

[SOL-2124] Add command for assigning category to category-less coupons.
parents d6c31572 e1eb332d
import logging
from django.core.management import BaseCommand
from oscar.core.loading import get_model
Category = get_model('catalogue', 'Category')
Product = get_model('catalogue', 'Product')
ProductCategory = get_model('catalogue', 'ProductCategory')
logger = logging.getLogger(__name__)
class Command(BaseCommand):
""" Populate the category field for coupons that do not have a category. """
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
def handle(self, *args, **options):
support_other_cat = Category.objects.get(name='Support-Other')
for coupon in Product.objects.filter(product_class__name='Coupon'):
try:
ProductCategory.objects.get(product=coupon)
except ProductCategory.DoesNotExist:
ProductCategory.objects.create(
product=coupon,
category=support_other_cat
)
logger.info('Added category for coupon [%s]', coupon.id)
from django.core.management import call_command
from oscar.core.loading import get_model
from ecommerce.coupons.tests.mixins import CouponMixin
from ecommerce.tests.testcases import TestCase
Category = get_model('catalogue', 'Category')
ProductCategory = get_model('catalogue', 'ProductCategory')
class TestCouponCommand(CouponMixin, TestCase):
""" Test coupon populate_coupon_category command. """
def setUp(self):
super(TestCouponCommand, self).setUp()
self.filler_category, __ = Category.objects.get_or_create(name='Support-Other', defaults={'depth': 1})
self.coupon = self.create_coupon()
def test_add_category_to_coupon(self):
""" Verify the correct category is assigned to a coupon without category. """
ProductCategory.objects.filter(product=self.coupon).delete()
self.assertEqual(ProductCategory.objects.count(), 0)
call_command('populate_coupon_categories')
self.assertEqual(ProductCategory.objects.count(), 1)
self.assertEqual(ProductCategory.objects.get(product=self.coupon).category, self.filler_category)
def test_coupon_with_category_unaffected(self):
""" Verify coupon with category is unchanged after running command. """
self.assertEqual(ProductCategory.objects.count(), 1)
category = ProductCategory.objects.get(product=self.coupon).category
call_command('populate_coupon_categories')
self.assertEqual(ProductCategory.objects.count(), 1)
self.assertEqual(ProductCategory.objects.get(product=self.coupon).category, category)
......@@ -266,6 +266,7 @@ DJANGO_APPS = [
# Apps specific to this project go here.
LOCAL_APPS = [
'ecommerce.core',
'ecommerce.coupons',
'ecommerce.courses',
'ecommerce.invoice',
'ecommerce.referrals',
......
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