Beispiel #1
0
 def validate_limit(self, value):
     limit = self.limit or value
     if value < limit:
         msg = pgettext(
             'Voucher not applicable',
             'This offer is only valid for orders over %(amount)s.')
         raise NotApplicable(msg % {'amount': net(limit)}, limit=limit)
Beispiel #2
0
 def validate_limit(self, value):
     limit = self.limit if self.limit is not None else value
     if value < limit:
         msg = pgettext(
             'Voucher not applicable',
             'This offer is only valid for orders over %(amount)s.')
         raise NotApplicable(msg % {'amount': net(limit)}, limit=limit)
Beispiel #3
0
def price_as_dict(price):
    if not price:
        return None
    return {'currency': price.currency,
            'gross': price.gross,
            'grossLocalized': prices_i18n.gross(price),
            'net': price.net,
            'netLocalized': prices_i18n.net(price)}
Beispiel #4
0
def price_as_dict(price):
    if not price:
        return None
    return {'currency': price.currency,
            'gross': price.gross,
            'grossLocalized': prices_i18n.gross(price),
            'net': price.net,
            'netLocalized': prices_i18n.net(price)}
Beispiel #5
0
def test_voucher_limit_validation(settings, limit, value, valid):
    voucher = Voucher(
        code='unique', type=Voucher.SHIPPING_TYPE,
        discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
        discount_value=Price(10, currency='USD'),
        limit=limit)
    if valid:
        voucher.validate_limit(value)
    else:
        with pytest.raises(NotApplicable) as e:
            voucher.validate_limit(value)
            msg = 'This offer is only valid for orders over %(amount)s.' % {
                'amount': net(limit)}
            assert str(e.value) == msg
def test_voucher_limit_validation(settings, limit, value, valid):
    voucher = Voucher(code='unique',
                      type=Voucher.SHIPPING_TYPE,
                      discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
                      discount_value=Price(10, currency='USD'),
                      limit=limit)
    if valid:
        voucher.validate_limit(value)
    else:
        with pytest.raises(NotApplicable) as e:
            voucher.validate_limit(value)
            msg = 'This offer is only valid for orders over %(amount)s.' % {
                'amount': net(limit)
            }
            assert str(e.value) == msg
Beispiel #7
0
 def resolve_net_localized(self, args, context, info):
     return prices_i18n.net(self)
Beispiel #8
0
def test_normalize_same_as_formatted(value):
    formatted_price = prices_i18n.format_price(value, 'USD', normalize=True)
    net = prices_i18n.net(Price(net=value, currency='USD'))
    assert not formatted_price == net
Beispiel #9
0
def test_templatetag_i18n_net_normalize_with_decimals():
    price = Price(net='12.23', currency='USD')
    net = prices_i18n.net(price, normalize=True)
    assert net == '$12.23'
Beispiel #10
0
def test_templatetag_i18n_net_normalize(price_fixture):
    net = prices_i18n.net(price_fixture, normalize=True)
    assert net == '$10'
def net_in_currency(price, currency):  # noqa
    converted_price = exchange_currency(price, currency)
    return prices_i18n.net(converted_price)
def net_in_currency(price, currency):  # noqa
    converted_price = exchange_currency(price, currency)
    return prices_i18n.net(converted_price)
Beispiel #13
0
 def resolve_net_localized(self, info):
     return prices_i18n.net(self)
Beispiel #14
0
 def resolve_net_localized(self, args, context, info):
     return prices_i18n.net(self)
Beispiel #15
0
 def resolve_net_localized(self, info):
     return prices_i18n.net(self)
