本文整理汇总了Python中shuup_tests.utils.forms.get_form_data函数的典型用法代码示例。如果您正苦于以下问题:Python get_form_data函数的具体用法?Python get_form_data怎么用?Python get_form_data使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_form_data函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_default_language_finnish
def test_default_language_finnish():
activate("en")
test_name_en = "Test shop"
test_name_fi = "Testi kauppa"
shop = get_default_shop()
shop.name = test_name_en
shop.public_name = test_name_en
shop.save()
shop_form = ShopBaseForm(instance=shop, languages=settings.LANGUAGES)
assert shop_form.languages[0] == "fi"
data = get_form_data(shop_form, prepared=True)
assert data.get("name__en") == test_name_en
assert not data.get("name__fi")
shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES)
shop_form.full_clean()
assert not shop_form.is_valid() and shop_form.errors
data["name__fi"] = test_name_fi
data["public_name__fi"] = test_name_fi
shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES)
shop_form.full_clean()
assert shop_form.is_valid() and not shop_form.errors
shop_form.save()
shop.set_current_language("en")
assert shop.name == test_name_en, "English activated"
shop.set_current_language("fi")
assert shop.name == test_name_fi, "Finnish activated"
示例2: test_protected_fields
def test_protected_fields():
activate("en")
shop = Shop.objects.create(
name="testshop",
identifier="testshop",
status=ShopStatus.ENABLED,
public_name="test shop",
domain="derp",
currency="EUR"
)
get_currency("EUR")
get_currency("USD")
assert shop.name == "testshop"
assert shop.currency == "EUR"
assert not ConfigurationItem.objects.filter(shop=shop, key="languages").exists()
shop_form = ShopBaseForm(instance=shop, languages=settings.LANGUAGES)
assert not shop_form._get_protected_fields() # No protected fields just yet, right?
data = get_form_data(shop_form, prepared=True)
shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES)
_test_cleanliness(shop_form)
shop_form.save()
# Now let's make it protected!
create_product(printable_gibberish(), shop=shop, supplier=get_default_supplier())
order = create_random_order(customer=create_random_person(), shop=shop)
assert order.shop == shop
# And try again...
data["currency"] = "USD"
shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES)
assert shop_form._get_protected_fields() # So protected!
_test_cleanliness(shop_form)
shop = shop_form.save()
assert shop.currency == "EUR" # But the shop form ignored the change . . .
示例3: test_change_shop_price
def test_change_shop_price(form):
product = _get_test_product()
shop = get_default_shop()
group = get_default_customer_group()
price = shop.create_price
form_field = "s_%d_g_%d" % (shop.id, group.id)
frm = form(product=product, shop=shop, empty_permitted=True)
form_data = get_form_data(frm, prepared=True)
if form == CustomerGroupPricingForm:
form_data[form_field] = "4000"
else:
form_data[form_field] = "50"
frm = form(product=product, shop=shop, data=form_data, empty_permitted=True)
frm.full_clean()
frm.save()
if form == CustomerGroupPricingForm:
assert CgpPrice.objects.get(product=product, shop=shop, group=group).price == price(4000)
else:
assert CgpDiscount.objects.get(product=product, shop=shop, group=group).discount_amount == price(50)
# Never mind actually, same price for all shops
form_data[form_field] = ""
frm = form(product=product, shop=shop, data=form_data, empty_permitted=True)
frm.full_clean()
frm.save()
if form == CustomerGroupPricingForm:
assert not CgpPrice.objects.filter(product=product, shop=shop, group=group).exists()
else:
assert not CgpDiscount.objects.filter(product=product, shop=shop, group=group).exists()
示例4: test_model_form_partially_translated
def test_model_form_partially_translated(rf, admin_user):
with override_settings(**{"LANGUAGES": (("en", "en"), ("fi", "fi"), ("ja", "ja")),
"PARLER_DEFAULT_LANGUAGE_CODE": "en"}):
activate("en")
get_default_shop()
request = apply_request_middleware(rf.get("/"), user=admin_user)
test_name_en = "Test shop"
payment_method = get_default_payment_method()
payment_method.name = test_name_en
payment_method.save()
form = PaymentMethodForm(instance=payment_method, request=request, languages=settings.LANGUAGES)
data = get_form_data(form, prepared=True)
assert data.get("name__en") == test_name_en
assert not data.get("name__fi")
form = PaymentMethodForm(data=data, instance=payment_method, request=request, languages=settings.LANGUAGES)
form.full_clean()
assert form.is_valid() and not form.errors
payment_method = form.save()
# Add description for Finnish and and name in Finnish should be required
data["description__fi"] = "Some description"
form = PaymentMethodForm(data=data, instance=payment_method, request=request, languages=settings.LANGUAGES)
form.full_clean()
assert not form.is_valid() and form.errors
test_name_fi = "Some method name in finnish"
data["name__fi"] = test_name_fi
form = PaymentMethodForm(data=data, instance=payment_method, request=request, languages=settings.LANGUAGES)
form.full_clean()
assert form.is_valid() and not form.errors
payment_method = form.save()
assert payment_method.name == test_name_en, "Object in English"
activate("fi")
payment_method.set_current_language("fi")
assert payment_method.name == test_name_fi, "Object in Finnish"
activate("ja")
payment_method.set_current_language("ja")
assert payment_method.name == test_name_en, "Should fallback to English"
# Check that no sneaky translations is not created for Japan
with pytest.raises(ObjectDoesNotExist):
translation = payment_method.get_translation("ja")
translation.refresh_from_db() # Just in case if the translation object comes from cache or something
# Empty finnish translations and see if Finnish starts fallbacks too
data["name__fi"] = data["description__fi"] = ""
form = PaymentMethodForm(data=data, instance=payment_method, request=request, languages=settings.LANGUAGES)
form.full_clean()
assert form.is_valid() and not form.errors
form.save()
# Check that no sneaky translations is not created for Finnish
with pytest.raises(ObjectDoesNotExist):
translation = payment_method.get_translation("fi")
translation.refresh_from_db() # Just in case if the translation object comes from cache or something
示例5: test_no_changes_into_form
def test_no_changes_into_form():
product = _get_test_product()
shop = get_default_shop()
frm = CustomerGroupPricingForm(product=product, empty_permitted=True)
# No changes made, right?
form_data = get_form_data(frm, prepared=True)
frm = CustomerGroupPricingForm(product=product, data=form_data, empty_permitted=True)
frm.full_clean()
frm.save()
assert CgpPrice.objects.get(product=product, shop=shop).price.value == 250
示例6: test_simple_children_formset
def test_simple_children_formset():
FormSet = formset_factory(SimpleVariationChildForm, SimpleVariationChildFormSet, extra=5, can_delete=True)
parent = create_product(printable_gibberish())
child = create_product(printable_gibberish())
# No links yet
formset = FormSet(parent_product=parent)
assert formset.initial_form_count() == 0 # No children yet
# Save a link
data = dict(get_form_data(formset, True), **{"form-0-child": child.pk})
formset = FormSet(parent_product=parent, data=data)
formset.save()
assert parent.variation_children.filter(pk=child.pk).exists() # Got link'd!
# Remove the link
formset = FormSet(parent_product=parent)
assert formset.initial_form_count() == 1 # Got the child here
data = dict(get_form_data(formset, True), **{"form-0-DELETE": "1"})
formset = FormSet(parent_product=parent, data=data)
formset.save()
assert not parent.variation_children.exists() # Got unlinked
示例7: test_contact_group_members_formset
def test_contact_group_members_formset(rf):
FormSet = formset_factory(ContactGroupMembersForm, ContactGroupMembersFormSet, extra=1, can_delete=True)
contact_group = get_default_customer_group()
person = create_random_person()
# No members
formset = FormSet(contact_group=contact_group)
assert formset.initial_form_count() == 0
# Add a member
data = dict(get_form_data(formset, True), **{"form-0-member": person.pk})
formset = FormSet(contact_group=contact_group, data=data)
formset.save()
assert contact_group.members.filter(pk=person.pk).exists()
示例8: test_cannot_add_product_to_own_package
def test_cannot_add_product_to_own_package(rf):
FormSet = formset_factory(PackageChildForm, PackageChildFormSet, extra=5, can_delete=True)
parent = create_product(printable_gibberish())
# No products in the package
formset = FormSet(parent_product=parent)
assert formset.initial_form_count() == 0 # No children yet
assert not parent.get_all_package_children()
# Try to add a product to its own package
data = dict(get_form_data(formset, True), **{"form-0-child": parent.pk, "form-0-quantity": 2})
formset = FormSet(parent_product=parent, data=data)
formset.save()
assert not parent.get_all_package_children()
示例9: test_default_language_english
def test_default_language_english():
activate("en")
test_name_en = "Test shop"
shop = get_default_shop()
shop.name = test_name_en
shop.public_name = test_name_en
shop.save()
shop_form = ShopBaseForm(instance=shop, languages=settings.LANGUAGES)
data = get_form_data(shop_form, prepared=True)
assert data.get("name__en") == test_name_en
assert not data.get("name__fi")
shop_form = ShopBaseForm(data=data, instance=shop, languages=settings.LANGUAGES)
shop_form.full_clean()
assert shop_form.is_valid() and not shop_form.errors
示例10: test_product_attributes_form
def test_product_attributes_form():
# Come up with a decent product...
product = create_product("TestProduct", shop=get_default_shop())
assert product.type.attributes.count()
product.set_attribute_value("bogomips", 6400)
product.set_attribute_value("genre", "Kauhu", "fi")
product.set_attribute_value("genre", "Horror", "en")
product.set_attribute_value("time_to_finish", 12.05)
product.set_attribute_value("release_date", VOGONY_DATE)
assert product.get_attribute_value("bogomips") == 6400
# English is missing on purpose from the languages list; it'll still be available
# for `genre` as it has an extant value.
paf = ProductAttributesForm(product=product, languages=("fi", "sv"), default_language="sv")
assert paf.languages[0] == "sv"
assert compare_partial_dicts(paf.initial, { # Check that things get loaded.
"bogomips": 6400,
"genre__fi": "Kauhu",
"genre__en": "Horror",
"release_date": VOGONY_DATE,
"time_to_finish": Decimal("12.05")
})
form_data = get_form_data(paf)
form_data.update({ # Change, clear and add fields
"genre__sv": "Skräck",
"genre__en": "Terror",
"bogomips": "",
"release_date": "",
"awesome": "True",
"important_datetime": make_aware(datetime.datetime(2000, 1, 1, 1, 2, 3), utc)
})
paf = ProductAttributesForm(product=product, languages=("fi", "sv"), default_language="sv", data=form_data)
paf.full_clean()
assert not paf.errors
paf.save()
for identifier in ("bogomips", "release_date"):
# Value should be gone
assert not product.get_attribute_value(identifier)
# ... and so should the actual product attribute object
assert not ProductAttribute.objects.filter(attribute__identifier=identifier, product=product).exists()
# Those other values got updated, right?
assert product.get_attribute_value("genre", "en") == form_data["genre__en"]
assert product.get_attribute_value("genre", "sv") == form_data["genre__sv"]
assert product.get_attribute_value("awesome") is True
assert str(product.get_attribute_value("important_datetime")) == "2000-01-01 01:02:03+00:00"
示例11: test_product_not_in_normal_mode
def test_product_not_in_normal_mode():
FormSet = formset_factory(PackageChildForm, PackageChildFormSet, extra=5, can_delete=True)
parent = create_product(printable_gibberish())
child_1 = create_product(printable_gibberish())
child_1.link_to_parent(parent)
child_2 = create_product(printable_gibberish())
parent.verify_mode()
assert parent.mode == ProductMode.SIMPLE_VARIATION_PARENT
# Trying to create a package from a non-normal mode product
with pytest.raises(Problem):
formset = FormSet(parent_product=parent)
data = dict(get_form_data(formset, True), **{"form-0-child": child_2.pk, "form-0-quantity": 2})
formset = FormSet(parent_product=parent, data=data)
formset.save()
示例12: test_category_form_saving
def test_category_form_saving(rf):
with transaction.atomic():
category = CategoryFactory()
form_kwargs = dict(instance=category, languages=("sw",), default_language="sw")
form = CategoryBaseForm(**form_kwargs)
assert isinstance(form, CategoryBaseForm)
form_data = get_form_data(form, prepared=True)
for lang, field_map in form.trans_name_map.items():
for dst_field in field_map.values():
form_data[form.add_prefix(dst_field)] = "IJWEHGWOHKSL"
form_kwargs["data"] = form_data
form = CategoryBaseForm(**form_kwargs)
form.full_clean()
form.save()
category = form.instance
category.set_current_language("sw")
assert category.name == "IJWEHGWOHKSL"
示例13: test_address_assigned_to_multiple_times_for_one_contact
def test_address_assigned_to_multiple_times_for_one_contact():
person = create_random_person()
address = create_random_address()
person.default_billing_address = person.default_shipping_address = address
person.save()
assert person.default_billing_address_id == address.id
assert person.default_shipping_address_id == address.id
form = MutableAddressForm(instance=address)
data = get_form_data(form, prepared=True)
form = MutableAddressForm(data=data, instance=address)
form.full_clean()
assert not form.errors
assert form.cleaned_data
form.save()
address.refresh_from_db()
assert person.default_billing_address_id != address.id
assert person.default_shipping_address_id != address.id
示例14: test_package_child_formset
def test_package_child_formset():
FormSet = formset_factory(PackageChildForm, PackageChildFormSet, extra=5, can_delete=True)
parent = create_product(printable_gibberish())
child = create_product(printable_gibberish())
# No products in the package
formset = FormSet(parent_product=parent)
assert formset.initial_form_count() == 0 # No children yet
assert not parent.get_all_package_children()
data = dict(get_form_data(formset, True), **{"form-0-child": child.pk, "form-0-quantity": 2})
formset = FormSet(parent_product=parent, data=data)
formset.save()
assert parent.get_all_package_children()
clear_existing_package(parent)
assert not parent.get_all_package_children()
示例15: test_impossible_simple_variation
def test_impossible_simple_variation():
FormSet = formset_factory(SimpleVariationChildForm, SimpleVariationChildFormSet, extra=5, can_delete=True)
parent = create_product(printable_gibberish())
child = create_product(printable_gibberish())
grandchild = create_product(printable_gibberish())
grandchild.link_to_parent(child)
assert child.variation_children.exists()
formset = FormSet(parent_product=parent)
data = dict(get_form_data(formset, True), **{"form-0-child": child.pk})
formset = FormSet(parent_product=parent, data=data)
assert formset.is_valid() # It's technically valid, but...
with pytest.raises(Problem) as ei:
formset.save()
if six.PY3: # Can only test inner exceptions on Py3. Ah well.
inner_exc = ei.value.__context__
assert isinstance(inner_exc, ImpossibleProductModeException)
assert inner_exc.code == "multilevel"