Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
op = DJANGO_MAP[op]
elif value is None:
op = OP.IS
else:
op = OP.EQ
for piece in key.split('__'):
model_attr = getattr(curr, piece)
if isinstance(model_attr, FieldAlias):
actual_model_attr = model_attr.field
else:
actual_model_attr = model_attr
if value is not None and isinstance(actual_model_attr, relationship):
curr = model_attr.rel_model
curr = alias_map.get(curr, curr)
joins.append(model_attr)
accum.append(Expression(model_attr, op, value))
return accum, joins
def __init__(self, lhs, op, rhs, flat=False):
super(Expression, self).__init__()
self.lhs = lhs
self.op = op
self.rhs = rhs
self.flat = flat
def inner(self, rhs):
return Expression(self, op, ArrayValue(self, rhs))
return inner
def match(self, query):
return Expression(self, OP.TS_MATCH, fn.to_tsquery(query))
def match(lhs, rhs):
return Expression(lhs, OP.MATCH, rhs)
def contains_any(self, *items):
return Expression(self, ACONTAINS_ANY, ArrayValue(self, items))
def contains(self, value):
if isinstance(value, dict):
return Expression(self, OP.HCONTAINS_DICT, Param(value))
elif isinstance(value, (list, tuple)):
return Expression(self, OP.HCONTAINS_KEYS, Param(value))
return Expression(self, OP.HCONTAINS_KEY, value)
def has_key(self, key):
return Expression(cast_jsonb(self), JSONB_CONTAINS_KEY, key)
# normalize args and kwargs into a new expression
# Note: This is a modified peewee's Query.filter method.
# Inner methods convert_dict_to_node and ensure_join also changed.
# That is done to support FieldProxy generated from aliases to prevent unnecessary joins (see issue link below).
# https://github.com/coleifer/peewee/issues/1338
if filters:
dq_node = Node() & DQ(**filters)
else:
return query
# dq_node should now be an Expression, lhs = Node(), rhs = ...
q = deque([dq_node])
dq_joins = set()
while q:
curr = q.popleft()
if not isinstance(curr, Expression):
continue
for side, piece in (('lhs', curr.lhs), ('rhs', curr.rhs)):
if isinstance(piece, DQ):
new_query, joins = convert_dict_to_node(query, piece.query, alias_map)
dq_joins.update(joins)
expression = reduce(operator.and_, new_query)
# Apply values from the DQ object.
expression._negated = piece._negated
expression._alias = piece.alias
setattr(curr, side, expression)
else:
q.append(piece)
dq_node = dq_node.rhs
new_query = query.clone()
def contains(self, other):
clone = self.as_json(True)
if isinstance(other, (list, dict)):
return Expression(clone, JSONB_CONTAINS, Json(other))
return Expression(clone, JSONB_EXISTS, other)