Esempio n. 1
0
def prepare_voucher(code='COUPONTEST',
                    _range=None,
                    start_datetime=None,
                    end_datetime=None,
                    benefit_value=100,
                    benefit_type=Benefit.PERCENTAGE,
                    usage=Voucher.SINGLE_USE,
                    max_usage=None,
                    email_domains=None,
                    enterprise_customer=None,
                    site=None):
    """ Helper function to create a voucher and add an offer to it that contains a product. """

    # NOTE (CCB): We use empty categories here to avoid unique-constraint issues that occur when we use
    # ProductCategoryFactory in conjunction with pre-created Category objects.
    if _range is None:
        product = ProductFactory(categories=[])
        _range = RangeFactory(products=[product],
                              enterprise_customer=enterprise_customer)
    elif _range.num_products() > 0:
        product = _range.all_products()[0]
    else:
        product = ProductFactory(categories=[])

    if start_datetime is None:
        start_datetime = now() - timedelta(days=1)

    if end_datetime is None:
        end_datetime = now() + timedelta(days=10)

    voucher = VoucherFactory(code=code,
                             start_datetime=start_datetime,
                             end_datetime=end_datetime,
                             usage=usage)
    benefit = BenefitFactory(type=benefit_type,
                             range=_range,
                             value=benefit_value)
    condition = ConditionFactory(value=1,
                                 range=_range,
                                 enterprise_customer_uuid=enterprise_customer)
    if max_usage:
        offer = ConditionalOfferFactory(offer_type=ConditionalOffer.VOUCHER,
                                        benefit=benefit,
                                        condition=condition,
                                        max_global_applications=max_usage,
                                        email_domains=email_domains,
                                        priority=OFFER_PRIORITY_VOUCHER)
    else:
        offer = ConditionalOfferFactory(
            offer_type=ConditionalOffer.VOUCHER,
            benefit=benefit,
            condition=condition,
            email_domains=email_domains,
            partner=site.siteconfiguration.partner if site else None,
            priority=OFFER_PRIORITY_VOUCHER)
    voucher.offers.add(offer)
    return voucher, product
 def setUp(self):
     self.basket = create_basket(empty=True)
     self.product = create_product(price=100)
     range = RangeFactory(products=[self.product])
     condition = ConditionFactory(range=range, value=2)
     self.voucher = VoucherFactory()
     self.voucher.offers.add(
         create_offer(offer_type='Voucher',
                      range=range,
                      condition=condition))
Esempio n. 3
0
 def setUp(self):
     self.applicator = Applicator()
     self.basket = BasketFactory()
     rng = RangeFactory(includes_all_products=True)
     self.condition = ConditionFactory(
         range=rng, type=ConditionFactory._meta.model.VALUE,
         value=D('100'), proxy_class=None)
     self.benefit = BenefitFactory(
         range=rng, type=BenefitFactory._meta.model.FIXED,
         value=D('10'))
Esempio n. 4
0
 def setUp(self):
     self.applicator = Applicator()
     rng = RangeFactory(includes_all_products=True)
     self.condition = ConditionFactory(
         range=rng, type=ConditionFactory._meta.model.VALUE,
         value=D('100'), proxy_class=None)
     self.benefit = BenefitFactory(
         range=rng, type=BenefitFactory._meta.model.FIXED,
         value=D('10'), max_affected_items=1)
     self.basket = factories.create_basket()
     self.line = self.basket.all_lines()[0]
Esempio n. 5
0
    def test_apply_offer_with_multibuy_benefit_and_count_condition(self):
        rng = RangeFactory(includes_all_products=True)
        condition = ConditionFactory(range=rng,
                                     type=ConditionFactory._meta.model.COUNT,
                                     value=1)
        benefit = BenefitFactory(range=rng,
                                 type=BenefitFactory._meta.model.MULTIBUY,
                                 value=1)
        offer = ConditionalOfferFactory(condition=condition, benefit=benefit)

        add_product(self.basket, D('100'), 5)

        applicator = Applicator()
        applicator.apply_offers(self.basket, [offer])
        line = self.basket.all_lines()[0]
        assert line.quantity_with_offer_discount(offer) == 1

        self.basket.refresh_from_db()
        assert self.basket.total_discount == D('100')
