本文整理汇总了Python中colander.Invalid类的典型用法代码示例。如果您正苦于以下问题:Python Invalid类的具体用法?Python Invalid怎么用?Python Invalid使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Invalid类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validator
def validator(node, value):
rate_validator = registry.getAdapter(value['object'], IRateValidator)
if not rate_validator.validate(value['rate']):
error = Invalid(node, msg='')
msg = rate_validator.helpful_error_message()
error.add(Invalid(node['rate'], msg=msg))
raise error
示例2: convert_types
def convert_types(mapping, row):
""" Translate a row of input data (e.g. from a CSV file) into the
structure understood by the dataset loader, i.e. where all
dimensions are dicts and all types have been converted.
This will validate the incoming data and emit a colander.Invalid
exception if validation was unsuccessful."""
out = {}
errors = Invalid(SchemaNode(Mapping(unknown="preserve")))
for dimension, meta in mapping.items():
meta["dimension"] = dimension
# handle AttributeDimensions, Measures and DateDimensions.
# this is clever, but possibly not always true.
if "column" in meta:
try:
out[dimension] = _cast(row, meta, dimension)
except Invalid, i:
errors.add(i)
# handle CompoundDimensions.
else:
out[dimension] = {}
for attribute, ameta in meta.get("attributes", {}).items():
try:
out[dimension][attribute] = _cast(row, ameta, dimension + "." + attribute)
except Invalid, i:
errors.add(i)
示例3: deserialize
def deserialize(self, field, pstruct):
result = []
error = None
if pstruct is null:
pstruct = []
field.sequence_fields = []
item_field = field.children[0]
for num, substruct in enumerate(pstruct):
subfield = item_field.clone()
try:
subval = subfield.deserialize(substruct)
except Invalid as e:
subval = e.value
if error is None:
error = Invalid(field.schema, value=result)
error.add(e, num)
result.append(subval)
field.sequence_fields.append(subfield)
if error is not None:
raise error
return result
示例4: deserialize
def deserialize(self, field, pstruct):
error = None
result = {}
if pstruct is null:
pstruct = {}
checkbox_schema = pstruct.get(self.checkbox_element, null)
if checkbox_schema is null:
return null
result[self.checkbox_element] = True
for num, subfield in enumerate(field.children):
name = subfield.name
subval = pstruct.get(name, null)
try:
result[name] = subfield.deserialize(subval)
except Invalid as e:
result[name] = e.value
if error is None:
error = Invalid(field.schema, value=result)
error.add(e, num)
if error is not None:
raise error
result[self.checkbox_element] = True
return result
示例5: validate_user_is_active
def validate_user_is_active(node: SchemaNode, value: dict):
user = request.validated.get('user', None)
if user is None:
return
elif not user.active:
error = Invalid(node)
error.add(Invalid(node[child_node_name],
msg='User account not yet activated'))
raise error
示例6: validate_rate_is_unique
def validate_rate_is_unique(node, value):
existing = _get_rates_user_non_anonymized(context, request, value)
existing += _get_rates_user_anonymized(context, request, value)
existing = _remove_following_versions(existing, context, request)
if existing:
error = Invalid(node, msg='')
msg = 'Another rate by the same user already exists'
error.add(Invalid(node['object'], msg=msg))
raise error
示例7: validate_login_password
def validate_login_password(node: SchemaNode, value: dict):
password = value['password']
user = request.validated.get('user', None)
if user is None:
return
valid = user.is_password_valid(registry, password)
if not valid:
error = Invalid(node)
error.add(Invalid(node['password'], msg=error_msg_wrong_login))
raise error
示例8: validate_login_password
def validate_login_password(node: SchemaNode, value: dict):
password = value['password']
user = request.validated.get('user', None)
if user is None:
return
sheet = registry.content.get_sheet(user, IPasswordAuthentication)
valid = sheet.check_plaintext_password(password)
if not valid:
error = Invalid(node)
error.add(Invalid(node['password'], msg=error_msg_wrong_login))
raise error
示例9: validate_login
def validate_login(node: SchemaNode, value: dict):
login = value[child_node_name]
locator = registry.getMultiAdapter((context, request), IUserLocator)
if child_node_name == 'email':
login = login.lower().strip()
user = locator.get_user_by_email(login)
else:
user = locator.get_user_by_login(login)
if user is None:
error = Invalid(node)
error.add(Invalid(node['password'], msg=error_msg_wrong_login))
raise error
else:
request.validated['user'] = user
示例10: validate_entity
def validate_entity(data, schema, context, deep=True):
schema = entity_schema(schema)
inv = Invalid(schema)
try:
data = schema.deserialize(data)
except Invalid as inv_real:
inv = inv_real
if deep:
for direction, attribute in (('incoming', 'source'), ('outgoing', 'target')):
data[direction], i = validate_deep(data, context, direction, attribute)
if len(i.children):
inv.add(i)
if len(inv.children):
raise inv
return data
示例11: _raise_if_unknown_field_value
def _raise_if_unknown_field_value(self, field_name: str,
err: Invalid,
json_object: dict):
"""Raise an 'unknown_xxx' WebSocketError error if appropriate."""
errdict = err.asdict()
if (self._is_only_key(errdict, field_name) and
field_name in json_object):
field_value = json_object[field_name]
raise WebSocketError('unknown_' + field_name, field_value)
示例12: deserialize
def deserialize(self, field, pstruct):
error = None
result = {}
if pstruct is null:
pstruct = {}
for num, subfield in enumerate(field.children):
name = subfield.name
subval = pstruct.get(name, null)
try:
result[name] = subfield.deserialize(subval)
except Invalid, e:
result[name] = e.value
if error is None:
error = Invalid(field.schema, value=result)
error.add(e, num)
示例13: validate_deep
def validate_deep(data, context, direction, attribute):
relations = []
node = sequence(direction)
inv = Invalid(node)
for i, relation_data in enumerate(data.get(direction, [])):
try:
other = relation_data.get(attribute, {})
schema = context.network.get_relation_schema(relation_data.get('type'))
if schema is None:
raise Invalid(node, "Invalid relation type: %e" % relation_data.get('type'))
relation = validate_relation(relation_data, schema, context, ignore_entities=True)
schema = context.network.get_entity_schema(other.get('type'))
if schema is None:
raise Invalid(node, "Invalid entity type: %e" % other.get('type'))
relation[attribute] = validate_entity(other, schema, context, deep=False)
relations.append(relation)
except Invalid as sub:
inv.add(sub, i)
return relations, inv
示例14: _impl
def _impl(self, node, value, callback):
value = self._validate(node, value)
error = None
result = {}
for index, (k, v) in enumerate(value.iteritems()):
key_node = node["key"]
value_node = node["value"].clone()
value_node.name = k
try:
name = callback(key_node, k)
result[name] = callback(value_node, v)
except Invalid as e:
if error is None:
error = Invalid(node)
error.add(e, index)
if error is not None:
raise error
return result
示例15: sequence_impl
def sequence_impl(self, node, value, callback, accept_scalar):
if accept_scalar is None:
accept_scalar = self.accept_scalar
value = self._validate(node, value, accept_scalar)
error = None
result = []
for num, subval in enumerate(value):
try:
result.append(callback(node.children[0], subval))
except Invalid as e:
if error is None:
error = Invalid(node)
error.add(e, num)
if error is not None:
raise error
if not result:
return null
return result