def basket_and_coupons(): """ Sample basket and coupon """ basket_item = BasketItemFactory() # Some prices for the basket item product ProductVersionFactory(product=basket_item.product, price=Decimal("15.00")) product_version = ProductVersionFactory( product=basket_item.product, price=Decimal("25.00") ) run = basket_item.product.content_object CourseRunSelection.objects.create(run=run, basket=basket_item.basket) payment_worst = CouponPaymentFactory() payment_best = CouponPaymentFactory() coupon_worst = CouponFactory(payment=payment_worst, coupon_code="WORST") coupon_best = CouponFactory(payment=payment_best, coupon_code="BEST") # Coupon payment for worst coupon, with lowest discount civ_worst = CouponPaymentVersionFactory( payment=payment_worst, amount=Decimal("0.10000"), automatic=True ) # Coupon payment for best coupon, with highest discount civ_best_old = CouponPaymentVersionFactory( payment=payment_best, amount=Decimal("0.50000") ) # Coupon payment for best coupon, more recent than previous so takes precedence civ_best = CouponPaymentVersionFactory( payment=payment_best, amount=Decimal("1.00000") ) # Coupon version for worst coupon cv_worst = CouponVersionFactory(payment_version=civ_worst, coupon=coupon_worst) # Coupon version for best coupon CouponVersionFactory(payment_version=civ_best_old, coupon=coupon_best) # Most recent coupon version for best coupon cv_best = CouponVersionFactory(payment_version=civ_best, coupon=coupon_best) # Both best and worst coupons eligible for the product CouponEligibilityFactory(coupon=coupon_best, product=basket_item.product) CouponEligibilityFactory(coupon=coupon_worst, product=basket_item.product) # Apply one of the coupons to the basket CouponSelectionFactory.create(basket=basket_item.basket, coupon=coupon_best) coupongroup_worst = CouponGroup(coupon_worst, cv_worst, payment_worst, civ_worst) coupongroup_best = CouponGroup(coupon_best, cv_best, payment_best, civ_best) return SimpleNamespace( basket=basket_item.basket, basket_item=basket_item, product_version=product_version, coupongroup_best=coupongroup_best, coupongroup_worst=coupongroup_worst, run=run, )
def redeemed_voucher_and_user_client(voucher_and_user, client): """ Returns a voucher, user, and authenticated client """ user = voucher_and_user.user voucher = voucher_and_user.voucher client.force_login(user) voucher.coupon = CouponFactory() voucher.save() CouponRedemptionFactory(coupon_version__coupon=voucher.coupon) return SimpleNamespace(**vars(voucher_and_user), client=client)