def test_generate_coupon_slug(self): """Verify the method generates proper slug.""" title = 'Test coupon' _hash = ' '.join( (unicode(title), unicode(self.catalog.id), str(self.partner.id))) _hash = md5(_hash.lower()).hexdigest()[-10:] expected = _hash.upper() actual = generate_coupon_slug(self.partner, title=title, catalog=self.catalog) self.assertEqual(actual, expected)
def test_generate_coupon_slug(self): """Verify the method generates proper slug.""" title = 'Test coupon' _hash = ' '.join(( unicode(title), unicode(self.catalog.id), str(self.partner.id) )) _hash = md5(_hash.lower()).hexdigest()[-10:] expected = _hash.upper() actual = generate_coupon_slug(self.partner, title=title, catalog=self.catalog) self.assertEqual(actual, expected)
def create_coupon_product(self, title, price, data): """Creates a coupon product and a stock record for it. Arguments: title (str): The name of the coupon. price (int): The price of the coupon(s). data (dict): Contains data needed to create vouchers,SKU and UPC: - partner (User) - benefit_type (str) - benefit_value (int) - catalog (Catalog) - end_date (Datetime) - code (str) - quantity (int) - start_date (Datetime) - voucher_type (str) - categories (list of Category objects) Returns: A coupon product object. Raises: IntegrityError: An error occured when create_vouchers method returns an IntegrityError exception ValidationError: An error occured clean() validation method returns a ValidationError exception """ coupon_slug = generate_coupon_slug(title=title, catalog=data['catalog'], partner=data['partner']) product_class = ProductClass.objects.get(slug='coupon') coupon_product, __ = Product.objects.get_or_create( title=title, product_class=product_class, slug=coupon_slug ) for category in data['categories']: ProductCategory.objects.get_or_create(product=coupon_product, category=category) # Vouchers are created during order and not fulfillment like usual # because we want vouchers to be part of the line in the order. try: create_vouchers( name=title, benefit_type=data['benefit_type'], benefit_value=Decimal(data['benefit_value']), catalog=data['catalog'], coupon=coupon_product, end_datetime=data['end_date'], code=data['code'] or None, quantity=int(data['quantity']), start_datetime=data['start_date'], voucher_type=data['voucher_type'] ) except IntegrityError as ex: logger.exception('Failed to create vouchers for [%s] coupon.', coupon_product.title) raise IntegrityError(ex) # pylint: disable=nonstandard-exception coupon_vouchers = CouponVouchers.objects.get(coupon=coupon_product) coupon_product.attr.coupon_vouchers = coupon_vouchers coupon_product.save() sku = generate_sku( product=coupon_product, partner=data['partner'], catalog=data['catalog'], ) stock_record, __ = StockRecord.objects.get_or_create( product=coupon_product, partner=data['partner'], partner_sku=sku ) stock_record.price_currency = 'USD' stock_record.price_excl_tax = price stock_record.save() return coupon_product
def create_coupon_product(self, title, price, data): """Creates a coupon product and a stock record for it. Arguments: title (str): The name of the coupon. price (int): The price of the coupon(s). data (dict): Contains data needed to create vouchers,SKU and UPC: - partner (User) - benefit_type (str) - benefit_value (int) - catalog (Catalog) - end_date (Datetime) - code (str) - quantity (int) - start_date (Datetime) - voucher_type (str) Returns: A coupon product object. Raises: IntegrityError: An error occured when create_vouchers method returns an IntegrityError exception ValidationError: An error occured clean() validation method returns a ValidationError exception """ coupon_slug = generate_coupon_slug(title=title, catalog=data['catalog'], partner=data['partner']) product_class = ProductClass.objects.get(slug='coupon') coupon_product, __ = Product.objects.get_or_create( title=title, product_class=product_class, slug=coupon_slug ) # Vouchers are created during order and not fulfillment like usual # because we want vouchers to be part of the line in the order. try: create_vouchers( name=title, benefit_type=data['benefit_type'], benefit_value=Decimal(data['benefit_value']), catalog=data['catalog'], coupon=coupon_product, end_datetime=data['end_date'], code=data['code'] or None, quantity=int(data['quantity']), start_datetime=data['start_date'], voucher_type=data['voucher_type'] ) except IntegrityError as ex: logger.exception('Failed to create vouchers for [%s] coupon.', coupon_product.title) raise IntegrityError(ex) # pylint: disable=nonstandard-exception coupon_vouchers = CouponVouchers.objects.get(coupon=coupon_product) coupon_product.attr.coupon_vouchers = coupon_vouchers coupon_product.save() sku = generate_sku( product=coupon_product, partner=data['partner'], catalog=data['catalog'], ) stock_record, __ = StockRecord.objects.get_or_create( product=coupon_product, partner=data['partner'], partner_sku=sku ) stock_record.price_currency = 'USD' stock_record.price_excl_tax = price stock_record.save() return coupon_product