def _initialize_source_from_state(self, state, creator, ip_address, save):
        shop_data = state.pop("shop", None).get("selected", {})
        shop = self.safe_get_first(Shop, pk=shop_data.pop("id", None))
        if not shop:
            self.add_error(ValidationError(_("Please choose a valid shop."), code="no_shop"))
            return None

        source = OrderSource(shop=shop)

        customer_data = state.pop("customer", None)
        billing_address_data = customer_data.pop("billingAddress", {})
        shipping_address_data = (
            billing_address_data
            if customer_data.pop("shipToBillingAddress", False)
            else customer_data.pop("shippingAddress", {}))
        is_company = customer_data.pop("isCompany", False)
        save_address = customer_data.pop("saveAddress", False)
        customer = self.safe_get_first(Contact, pk=customer_data.get("id")) if customer_data else None
        if not customer:
            customer = self._create_contact_from_address(billing_address_data, is_company)
            if not customer:
                return
            if save:
                customer.save()

        billing_address = MutableAddress.from_data(billing_address_data)
        shipping_address = MutableAddress.from_data(shipping_address_data)
        if save:
            billing_address.save()
            shipping_address.save()

        if save and save_address:
            customer.default_billing_address = billing_address
            customer.default_shipping_address = shipping_address
            customer.save()

        methods_data = state.pop("methods", None) or {}
        shipping_method = methods_data.pop("shippingMethod")
        if not shipping_method:
            self.add_error(ValidationError(_("Please select shipping method."), code="no_shipping_method"))

        payment_method = methods_data.pop("paymentMethod")
        if not payment_method:
            self.add_error(ValidationError(_("Please select payment method."), code="no_payment_method"))

        if self.errors:
            return

        source.update(
            creator=creator,
            ip_address=ip_address,
            customer=customer,
            billing_address=billing_address,
            shipping_address=shipping_address,
            status=OrderStatus.objects.get_default_initial(),
            shipping_method=self.safe_get_first(ShippingMethod, pk=shipping_method.get("id")),
            payment_method=self.safe_get_first(PaymentMethod, pk=payment_method.get("id")),
        )
        return source
Beispiel #2
0
def test_stacked_tax_taxful_price():
    shop = get_shop(prices_include_tax=True, currency='EUR')
    source = OrderSource(shop)
    assert source.prices_include_tax
    source.add_line(
        type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(20)
    )
    with override_provides("tax_module", TAX_MODULE_SPEC):
        with override_settings(SHOOP_TAX_MODULE="irvine"):
            source.shipping_address = MutableAddress(
                street="16215 Alton Pkwy",
                postal_code="92602",
            )
            line = source.get_final_lines(with_taxes=True)[0]
            assert isinstance(line, SourceLine)
            assert line.taxes
            assert line.taxful_price == TaxfulPrice(20, 'EUR')
            assert_almost_equal(line.taxless_price, TaxlessPrice("18.518518", 'EUR'))
            source.uncache()

            # Let's move out to a taxless location.
            source.shipping_address.postal_code = "11111"
            line = source.get_final_lines(with_taxes=True)[0]
            assert isinstance(line, SourceLine)
            assert not line.taxes
            assert line.taxful_price == TaxfulPrice(20, source.currency)
            assert line.taxless_price.value == Decimal("20")
def test_home_country_in_address():
    with override("fi"):
        finnish_address = MutableAddress(country="FI")
        with override_settings(SHOOP_ADDRESS_HOME_COUNTRY="US"):
            assert "Suomi" in str(
                finnish_address
            ), "When home is not Finland, Finland appears in address string"
        with override_settings(SHOOP_ADDRESS_HOME_COUNTRY="FI"):
            assert "Suomi" not in str(
                finnish_address
            ), "When home is Finland, Finland does not appear in address string"
Beispiel #4
0
def create_random_address(fake=None, **values):
    if not fake:
        fake = get_faker(["person", "address"])
    empty = str  # i.e. constructor for empty string
    values.setdefault("name", fake.name())
    values.setdefault("street", fake.address())
    values.setdefault("city", fake.city())
    values.setdefault("region", getattr(fake, "state", empty)())
    values.setdefault("country", random.choice(COUNTRY_CODES))
    values.setdefault("postal_code", getattr(fake, "postalcode", empty)())
    address = MutableAddress.from_data(values)
    address.save()
    return address