Beispiel #16
0
    def get_discount_for_checkout(self, checkout):
        if self.type == Voucher.VALUE_TYPE:
            cart_total = checkout.cart.get_total()
            limit = self.limit if self.limit is not None else cart_total
            if cart_total < limit:
                msg = pgettext(
                    'voucher',
                    'This offer is only valid for orders over %(amount)s.')
                raise NotApplicable(msg % {'amount': net(limit)})
            return self.get_fixed_discount_for(cart_total)

        elif self.type == Voucher.SHIPPING_TYPE:
            if not checkout.is_shipping_required:
                msg = pgettext('voucher',
                               'Your order does not require shipping.')
                raise NotApplicable(msg)
            shipping_method = checkout.shipping_method
            if not shipping_method:
                msg = pgettext('voucher',
                               'Please select a shipping method first.')
                raise NotApplicable(msg)
            if (self.apply_to
                    and shipping_method.country_code != self.apply_to):
                msg = pgettext('voucher',
                               'This offer is only valid in %(country)s.')
                raise NotApplicable(msg %
                                    {'country': self.get_apply_to_display()})
            if self.limit is not None and shipping_method.price > self.limit:
                msg = pgettext(
                    'voucher',
                    'This offer is only valid for shipping over %(amount)s.')
                raise NotApplicable(msg % {'amount': net(self.limit)})
            return self.get_fixed_discount_for(shipping_method.price)

        elif self.type in (Voucher.PRODUCT_TYPE, Voucher.CATEGORY_TYPE):
            if self.type == Voucher.PRODUCT_TYPE:
                prices = list((item[1]
                               for item in get_product_variants_and_prices(
                                   checkout.cart, self.product)))
            else:
                prices = list((item[1]
                               for item in get_category_variants_and_prices(
                                   checkout.cart, self.category)))
            if len(prices) == 0:
                msg = pgettext('voucher',
                               'This offer is only valid for selected items.')
                raise NotApplicable(msg)
            if self.apply_to == Voucher.APPLY_TO_ALL_PRODUCTS:
                discounts = (self.get_fixed_discount_for(price)
                             for price in prices)
                discount_total = sum(
                    (discount.amount for discount in discounts),
                    Price(0, currency=settings.DEFAULT_CURRENCY))
                return FixedDiscount(discount_total, smart_text(self))
            else:
                product_total = sum(
                    prices, Price(0, currency=settings.DEFAULT_CURRENCY))
                return self.get_fixed_discount_for(product_total)

        else:
            raise NotImplementedError('Unknown discount type')
Beispiel #17
0
def test_templatetag_i18n_net(price_fixture):
    net = prices_i18n.net(price_fixture)
    assert net == '$10.00'
Beispiel #18
0
def test_templatetag_i18n_net_html_normalize(price_fixture):
    net = prices_i18n.net(price_fixture, html=True, normalize=True)
    assert net == '<span class="currency">$</span>10'
Beispiel #19
0
    def get_discount_for_checkout(self, checkout):
        if self.type == Voucher.VALUE_TYPE:
            cart_total = checkout.cart.get_total()
            limit = self.limit if self.limit is not None else cart_total
            if cart_total < limit:
                msg = pgettext(
                    'voucher',
                    'This offer is only valid for orders over %(amount)s.')
                raise NotApplicable(msg % {'amount': net(limit)})
            return self.get_fixed_discount_for(cart_total)

        elif self.type == Voucher.SHIPPING_TYPE:
            if not checkout.is_shipping_required:
                msg = pgettext(
                    'voucher', 'Your order does not require shipping.')
                raise NotApplicable(msg)
            shipping_method = checkout.shipping_method
            if not shipping_method:
                msg = pgettext(
                    'voucher', 'Please select a shipping method first.')
                raise NotApplicable(msg)
            if (self.apply_to and
                    shipping_method.country_code != self.apply_to):
                msg = pgettext(
                    'voucher', 'This offer is only valid in %(country)s.')
                raise NotApplicable(msg % {
                    'country': self.get_apply_to_display()})
            if self.limit is not None and shipping_method.price > self.limit:
                msg = pgettext(
                    'voucher',
                    'This offer is only valid for shipping over %(amount)s.')
                raise NotApplicable(msg % {'amount': net(self.limit)})
            return self.get_fixed_discount_for(shipping_method.price)

        elif self.type in (Voucher.PRODUCT_TYPE, Voucher.CATEGORY_TYPE):
            if self.type == Voucher.PRODUCT_TYPE:
                prices = list(
                    (item[1] for item in get_product_variants_and_prices(
                        checkout.cart, self.product)))
            else:
                prices = list(
                    (item[1] for item in get_category_variants_and_prices(
                        checkout.cart, self.category)))
            if len(prices) == 0:
                msg = pgettext(
                    'voucher', 'This offer is only valid for selected items.')
                raise NotApplicable(msg)
            if self.apply_to == Voucher.APPLY_TO_ALL_PRODUCTS:
                discounts = (
                    self.get_fixed_discount_for(price) for price in prices)
                discount_total = sum(
                    (discount.amount for discount in discounts),
                    Price(0, currency=settings.DEFAULT_CURRENCY))
                return FixedDiscount(discount_total, smart_text(self))
            else:
                product_total = sum(
                    prices, Price(0, currency=settings.DEFAULT_CURRENCY))
                return self.get_fixed_discount_for(product_total)

        else:
            raise NotImplementedError('Unknown discount type')
