本文整理匯總了Python中wtforms.ValidationError方法的典型用法代碼示例。如果您正苦於以下問題:Python wtforms.ValidationError方法的具體用法?Python wtforms.ValidationError怎麽用?Python wtforms.ValidationError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類wtforms
的用法示例。
在下文中一共展示了wtforms.ValidationError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: email_validator
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def email_validator():
"""validate email address. Handle both only email and email with name:
- ab@cd.com
- AB CD <ab@cd.com>
"""
message = "Invalid email format. Email must be either email@example.com or *First Last <email@example.com>*"
def _check(form, field):
email = field.data
email = email.strip()
email_part = email
if "<" in email and ">" in email:
if email.find("<") + 1 < email.find(">"):
email_part = email[email.find("<") + 1 : email.find(">")].strip()
if re.match(r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$", email_part):
return
raise ValidationError(message)
return _check
示例2: __call__
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def __call__(self, form, field):
if current_app.testing:
return True
if request.json:
response = request.json.get('g-recaptcha-response', '')
else:
response = request.form.get('g-recaptcha-response', '')
remote_ip = request.remote_addr
if not response:
raise ValidationError(field.gettext(self.message))
if not self._validate_recaptcha(response, remote_ip):
field.recaptcha_error = 'incorrect-captcha-sol'
raise ValidationError(field.gettext(self.message))
示例3: uia_email_mapper
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def uia_email_mapper(identity):
""" Used to match identity as an email.
See :py:data:`SECURITY_USER_IDENTITY_ATTRIBUTES`.
.. versionadded:: 3.4.0
"""
# Fake up enough to invoke the WTforms email validator.
class FakeField:
pass
email_validator = validators.Email(message="nothing")
field = FakeField()
field.data = identity
try:
email_validator(None, field)
except ValidationError:
return None
return identity
示例4: protect
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def protect(self):
if request.method not in current_app.config['WTF_CSRF_METHODS']:
return
try:
validate_csrf(self._get_csrf_token())
except ValidationError as e:
logger.info(e.args[0])
self._error_response(e.args[0])
if request.is_secure and current_app.config['WTF_CSRF_SSL_STRICT']:
if not request.referrer:
self._error_response('The referrer header is missing.')
good_referrer = 'https://{0}/'.format(request.host)
if not same_origin(request.referrer, good_referrer):
self._error_response('The referrer does not match the host.')
g.csrf_valid = True # mark this request as CSRF valid
示例5: on_model_change
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def on_model_change(self, form, model, is_created):
from wtforms import ValidationError
super(FranchisePurchaseOrderAdmin, self).on_model_change(form, model, is_created)
for l in model.lines:
if l.unit_price is None:
l.unit_price = l.product.franchise_price
if current_user.organization.type.code != const.FRANCHISE_STORE_ORG_TYPE_KEY:
raise ValidationError(gettext("Your organization is not a franchise store and is not allowed to create franchise purchase order"))
if current_user.organization.parent is None:
raise ValidationError(gettext("Franchise purchase order creation failed, your organization does not have a valid parent organization"))
if is_created:
model.to_organization = current_user.organization.parent
status = model.status
if status.code == const.PO_ISSUED_STATUS_KEY:
sales_order, incoming, expense = self.create_so_from_fpo(model)
related_value = self.create_related_value(sales_order, model)
db_util.save_objects_commit(sales_order, incoming, expense, related_value)
示例6: validate
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def validate(form, model, object_type="Object ", parent="parent", children="child", is_created=False):
if is_created is False and \
form[parent] is not None and \
form[parent].data is not None and \
form[parent].data.id == model.id:
raise ValidationError(gettext("Can not set %(ot)s's parent to itself[%(data)s]", ot=gettext(object_type), data=model))
if is_created is False and \
form[parent] is not None and \
form[parent].data is not None and \
form[parent].data in getattr(model, children):
raise ValidationError(gettext("Can not set %(ot)s's parent to it's child[%(data)s]", ot=gettext(object_type), data=form[parent].data))
if hasattr(form, children) and \
form[children] is not None and \
form[children].data is not None and \
model in form[children].data:
try:
raise ValidationError(gettext("Can not set %(ot)s's child to itself[%(data)s]", ot=gettext(object_type), data=model))
except BaseException:
raise ValidationError(gettext("Can not set %(ot)s's child to itself", ot=gettext(object_type)))
示例7: test_delete_complete_receiving_not_allowed
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def test_delete_complete_receiving_not_allowed(self):
def test_logic():
from psi.app.models import Receiving, EnumValues
from psi.app.views import ReceivingAdmin
from psi.app.service import Info
receiving = Receiving()
complete_status = EnumValues.get(RECEIVING_COMPLETE_STATUS_KEY)
receiving.status = complete_status
db_session = Info.get_db().session
receiving_admin = ReceivingAdmin(Receiving, db_session, name=lazy_gettext("Receiving"),
category=lazy_gettext('Purchase'), menu_icon_type=ICON_TYPE_GLYPH,
menu_icon_value='glyphicon-import')
self.assertRaises(ValidationError, receiving_admin.on_model_delete, receiving)
run_as_admin(self.test_client, test_logic)
示例8: validate_email
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def validate_email(self, field):
if field.data != self.user.email and \
User.query.filter_by(email=field.data).first():
raise ValidationError('Email already registered.')
示例9: validate_username
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def validate_username(self, field):
if field.data != self.user.username and \
User.query.filter_by(username=field.data).first():
raise ValidationError('Username already in use.')
示例10: validate_email
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def validate_email(self, field):
if User.query.filter_by(email=field.data).first():
raise ValidationError('Email already registered.')
示例11: validate_username
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def validate_username(self, field):
if User.query.filter_by(username=field.data).first():
raise ValidationError('Username already in use.')
示例12: validate_email
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def validate_email(self, field):
if User.query.filter_by(email=field.data).first():
raise ValidationError('郵箱已被注冊。')
示例13: validate_nickname
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def validate_nickname(self, field):
if User.query.filter_by(nickname=field.data).first():
raise ValidationError('昵稱已經存在。')
# 更改密碼表單
示例14: __call__
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def __call__(self, form, field):
message = field.gettext("One or more invalid email address.")
for data in field.data.split(","):
data = data.strip()
if not self.regex.match(data or ""):
raise wtforms.validators.ValidationError(message)
示例15: file_virus_validator
# 需要導入模塊: import wtforms [as 別名]
# 或者: from wtforms import ValidationError [as 別名]
def file_virus_validator(form, field):
""" Checks for virus in the file from flask request object,
raises wtf.ValidationError if virus is found else None. """
if not pagure_config["VIRUS_SCAN_ATTACHMENTS"]:
return
from pyclamd import ClamdUnixSocket
if (
field.name not in flask.request.files
or flask.request.files[field.name].filename == ""
):
# If no file was uploaded, this field is correct
return
uploaded = flask.request.files[field.name]
clam = ClamdUnixSocket()
if not clam.ping():
raise wtforms.ValidationError(
"Unable to communicate with virus scanner"
)
results = clam.scan_stream(uploaded.stream.read())
if results is None:
uploaded.stream.seek(0)
return
else:
result = results.values()
res_type, res_msg = result
if res_type == "FOUND":
raise wtforms.ValidationError("Virus found: %s" % res_msg)
else:
raise wtforms.ValidationError("Error scanning uploaded file")