Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def validate(self, node):
"""
Validate DjangoQL AST tree vs. current schema
"""
assert isinstance(node, Node)
if isinstance(node.operator, Logical):
self.validate(node.left)
self.validate(node.right)
return
assert isinstance(node.left, Name)
assert isinstance(node.operator, Comparison)
assert isinstance(node.right, (Const, List))
# Check that field and value types are compatible
field = self.resolve_name(node.left)
value = node.right.value
if field is None:
if value is not None:
raise DjangoQLSchemaError(
'Related model %s can be compared to None only, but not to '
'%s' % (node.left.value, type(value).__name__)
)
def build_filter(expr, schema_instance):
if isinstance(expr.operator, Logical):
left = build_filter(expr.left, schema_instance)
right = build_filter(expr.right, schema_instance)
if expr.operator.operator == 'or':
return left | right
else:
return left & right
field = schema_instance.resolve_name(expr.left)
if not field:
# That must be a reference to a model without specifying a field.
# Let's construct an abstract lookup field for it
field = DjangoQLField(
name=expr.left.parts[-1],
nullable=True,
)
return field.get_lookup(