Esempio n. 6
0
def prepare_enterprise_voucher(code='COUPONTEST',
                               start_datetime=None,
                               end_datetime=None,
                               benefit_value=100,
                               benefit_type=Benefit.PERCENTAGE,
                               usage=Voucher.SINGLE_USE,
                               enterprise_customer=None,
                               enterprise_customer_catalog=None):
    """ Helper function to create a voucher and add an enterprise conditional offer to it. """
    if start_datetime is None:
        start_datetime = now() - timedelta(days=1)

    if end_datetime is None:
        end_datetime = now() + timedelta(days=10)

    voucher = VoucherFactory(code=code,
                             start_datetime=start_datetime,
                             end_datetime=end_datetime,
                             usage=usage)
    benefit = BenefitFactory(
        proxy_class=class_path(ENTERPRISE_BENEFIT_MAP[benefit_type]),
        value=benefit_value,
        type='',
        range=None,
    )
    condition = ConditionFactory(
        proxy_class=class_path(EnterpriseCustomerCondition),
        enterprise_customer_uuid=enterprise_customer,
        enterprise_customer_catalog_uuid=enterprise_customer_catalog,
        range=None,
    )
    offer = ConditionalOfferFactory(offer_type=ConditionalOffer.VOUCHER,
                                    benefit=benefit,
                                    condition=condition,
                                    priority=OFFER_PRIORITY_VOUCHER)

    voucher.offers.add(offer)
    return voucher
 def setUp(self):
     product = create_product(price=100)
     self.offer_range = RangeFactory(products=[product])
     self.offer_condition = ConditionFactory(range=self.offer_range,
                                             value=2)
    def setUp(self):
        """
        Create test data.
        """
        super(MigrateEnterpriseConditionalOffersTests, self).setUp()

        # Set up vouchers that relate to a range with a enterprise_customer
        uuid = '123e4567-e89b-12d3-a456-426655440000'
        range_with_ent_customer = RangeFactory(enterprise_customer=uuid)
        condition = ConditionFactory(range=range_with_ent_customer)
        benefit_percent = BenefitFactory(
            range=range_with_ent_customer,
            type='Percentage',
            value=10.00,
        )
        benefit_absolute = BenefitFactory(
            range=range_with_ent_customer,
            type='Absolute',
            value=47,
        )

        for i in range(2):
            code = '{}EntUserPercentBenefit'.format(i)
            voucher = VoucherFactory(code=code)
            offer_name = "Coupon [{}]-{}-{}".format(voucher.pk,
                                                    benefit_percent.type,
                                                    benefit_percent.value)
            conditional_offer = ConditionalOfferFactory(
                condition=condition,
                benefit=benefit_percent,
                name=offer_name,
            )
            voucher.offers.add(conditional_offer)

        for i in range(2):
            code = '{}EntUserAbsoluteBenefit'.format(i)
            voucher = VoucherFactory(code=code)
            offer_name = "Coupon [{}]-{}-{}".format(voucher.pk,
                                                    benefit_absolute.type,
                                                    benefit_absolute.value)
            conditional_offer = ConditionalOfferFactory(
                condition=condition,
                benefit=benefit_absolute,
                name=offer_name,
            )
            voucher.offers.add(conditional_offer)

        # Set up vouchers that do not relate to a range with an enterprise_customer
        range_no_ent_customer = RangeFactory()
        condition = ConditionFactory(range=range_no_ent_customer)
        benefit = BenefitFactory(
            range=range_no_ent_customer,
            type='Percentage',
            value=10.00,
        )

        for i in range(3):
            code = '{}NoEntUserPercentBenefit'.format(i)
            voucher = VoucherFactory(code=code)
            offer_name = "Coupon [{}]-{}-{}".format(voucher.pk, benefit.type,
                                                    benefit.value)
            conditional_offer = ConditionalOfferFactory(
                condition=condition,
                benefit=benefit,
                name=offer_name,
            )
            voucher.offers.add(conditional_offer)

        assert Voucher.objects.filter(
            offers__condition__range__enterprise_customer__isnull=False).count(
            ) == 4

        assert Voucher.objects.filter(
            offers__condition__range__enterprise_customer__isnull=True).count(
            ) == 3

        self.command = Command()
Esempio n. 9
0
 def setUp(self):
     super(OfferDecoratorTests, self).setUp()
     self.condition = ConditionFactory()
     self.offer = ConditionalOfferFactory(condition=self.condition,
                                          partner=self.partner)
     self.user = UserFactory()