本文整理汇总了Python中postal.library.form_factory函数的典型用法代码示例。如果您正苦于以下问题:Python form_factory函数的具体用法?Python form_factory怎么用?Python form_factory使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了form_factory函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: address_inline
def address_inline(request, prefix, form):
"""displays the invoice address with localized fields
"""
template_name = "lfs/customer/" + prefix + "_address_inline.html"
country_code = get_country_code(request, prefix)
if country_code != '':
shop = lfs.core.utils.get_default_shop(request)
countries = None
if prefix == INVOICE_PREFIX:
countries = shop.invoice_countries.all()
else:
countries = shop.shipping_countries.all()
customer = customer_utils.get_or_create_customer(request)
address_form_class = form_factory(country_code)
if request.method == 'POST':
if POSTAL_ADDRESS_L10N == True:
address_form = address_form_class(prefix=prefix, data=request.POST,)
else:
address_form = PostalAddressForm(prefix=prefix, data=request.POST,)
if countries is not None:
address_form.fields["country"].choices = [(c.code.upper(), c.name) for c in countries]
else:
# If there are addresses intialize the form.
initial = {}
customer_selected_address = None
if hasattr(customer, 'selected_' + prefix + '_address'):
customer_selected_address = getattr(customer, 'selected_' + prefix + '_address')
if customer_selected_address is not None:
initial.update({
"line1": customer_selected_address.line1,
"line2": customer_selected_address.line2,
"city" : customer_selected_address.city,
"state": customer_selected_address.state,
"code": customer_selected_address.zip_code,
"country": customer_selected_address.country.code.upper(),
})
address_form = address_form_class(prefix=prefix, initial=initial)
else:
address_form = address_form_class(prefix=prefix)
address_form.fields["country"].initial = country_code
if countries is not None:
address_form.fields["country"].choices = [(c.code.upper(), c.name) for c in countries]
# Removes fields from address form if requested via settings.
for i in range(1, 6):
address_settings = getattr(settings, "POSTAL_ADDRESS_LINE%s" % i, None)
try:
if address_settings and address_settings[2] == False:
del address_form.fields["line%s" % i]
except IndexError:
pass
# if request via ajax don't display validity errors
if request.is_ajax():
address_form._errors = {}
return render_to_string(template_name, RequestContext(request, {
"address_form": address_form,
"form": form,
"settings": settings,
}))
示例2: test_get_mx_address
def test_get_mx_address(self):
"""
Tests that we get the correct widget for Mexico
"""
mx_form_class = form_factory("mx")
self.assertNotEqual(mx_form_class, None)
# only use required fields
test_data = {
'line1': 'Avenida Reforma',
'line2': '1110',
'line3': 'Centro',
'city': 'Puebla',
'state': 'Puebla',
'code': '12345'
}
form = mx_form_class(data=test_data)
self.assertEqual(form.fields['line1'].label.lower(), 'street')
self.assertEqual(form.fields['line2'].label.lower(), 'number')
self.assertEqual(form.fields['city'].label.lower(), 'city')
self.assertEqual(form.fields['state'].label.lower(), 'state')
self.assertEqual(form.fields['code'].label.lower(), 'zip code')
from localflavor.mx.forms import MXStateSelect, MXZipCodeField
self.assertIsInstance(form.fields['state'].widget, MXStateSelect)
self.assertIsInstance(form.fields['code'], MXZipCodeField)
示例3: address_inline
def address_inline(request, prefix="", country_code=None, template_name="postal/form.html"):
""" Displays postal address with localized fields """
country_prefix = "country"
prefix = request.POST.get('prefix', prefix)
if prefix:
country_prefix = prefix + '-country'
country_code = request.POST.get(country_prefix, country_code)
form_class = form_factory(country_code=country_code)
if request.method == "POST":
data = {}
for (key, val) in request.POST.items():
if val is not None and len(val) > 0:
data[key] = val
data.update({country_prefix: country_code})
form = form_class(prefix=prefix, initial=data)
else:
form = form_class(prefix=prefix)
return render_to_string(template_name, RequestContext(request, {
"form": form,
"prefix": prefix,
}))
示例4: test_checkout_with_4_line_shipping_address
def test_checkout_with_4_line_shipping_address(self):
# login as our customer
logged_in = self.client.login(username=self.username, password=self.password)
self.assertEqual(logged_in, True)
# test that our Netherlands form has only 4 address line fields
nl_form_class = form_factory("NL")
nl_form = nl_form_class()
self.assertEqual('state' in nl_form.fields, False)
self.assertEqual('code' in nl_form.fields, True)
# check initial database quantities
self.assertEquals(Address.objects.count(), 2)
self.assertEquals(Customer.objects.count(), 1)
self.assertEquals(Order.objects.count(), 0)
# check we have no invoice or shipping phone or email prior to checkout
our_customer = Customer.objects.all()[0]
self.assertEqual(our_customer.selected_invoice_address.phone, None)
self.assertEqual(our_customer.selected_invoice_address.email, None)
self.assertEqual(our_customer.selected_shipping_address.phone, None)
self.assertEqual(our_customer.selected_shipping_address.email, None)
checkout_data = {'invoice-firstname': 'bob',
'invoice-lastname': 'builder',
'invoice-line1': 'de company',
'invoice-line2': 'de street',
'invoice-city': 'de area',
'invoice-state': 'de town',
'invoice-code': '1234AB',
'invoice-country': "NL",
'invoice-email': '[email protected]',
'invoice-phone': '1234567',
'shipping-firstname': 'hans',
'shipping-lastname': 'schmidt',
'shipping-line1': 'orianenberger strasse',
'shipping-line2': 'de town',
'shipping-city': 'stuff',
'shipping-state': 'BE',
'shipping-code': '1234AB',
'shipping-country': "NL",
'shipping-email': '[email protected]',
'shipping-phone': '7654321',
'payment_method': self.by_invoice.id,
}
checkout_post_response = self.client.post(reverse('lfs_checkout'), checkout_data)
self.assertRedirects(checkout_post_response, reverse('lfs_thank_you'), status_code=302, target_status_code=200,)
# check database quantities post-checkout
self.assertEquals(Address.objects.count(), 4)
self.assertEquals(Customer.objects.count(), 1)
self.assertEquals(Order.objects.count(), 1)
# check our customer details post checkout
our_customer = Customer.objects.all()[0]
self.assertEqual(our_customer.selected_invoice_address.phone, "1234567")
self.assertEqual(our_customer.selected_invoice_address.email, "[email protected]")
self.assertEqual(our_customer.selected_shipping_address.phone, '7654321')
self.assertEqual(our_customer.selected_shipping_address.email, "[email protected]")
示例5: is_valid
def is_valid(self):
"""
Returns True if the postal and the additional form is valid.
"""
if self.type == "shipping" and self.data.get("no_shipping"):
return True
if self.data:
form_model = form_factory("%s-country" % self.type)
else:
form_model = form_factory(self.address.country.code.upper())
postal_form = form_model(data=self.data, initial=self.get_address_as_dict(), prefix=self.type)
address_form_model = self.get_form_model()
address_form = address_form_model(data=self.data, instance=self.address, prefix=self.type)
return postal_form.is_valid() and address_form.is_valid()
示例6: is_valid
def is_valid(self):
"""
Returns True if the postal and the additional form is valid.
"""
if self.type == CHECKOUT_NOT_REQUIRED_ADDRESS and self.data.get("no_%s" % CHECKOUT_NOT_REQUIRED_ADDRESS):
return True
if self.data:
form_model = form_factory(self.data.get("%s-country" % self.type, self.address.country.code.upper()))
else:
form_model = form_factory(self.address.country.code.upper())
postal_form = form_model(data=self.data, initial=self.get_address_as_dict(), prefix=self.type)
address_form_model = self.get_form_model()
address_form = address_form_model(data=self.data, instance=self.address, prefix=self.type)
return postal_form.is_valid() and address_form.is_valid()
示例7: test_4_line_address
def test_4_line_address(self):
netherlands_form_class = form_factory("nl")
self.assertNotEqual(netherlands_form_class, None)
test_data = {'code': '1234AB'}
form = netherlands_form_class(data=test_data)
self.assertEqual(form.fields['line1'].label.lower(), "street")
self.assertEqual(form.fields['line2'].label.lower(), "area")
self.assertEqual(form.fields['city'].label.lower(), "town/city")
self.assertEqual(form.fields.get('state'), None)
self.assertEqual(form.fields['code'].label.lower(), "zip code")
示例8: read
def read(self, request):
iso_code = request.GET.get('country', '')
json = {}
form_class = form_factory(country_code=iso_code)
form_obj = form_class()
for k, v in form_obj.fields.items():
if k not in json.keys():
json[k] = {}
json[k]['label'] = unicode(v.label)
json[k]['widget'] = v.widget.render(k, "", attrs={'id': 'id_' + k})
return json
示例9: test_incorrect_country_code
def test_incorrect_country_code(self):
"""
Tests that we don't throw an exception for an incorrect country code
"""
no_country_form_class = form_factory("xx")
self.assertNotEqual(no_country_form_class, None)
form = no_country_form_class()
self.assertEqual(form.fields['line1'].label.lower(), "street")
self.assertEqual(form.fields['line2'].label.lower(), "area")
self.assertEqual(form.fields['city'].label.lower(), "city")
self.assertEqual(form.fields['state'].label.lower(), "state")
self.assertEqual(form.fields['code'].label.lower(), "zip code")
示例10: test_get_de_address
def test_get_de_address(self):
"""
Tests that we get the correct widget for Germny
"""
german_form_class = form_factory("de")
self.assertNotEqual(german_form_class, None)
# only use required fields
test_data = {'code': '12345',}
form = german_form_class(data=test_data)
self.assertEqual(form.fields['line1'].label.lower(), "street")
self.assertEqual(form.fields.has_key('line2'), False)
self.assertEqual(form.fields['city'].label.lower(), "city")
self.assertEqual(form.fields['code'].label.lower(), "zip code")
示例11: test_get_ie_address
def test_get_ie_address(self):
"""
Tests that we get the correct widget for Ireland
"""
irish_form_class = form_factory("ie")
self.assertNotEqual(irish_form_class, None)
# only use required fields
test_data = {'line1': 'street', 'city': 'Tullamore',
'state': 'offaly', }
form = irish_form_class(data=test_data)
self.assertEqual(form.fields['line1'].label.lower(), "street")
self.assertEqual(form.fields['line2'].label.lower(), "area")
self.assertEqual(form.fields['city'].label.lower(), "town/city")
self.assertEqual(form.fields['state'].label.lower(), "county")
示例12: test_get_co_address
def test_get_co_address(self):
"""
Tests that we get the correct widget for Colombia
"""
co_form_class = form_factory("co")
self.assertNotEqual(co_form_class, None)
test_data = {
'line1': 'Diagonal 25 G',
'line2': '#95 a 55',
'state': 'Bogota D.C.',
}
form = co_form_class(data=test_data)
self.assertEqual(form.fields['line1'].label.lower(), "street")
self.assertEqual(form.fields['line2'].label.lower(), "number")
self.assertEqual(form.fields['city'].label.lower(), "city")
self.assertEqual(form.fields['state'].label.lower(), "department")
self.assertIsInstance(form.fields['code'].widget, forms.HiddenInput)
示例13: test_no_localisation
def test_no_localisation(self):
postal.settings.POSTAL_ADDRESS_L10N = False
postal.settings.POSTAL_ADDRESS_LINE1 = ('a', False)
postal.settings.POSTAL_ADDRESS_LINE2 = ('b', False)
postal.settings.POSTAL_ADDRESS_CITY = ('c', False)
postal.settings.POSTAL_ADDRESS_STATE = ('d', False)
postal.settings.POSTAL_ADDRESS_CODE = ('e', False)
reload(postal.forms)
reload(postal.library)
noloc_form_class = form_factory("nl")
self.assertNotEqual(noloc_form_class, None)
test_data = {'code': '1234AB'}
form = noloc_form_class(data=test_data)
self.assertEqual(form.fields['line1'].label, "a")
self.assertEqual(form.fields['line2'].label, "b")
self.assertEqual(form.fields['city'].label, "c")
self.assertEqual(form.fields['state'].label, 'd')
self.assertEqual(form.fields['code'].label, "e")
示例14: test_get_it_address
def test_get_it_address(self):
"""
Tests that we get the correct widget for Italy
"""
italian_form_class = form_factory("it")
self.assertNotEqual(italian_form_class, None)
# only use required fields
test_data = {
'street': 'Piazza Duomo',
'code': '20100',
'city': 'Milano',
'state': 'MI'
}
form = italian_form_class(data=test_data)
self.assertEqual(form.fields['line1'].label.lower(), "street")
self.assertEqual(form.fields['line2'].label.lower(), "area")
self.assertEqual(form.fields['state'].label.lower(), "province")
self.assertEqual(form.fields['city'].label.lower(), "city")
self.assertEqual(form.fields['code'].label.lower(), "zip code")
示例15: test_get_ar_address
def test_get_ar_address(self):
"""
Tests that we get the correct widget for Argentina
"""
form_class = form_factory("ar")
self.assertNotEqual(form_class, None)
# only use required fields
test_data = {
'line1': 'Maipu',
'line2': '270',
'city': 'Ciudad de Buenos Aires',
'state': 'B',
'code': 'C1006ACT',
}
form = form_class(data=test_data)
self.assertEqual(form.fields['line1'].label.lower(), "street")
self.assertEqual(form.fields['line2'].label.lower(), "number")
self.assertEqual(form.fields['city'].label.lower(), "city")
self.assertEqual(form.fields['code'].label.lower(), "zip code")