def test_order_creator_no_extra(rf): admin_user = get_user_model().objects.create_superuser(email='*****@*****.**', password='******') source = seed_source(admin_user, False) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) creator = ShuupBRBasketOrderCreator() order = creator.create_order(source) assert get_data_dict(source.billing_address) == get_data_dict(order.billing_address) assert get_data_dict(source.shipping_address) == get_data_dict(order.shipping_address) assert hasattr(source.billing_address, 'extra') == False assert hasattr(source.shipping_address, 'extra') == False assert source.customer == order.customer assert source.payment_method == order.payment_method assert source.shipping_method == order.shipping_method assert order.pk
def test_order_creator(rf, admin_user): source = seed_source(admin_user) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) creator = OrderCreator() order = creator.create_order(source) assert get_data_dict(source.billing_address) == get_data_dict(order.billing_address) assert get_data_dict(source.shipping_address) == get_data_dict(order.shipping_address) customer = source.customer assert customer == order.customer assert customer.groups.count() == 1 assert customer.groups.first() == order.customer_groups.first() assert customer.tax_group is not None assert customer.tax_group == order.tax_group assert source.payment_method == order.payment_method assert source.shipping_method == order.shipping_method assert order.pk
def test_get_data_dict_force_value_with_json_serializer(): product = factories.get_default_product() with pytest.raises(TypeError): json.dumps(get_data_dict(product)) json.dumps(get_data_dict(product, force_text_for_value=True))
def test_order_creator(rf, admin_user): source = seed_source(admin_user) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) creator = OrderCreator() order = creator.create_order(source) assert get_data_dict(source.billing_address) == get_data_dict( order.billing_address) assert get_data_dict(source.shipping_address) == get_data_dict( order.shipping_address) customer = source.customer assert customer == order.customer assert customer.groups.count() == 1 assert customer.groups.first() == order.customer_groups.first() assert customer.tax_group is not None assert customer.tax_group == order.tax_group assert source.payment_method == order.payment_method assert source.shipping_method == order.shipping_method assert order.pk
def test_new_mutable_address(): address = get_address() new_mutable = address.to_mutable() # New address should be unsaved assert new_mutable.pk == None assert isinstance(new_mutable, MutableAddress) assert get_data_dict(address).items() == get_data_dict(new_mutable).items()
def get_form(self, form_class=None): order = self.get_object() form_group = FormGroup(**self.get_form_kwargs()) address_form_class = cached_load("SHUUP_ADDRESS_MODEL_FORM") form_group.add_form_def( "billing_address", address_form_class, kwargs={"initial": get_data_dict(order.billing_address) if order.billing_address else {}}) form_group.add_form_def( "shipping_address", address_form_class, kwargs={"initial": get_data_dict(order.shipping_address) if order.shipping_address else {}}) return form_group
def test_immutable_address(): address = get_address() new_immutable = address.to_immutable() # New address should be saved assert new_immutable.pk != None assert isinstance(new_immutable, ImmutableAddress) assert get_data_dict(address).items() == get_data_dict(new_immutable).items() # Taking immutable for same address should return same object assert new_immutable == address.to_immutable() # Taking immutable from new_immutable should return same item assert new_immutable == new_immutable.to_immutable()
def test_order_customer_groups(rf, admin_user): customer = create_random_person() default_group = get_default_customer_group() default_group.members.add(customer) source = seed_source(admin_user) source.customer = customer source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) creator = OrderCreator() order = creator.create_order(source) assert get_data_dict(source.billing_address) == get_data_dict( order.billing_address) assert get_data_dict(source.shipping_address) == get_data_dict( order.shipping_address) customer = source.customer assert customer == order.customer assert customer.groups.count() == 2 assert order.customer_groups.filter(id=default_group.id).exists() with pytest.raises(ProtectedError): default_group.delete() assert customer.tax_group is not None assert customer.tax_group == order.tax_group with pytest.raises(ProtectedError): customer.tax_group.delete() new_group = create_random_contact_group() new_group.members.add(customer) order.phone = "911" order.save() assert order.customer_groups.filter(id=default_group.id).exists() assert not order.customer_groups.filter(id=new_group.id).exists()
def to_mutable(self): """ Get a new MutableAddress from self. :rtype: MutableAddress :return: Fresh unsaved MutableAddress with same data as self. """ data = get_data_dict(self) return MutableAddress.from_data(data)
def to_immutable(self): """ Get or create saved ImmutableAddress from self. :rtype: ImmutableAddress :return: Saved ImmutableAddress with same data as self. """ data = get_data_dict(self) return ImmutableAddress.from_data(data)
def supplier_post_save(sender, object, request, **kwargs): if isinstance(object, Supplier): # TODO: check if extra has changed from last log entry object.add_log_entry( "Supplier saved at admin", user=request.user, identifier=SHUUP_LOGGING_ADMIN_SAVE_LOG_IDENTIFIER, kind=LogEntryKind.EDIT, extra=get_data_dict(object, force_text_for_value=True))
def test_order_customer_groups(rf, admin_user): customer = create_random_person() default_group = get_default_customer_group() default_group.members.add(customer) source = seed_source(admin_user) source.customer=customer source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) creator = OrderCreator() order = creator.create_order(source) assert get_data_dict(source.billing_address) == get_data_dict(order.billing_address) assert get_data_dict(source.shipping_address) == get_data_dict(order.shipping_address) customer = source.customer assert customer == order.customer assert customer.groups.count() == 2 assert order.customer_groups.filter(id=default_group.id).exists() with pytest.raises(ProtectedError): default_group.delete() assert customer.tax_group is not None assert customer.tax_group == order.tax_group with pytest.raises(ProtectedError): customer.tax_group.delete() new_group = create_random_contact_group() new_group.members.add(customer) order.phone = "911" order.save() assert order.customer_groups.filter(id=default_group.id).exists() assert not order.customer_groups.filter(id=new_group.id).exists()
def billing_address(self, value): self._billing_address = value if value: if value.id: self._set_value_to_data("billing_address_id", value.id) self._set_value_to_data("billing_address_data", None) else: from shuup.utils.models import get_data_dict self._set_value_to_data("billing_address_data", get_data_dict(value))
def test_order_creator(rf, admin_user): source = seed_source(admin_user) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line(accounting_identifier="strawberries", type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, extra={"runner": "runner"}) the_line = [ sl for sl in source.get_lines() if sl.accounting_identifier == "strawberries" ] assert the_line[0].data["extra"]["runner"] == "runner" creator = OrderCreator() order = creator.create_order(source) assert get_data_dict(source.billing_address) == get_data_dict( order.billing_address) assert get_data_dict(source.shipping_address) == get_data_dict( order.shipping_address) customer = source.customer assert customer == order.customer assert customer.groups.count() == 1 assert customer.groups.first() == order.customer_groups.first() assert customer.tax_group is not None assert customer.tax_group == order.tax_group assert source.payment_method == order.payment_method assert source.shipping_method == order.shipping_method assert order.pk assert order.lines.filter(accounting_identifier="strawberries").first( ).extra_data["runner"] == "runner"
def from_data(cls, data): """ Get or create immutable address with given data. :param data: data for address :type data: dict[str,str] :return: Saved immutable address :rtype: ImmutableAddress """ # Populate all known address fields even if not originally in `data` data_with_all_fields = get_data_dict(cls(**data)) address = cls.objects.filter(**data_with_all_fields).first() return address if address else cls.objects.create(**data_with_all_fields)
def test_basic_address(): address = get_address() address.full_clean() string_repr = str(address) for field, value in get_data_dict(address).items(): if field == "country": # We can't test this right now, it's formatted in the repr continue if not value: continue assert value in string_repr, "Field %s is not represented in %r" % (field, string_repr) assert address.is_european_union, "Dog Fort, UK is not in the EU, France actually is" assert list(address.split_name) == ["Dog", "Hello"], "Names split correctly" assert address.first_name == "Dog", "Names split correctly" assert address.last_name == "Hello", "Names split correctly" assert address.full_name == "Sir Dog Hello , Esq.", "Names join correctly"
def test_basic_address(): address = get_address() address.full_clean() string_repr = str(address) for field, value in get_data_dict(address).items(): if field == "country": # We can't test this right now, it's formatted in the repr continue if not value: continue assert value in string_repr, "Field %s is not represented in %r" % (field, string_repr) assert address.is_european_union, "Dog Fort, UK is in the EU" assert list(address.split_name) == ["Dog", "Hello"], "Names split correctly" assert address.first_name == "Dog", "Names split correctly" assert address.last_name == "Hello", "Names split correctly" assert address.full_name == "Sir Dog Hello , Esq.", "Names join correctly"
def get(self, request, *args, **kwargs): shop_product = self.get_object() product = shop_product.product product_data = get_data_dict(product) product_data.update( {"sku": "{}-{}".format(product.sku, Product.objects.count())}) new_product = Product.objects.create(**product_data) new_product.name = product.name new_product.short_description = product.short_description new_product.description = product.description new_product.slug = product.slug new_product.save() new_shop_product = ShopProduct.objects.create(product=new_product, shop=shop_product.shop) new_shop_product.visibility = shop_product.visibility new_shop_product.purchasable = shop_product.purchasable new_shop_product.default_price_value = shop_product.default_price_value new_shop_product.primary_category = shop_product.primary_category new_shop_product.save() new_shop_product.suppliers.set(shop_product.suppliers.all()) new_shop_product.categories.set(shop_product.categories.all()) for attribute in product.attributes.all(): ProductAttribute.objects.create(product=new_product, attribute=attribute.attribute, value=attribute.value) for old_media in product.media.all(): media = ProductMedia.objects.create(product=new_product, file=old_media.file, kind=old_media.kind) media.shops.add(shop_product.shop) if product.primary_image == old_media: new_product.primary_image = media new_product.save() product_copied.send(sender=type(self), shop=shop_product.shop, copied=product, copy=new_product) messages.success(request, _("Product %s copy successfull.") % new_product) return HttpResponseRedirect( get_model_url(new_shop_product, shop=self.request.shop))
def test_order_creator(rf, admin_user): source = seed_source(admin_user) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line(accounting_identifier="strawberries", type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, extra={"runner": "runner"}) the_line = [ sl for sl in source.get_lines() if sl.accounting_identifier == "strawberries" ] assert the_line[0].data["extra"]["runner"] == "runner" creator = OrderCreator() order = creator.create_order(source) zero = Money(0, order.currency) taxful_total_price = TaxfulPrice(-50, order.currency) last_price = order.taxful_total_price order.taxful_total_price = taxful_total_price order.save() assert not order.is_paid() assert not order.is_canceled() assert not order.get_total_unpaid_amount() > zero assert order.get_total_unpaid_amount() == zero assert not order.get_total_unpaid_amount() < zero assert not order.can_create_payment() order.taxful_total_price = last_price order.save() assert not order.is_paid() assert not order.is_canceled() assert order.get_total_unpaid_amount() > zero assert not order.get_total_unpaid_amount() == zero assert not order.get_total_unpaid_amount() < zero assert order.can_create_payment() order.set_canceled() assert not order.is_paid() assert order.is_canceled() assert order.get_total_unpaid_amount() > zero assert not order.get_total_unpaid_amount() == zero assert not order.get_total_unpaid_amount() < zero assert not order.can_create_payment() order.create_payment(order.get_total_unpaid_amount()) assert order.is_paid() assert order.is_canceled() assert not order.get_total_unpaid_amount() > zero assert order.get_total_unpaid_amount() == zero assert not order.get_total_unpaid_amount() < zero assert not order.can_create_payment() with pytest.raises(NoPaymentToCreateException): order.create_payment(order.get_total_unpaid_amount()) order.create_payment(order.get_total_unpaid_amount() + Money(10, order.currency)) order.create_payment(order.get_total_unpaid_amount() - Money(10, order.currency)) assert get_data_dict(source.billing_address) == get_data_dict( order.billing_address) assert get_data_dict(source.shipping_address) == get_data_dict( order.shipping_address) customer = source.customer assert customer == order.customer assert customer.groups.count() == 1 assert customer.groups.first() == order.customer_groups.first() assert customer.tax_group is not None assert customer.tax_group == order.tax_group assert source.payment_method == order.payment_method assert source.shipping_method == order.shipping_method assert order.pk assert order.lines.filter(accounting_identifier="strawberries").first( ).extra_data["runner"] == "runner"
def clone_product(self, shop_product: ShopProduct): # clone product product = shop_product.product new_product = copy_model_instance(product) new_product.sku = "{}-{}".format(product.sku, Product.objects.count()) new_product.name = ("{name} - Copy").format(name=product.name) new_product.save() for trans in product.translations.all(): trans_product_data = get_data_dict(trans) trans_product_data["master"] = new_product new_trans = Product._parler_meta.get_model_by_related_name( "translations").objects.get_or_create( language_code=trans.language_code, master=new_product)[0] for (key, value) in trans_product_data.items(): setattr(new_trans, key, value) new_trans.save() # clone shop product new_shop_product = copy_model_instance(shop_product) new_shop_product.product = new_product new_shop_product.save() for trans in shop_product.translations.all(): trans_shop_product_data = get_data_dict(trans) trans_shop_product_data["master"] = new_shop_product ShopProduct._parler_meta.get_model_by_related_name( "translations").objects.get_or_create( **trans_shop_product_data) # clone suppliers if self.current_supplier: new_shop_product.suppliers.add(self.current_supplier) else: new_shop_product.suppliers.set(shop_product.suppliers.all()) new_shop_product.categories.set(shop_product.categories.all()) # clone attributes for original_product_attribute in product.attributes.all(): product_attribute = ProductAttribute.objects.create( product=new_product, attribute=original_product_attribute.attribute, ) product_attribute.value = original_product_attribute.value product_attribute.save() # clone media for media in product.media.all(): media_copy = copy_model_instance(media) media_copy.product = new_product media_copy.file = media.file media.shops.add(shop_product.shop) if product.primary_image == media: new_product.primary_image = media_copy for trans in media.translations.all(): trans_product_media_data = get_data_dict(trans) trans_product_media_data["master"] = new_shop_product ProductMedia._parler_meta.get_model_by_related_name( "translations").objects.create(**trans_product_media_data) media_copy.save() product_copied.send(sender=type(self), shop=shop_product.shop, suppliers=self.current_supplier, copied=product, copy=new_product) return new_shop_product