本文整理汇总了Python中bedrock.newsletter.forms.NewsletterFooterForm.is_valid方法的典型用法代码示例。如果您正苦于以下问题:Python NewsletterFooterForm.is_valid方法的具体用法?Python NewsletterFooterForm.is_valid怎么用?Python NewsletterFooterForm.is_valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bedrock.newsletter.forms.NewsletterFooterForm
的用法示例。
在下文中一共展示了NewsletterFooterForm.is_valid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_invalid_newsletter_is_error
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def test_invalid_newsletter_is_error(self):
"""Invalid newsletter should not raise exception. Bug 1072302.
Instead, an invalid newsletter name should manifest as a normal
form error.
"""
data = {
'email': '[email protected]',
'lang': 'fr',
'privacy': True,
'fmt': 'H',
'newsletters': '',
}
form = NewsletterFooterForm('', locale='en-US', data=data.copy())
self.assertFalse(form.is_valid())
self.assertIn('newsletters', form.errors)
self.assertEqual(form.errors['newsletters'], [u'This field is required.'])
invalid_newsletter = '[email protected]'
data = {
'email': '[email protected]',
'lang': 'fr',
'privacy': True,
'fmt': 'H',
'newsletters': invalid_newsletter,
}
form = NewsletterFooterForm(invalid_newsletter, locale='en-US', data=data.copy())
self.assertFalse(form.is_valid())
self.assertIn('newsletters', form.errors)
self.assertEqual(form.errors['newsletters'], [u'Invalid Newsletter'])
示例2: test_multiple_newsletters
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def test_multiple_newsletters(self):
"""Should allow to subscribe to multiple newsletters at a time."""
newsletters = "mozilla-and-you,beta"
data = {"email": "[email protected]", "lang": "en", "privacy": "Y", "fmt": "H", "newsletters": newsletters}
form = NewsletterFooterForm(newsletters, "en-US", data=data.copy())
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data["newsletters"], newsletters)
# whitespace shouldn't matter
form = NewsletterFooterForm("mozilla-and-you , beta ", "en-US", data=data.copy())
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data["newsletters"], newsletters)
示例3: test_multiple_newsletters
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def test_multiple_newsletters(self):
"""Should allow to subscribe to multiple newsletters at a time."""
data = {
'email': '[email protected]',
'lang': 'en',
'privacy': 'Y',
'fmt': 'H',
}
form = NewsletterFooterForm('mozilla-and-you,beta', 'en-US', data=data.copy())
self.assertTrue(form.is_valid())
self.assertEqual(form.newsletters, 'mozilla-and-you,beta')
# whitespace shouldn't matter
form = NewsletterFooterForm('mozilla-and-you , beta ', 'en-US', data=data.copy())
self.assertTrue(form.is_valid())
self.assertEqual(form.newsletters, 'mozilla-and-you,beta')
示例4: process_request
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def process_request(self, request):
success = False
is_footer_form = (request.method == 'POST' and
'newsletter-footer' in request.POST)
if is_footer_form:
form = NewsletterFooterForm(request.locale, request.POST or None)
if form.is_valid():
data = form.cleaned_data
kwargs = {
'format': data['fmt'],
}
# add optional data
kwargs.update(dict((k, data[k]) for k in ['country',
'lang',
'source_url']
if data[k]))
result = newsletter_subscribe(data['email'])
request.newsletter_subscriber = data['email']
request.new_subscription = result
success = True
request.newsletter_form = form
else:
request.newsletter_form = NewsletterFooterForm(request.locale, None)
request.newsletter_success = success
示例5: process_request
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def process_request(self, request):
success = False
form = NewsletterFooterForm(request.locale, request.POST or None)
is_footer_form = (request.method == 'POST' and
'newsletter-footer' in request.POST)
if is_footer_form:
if form.is_valid():
data = form.cleaned_data
kwargs = {
'format': data['fmt'],
}
# add optional data
kwargs.update(dict((k, data[k]) for k in ['country',
'lang',
'source_url']
if data[k]))
try:
basket.subscribe(data['email'], data['newsletter'],
**kwargs)
success = True
except basket.BasketException:
msg = _lazy("We are sorry, but there was a problem "
"with our system. Please try again later!")
form.errors['__all__'] = form.error_class([msg])
request.newsletter_form = form
request.newsletter_success = success
示例6: one_newsletter_subscribe
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def one_newsletter_subscribe(request, template_name, target=None):
success = False
# not in a footer, but we use the same form
form = NewsletterFooterForm(request.locale, request.POST or None)
if form.is_valid():
data = form.cleaned_data
request.newsletter_lang = data.get('lang', 'en') or 'en'
kwargs = {
'format': data['fmt'],
}
# add optional data
kwargs.update(dict((k, data[k]) for k in ['country',
'lang',
'source_url']
if data[k]))
newsletter_subscribe(data['email'])
success = True
request.newsletter_form = form
request.newsletter_success = success
return l10n_utils.render(request,
template_name,
{'target': target})
示例7: contribute
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def contribute(request, template, return_to_form):
newsletter_id = 'about-mozilla'
has_contribute_form = (request.method == 'POST' and
'contribute-form' in request.POST)
has_newsletter_form = (request.method == 'POST' and
'newsletter-form' in request.POST)
locale = getattr(request, 'locale', 'en-US')
contribute_success = False
newsletter_success = False
# This is ugly, but we need to handle two forms. I would love if
# these forms could post to separate pages and get redirected
# back, but we're forced to keep the error/success workflow on the
# same page. Please change this.
if has_contribute_form:
form = ContributeForm(request.POST)
contribute_success = email_contribute.handle_form(request, form)
if contribute_success:
# If form was submitted successfully, return a new, empty
# one.
form = ContributeForm()
else:
form = ContributeForm()
if has_newsletter_form:
newsletter_form = NewsletterFooterForm(newsletter_id, locale,
request.POST,
prefix='newsletter')
if newsletter_form.is_valid():
data = newsletter_form.cleaned_data
try:
basket.subscribe(data['email'],
newsletter_id,
format=data['fmt'],
country=data['country'])
newsletter_success = True
except basket.BasketException:
msg = newsletter_form.error_class(
[_('We apologize, but an error occurred in our system. '
'Please try again later.')]
)
newsletter_form.errors['__all__'] = msg
else:
newsletter_form = NewsletterFooterForm(newsletter_id, locale, prefix='newsletter')
return l10n_utils.render(request,
template,
{'form': form,
'contribute_success': contribute_success,
'newsletter_form': newsletter_form,
'newsletter_success': newsletter_success,
'return_to_form': return_to_form,
'hide_form': hide_contrib_form(request.locale),
'has_moz15': locale in settings.LOCALES_WITH_MOZ15})
示例8: test_lang_not_required
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def test_lang_not_required(self):
"""lang not required since field not always displayed"""
data = {"email": "[email protected]", "privacy": True, "fmt": "H", "newsletters": self.newsletter_name}
form = NewsletterFooterForm(self.newsletter_name, locale="en-US", data=data.copy())
self.assertTrue(form.is_valid(), form.errors)
# Form returns '' for lang, so we don't accidentally change the user's
# preferred language thinking they entered something here that they
# didn't.
self.assertEqual(u"", form.cleaned_data["lang"])
示例9: test_privacy_required
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def test_privacy_required(self):
"""they have to check the privacy box"""
data = {
'email': '[email protected]',
'privacy': False,
'fmt': 'H',
'newsletters': self.newsletter_name,
}
form = NewsletterFooterForm(self.newsletter_name, locale='en-US', data=data)
self.assertFalse(form.is_valid())
self.assertIn('privacy', form.errors)
示例10: test_form
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def test_form(self):
"""Form works normally"""
data = {
'email': '[email protected]',
'lang': 'fr',
'privacy': True,
'fmt': 'H',
}
form = NewsletterFooterForm(self.newsletter_name, locale='en-US', data=data)
self.assertTrue(form.is_valid(), form.errors)
cleaned_data = form.cleaned_data
self.assertEqual(data['fmt'], cleaned_data['fmt'])
self.assertEqual(data['lang'], cleaned_data['lang'])
示例11: test_lang_not_required
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def test_lang_not_required(self):
"""lang not required since field not always displayed"""
data = {
'email': '[email protected]',
'privacy': True,
'fmt': 'H',
}
form = NewsletterFooterForm(self.newsletter_name, locale='en-US', data=data)
self.assertTrue(form.is_valid(), form.errors)
# Form returns '' for lang, so we don't accidentally change the user's
# preferred language thinking they entered something here that they
# didn't.
self.assertEqual(u'', form.cleaned_data['lang'])
示例12: test_form
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def test_form(self):
"""Form works normally"""
data = {
"email": "[email protected]",
"lang": "fr",
"privacy": True,
"fmt": "H",
"newsletters": self.newsletter_name,
}
form = NewsletterFooterForm(self.newsletter_name, locale="en-US", data=data.copy())
self.assertTrue(form.is_valid(), form.errors)
cleaned_data = form.cleaned_data
self.assertEqual(data["fmt"], cleaned_data["fmt"])
self.assertEqual(data["lang"], cleaned_data["lang"])
示例13: email_newsletter_form
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def email_newsletter_form(ctx, newsletters='mozilla-and-you', title=None,
include_country=True, include_language=True,
use_thankyou=True, footer=True, process_form=True):
request = ctx['request']
context = ctx.get_all()
context.update(dict(
id=newsletters,
title=title,
include_country=include_country,
include_language=include_language,
use_thankyou=use_thankyou,
footer=footer,
))
success = False
form = NewsletterFooterForm(newsletters, get_locale(request),
request.POST or None)
if process_form and request.method == 'POST':
if form.is_valid():
data = form.cleaned_data
# If data['lang'] is set, pass it to the template.
# If it's None, empty, or nonexistent, pass 'en'.
context['lang'] = data.get('lang', 'en').strip() or 'en'
kwargs = {'format': data['fmt']}
# add optional data
kwargs.update(dict((k, data[k]) for k in ['country',
'lang',
'source_url']
if data[k]))
try:
basket.subscribe(data['email'], form.newsletters,
**kwargs)
except basket.BasketException as e:
if e.code == basket.errors.BASKET_INVALID_EMAIL:
form.errors['email'] = form.error_class([invalid_email_address])
else:
log.exception("Error subscribing %s to newsletter %s" %
(data['email'], form.newsletters))
form.errors['__all__'] = form.error_class([general_error])
else:
success = True
request.newsletter_success = success
context.update(dict(form=form, success=success))
html = jingo.render_to_string(request, 'newsletter/includes/form.html', context)
if not (success and not use_thankyou):
return jinja2.Markup(html)
示例14: test_invalid_newsletter_is_error
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def test_invalid_newsletter_is_error(self):
"""Invalid newsletter should not raise exception. Bug 1072302.
Instead, an invalid newsletter name should manifest as a normal
form error.
"""
data = {"email": "[email protected]", "lang": "fr", "privacy": True, "fmt": "H", "newsletters": ""}
form = NewsletterFooterForm("", locale="en-US", data=data.copy())
self.assertFalse(form.is_valid())
self.assertIn("newsletters", form.errors)
self.assertEqual(form.errors["newsletters"], [u"This field is required."])
invalid_newsletter = "[email protected]"
data = {
"email": "[email protected]",
"lang": "fr",
"privacy": True,
"fmt": "H",
"newsletters": invalid_newsletter,
}
form = NewsletterFooterForm(invalid_newsletter, locale="en-US", data=data.copy())
self.assertFalse(form.is_valid())
self.assertIn("newsletters", form.errors)
self.assertEqual(form.errors["newsletters"], [u"Invalid Newsletter"])
示例15: test_source_url_too_long
# 需要导入模块: from bedrock.newsletter.forms import NewsletterFooterForm [as 别名]
# 或者: from bedrock.newsletter.forms.NewsletterFooterForm import is_valid [as 别名]
def test_source_url_too_long(self):
"""Form works normally"""
data = {
'email': '[email protected]',
'lang': 'fr',
'first_name': 'Walter',
'last_name': 'Sobchak',
'privacy': True,
'fmt': 'H',
'source_url': 'about:devtools' * 20,
'newsletters': self.newsletter_name,
}
form = NewsletterFooterForm(self.newsletter_name, locale='en-US', data=data.copy())
self.assertTrue(form.is_valid(), form.errors)
cleaned_data = form.cleaned_data
self.assertEqual(data['source_url'][:255], cleaned_data['source_url'])