Beispiel #20
0
    def get_discount_for_checkout(self, checkout):
        if self.type == Voucher.VALUE_TYPE:
            cart_total = checkout.cart.get_total()
            limit = self.limit if self.limit is not None else cart_total
            if cart_total < limit:
                msg = pgettext(
                    'voucher',
                    'Este desconto é valido apenas para ofertas acima de %(amount)s.'
                )
                raise NotApplicable(msg % {'amount': net(limit)})
            return self.get_fixed_discount_for(cart_total)

        elif self.type == Voucher.SHIPPING_TYPE:
            if not checkout.is_shipping_required:
                msg = pgettext('voucher', 'Sua ordem não necessita de frete.')
                raise NotApplicable(msg)
            shipping_method = checkout.shipping_method
            if not shipping_method:
                msg = pgettext(
                    'voucher',
                    'Por favor, selecione antes um método de entrega.')
                raise NotApplicable(msg)
            if (self.apply_to
                    and shipping_method.country_code != self.apply_to):
                msg = pgettext(
                    'voucher',
                    'Esta oferta é válida apenas nos países %(country)s.')
                raise NotApplicable(msg %
                                    {'country': self.get_apply_to_display()})
            if self.limit is not None and shipping_method.price > self.limit:
                msg = pgettext(
                    'voucher',
                    'Esta oferta é válida apenas para fretes acima de %(amount)s.'
                )
                raise NotApplicable(msg % {'amount': net(self.limit)})
            return self.get_fixed_discount_for(shipping_method.price)

        elif self.type in (Voucher.PRODUCT_TYPE, Voucher.CATEGORY_TYPE):
            if self.type == Voucher.PRODUCT_TYPE:
                prices = list((item[1]
                               for item in get_product_variants_and_prices(
                                   checkout.cart, self.product)))
            else:
                prices = list((item[1]
                               for item in get_category_variants_and_prices(
                                   checkout.cart, self.category)))
            if len(prices) == 0:
                msg = pgettext(
                    'voucher',
                    'Este desconto é válido apenas para alguns produtos específicos.'
                )
                raise NotApplicable(msg)
            if self.apply_to == Voucher.APPLY_TO_ALL_PRODUCTS:
                discounts = (self.get_fixed_discount_for(price)
                             for price in prices)
                discount_total = sum(
                    (discount.amount for discount in discounts),
                    Price(0, currency=settings.DEFAULT_CURRENCY))
                return FixedDiscount(discount_total, smart_text(self))
            else:
                product_total = sum(
                    prices, Price(0, currency=settings.DEFAULT_CURRENCY))
                return self.get_fixed_discount_for(product_total)

        else:
            raise NotImplementedError('Unknown discount type')