Beispiel #5
0
def create_random_address(fake=None, **values):
    if not fake:
        fake = get_faker(["person", "address"])
    empty = str  # i.e. constructor for empty string
    values.setdefault("name", fake.name())
    values.setdefault("street", fake.address())
    values.setdefault("city", fake.city())
    values.setdefault("region", getattr(fake, "state", empty)())
    values.setdefault("country", random.choice(COUNTRY_CODES))
    values.setdefault("postal_code", getattr(fake, "postalcode", empty)())
    address = MutableAddress.from_data(values)
    address.save()
    return address
Beispiel #6
0
def test_stacked_tax_taxless_price():
    source = OrderSource(get_shop(prices_include_tax=False))
    assert source.prices_include_tax is False
    source.add_line(
        type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10)
    )
    with override_provides("tax_module", TAX_MODULE_SPEC):
        with override_settings(SHOOP_TAX_MODULE="irvine"):
            source.shipping_address = MutableAddress(
                street="16215 Alton Pkwy",
                postal_code="92602",
            )
            line = source.get_final_lines(with_taxes=True)[0]
            assert isinstance(line, SourceLine)
            assert line.taxes
            assert line.taxful_price.value == Decimal("10.800")
            source.uncache()

            # Let's move out to a taxless location.
            source.shipping_address.postal_code = "11111"
            line = source.get_final_lines(with_taxes=True)[0]
            assert isinstance(line, SourceLine)
            assert not line.taxes
            assert line.taxful_price.value == Decimal("10")
Beispiel #7
0
def get_address(**overrides):
    data = dict(DEFAULT_ADDRESS_DATA, **overrides)
    return MutableAddress.from_data(data)
Beispiel #8
0
    def _initialize_source_from_state(self, state, creator, save):
        shop_data = state.pop("shop", None).get("selected", {})
        shop = self.safe_get_first(Shop, pk=shop_data.pop("id", None))
        if not shop:
            self.add_error(
                ValidationError(_("Please choose a valid shop."),
                                code="no_shop"))
            return None

        source = OrderSource(shop=shop)

        customer_data = state.pop("customer", None)
        billing_address_data = customer_data.pop("billingAddress", {})
        shipping_address_data = (billing_address_data if customer_data.pop(
            "shipToBillingAddress", False) else customer_data.pop(
                "shippingAddress", {}))
        is_company = customer_data.pop("isCompany", False)
        save_address = customer_data.pop("saveAddress", False)
        customer = self.safe_get_first(
            Contact, pk=customer_data.get("id")) if customer_data else None
        if not customer:
            customer = self._create_contact_from_address(
                billing_address_data, is_company)
            if not customer:
                return
            if save:
                customer.save()

        billing_address = MutableAddress.from_data(billing_address_data)
        shipping_address = MutableAddress.from_data(shipping_address_data)
        if save:
            billing_address.save()
            shipping_address.save()

        if save and save_address:
            customer.default_billing_address = billing_address
            customer.default_shipping_address = shipping_address
            customer.save()

        methods_data = state.pop("methods", None) or {}
        shipping_method = methods_data.pop("shippingMethod")
        if not shipping_method:
            self.add_error(
                ValidationError(_("Please select shipping method."),
                                code="no_shipping_method"))

        payment_method = methods_data.pop("paymentMethod")
        if not payment_method:
            self.add_error(
                ValidationError(_("Please select payment method."),
                                code="no_payment_method"))

        if self.errors:
            return

        source.update(
            creator=creator,
            customer=customer,
            billing_address=billing_address,
            shipping_address=shipping_address,
            status=OrderStatus.objects.get_default_initial(),
            shipping_method=self.safe_get_first(ShippingMethod,
                                                pk=shipping_method.get("id")),
            payment_method=self.safe_get_first(PaymentMethod,
                                               pk=payment_method.get("id")),
        )
        return source
Beispiel #9
0
def test_partial_address_fails():
    address = MutableAddress(
        name=u"Dog Hello"
    )
    with pytest.raises(ValidationError):
        address.full_clean()
Beispiel #10
0
def get_address(**overrides):
    data = dict(DEFAULT_ADDRESS_DATA, **overrides)
    return MutableAddress.from_data(data)
def test_partial_address_fails():
    address = MutableAddress(name=u"Dog Hello")
    with pytest.raises(ValidationError):
        address.full_clean()