You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ALLOWED_OPERATOR= ["gt", "gte", "le", "lte"]
OPERATOR_MAP= {
"gt": "__gt__",
"gte": "__ge__",
"lt": "__lt__",
"lte": "__le__",
}
classCompare(R):
""" Django-like query for comparison : - `Compare(field__gt=3)` - `Compare(field__gte=3)` - `Compare(field__lt=3)` - `Compare(field__gte=3)` """def__repr__(self):
return"R({})".format(
", ".join("{}__{}={!r}".format(*k.rsplit("__", 1), v) fork, vinself.kwargs.items())
)
defcheck(self, user, instance=None):
ifinstanceisNone:
returnFalse# This loop exits early, returning False, if any argument# doesn't match.forkey, valueinself.kwargs.items():
# Find the appropriate LHS on this object, traversing# foreign keys if necessary.lhs=instancekey, operator=key.rsplit("__", 1)
ifoperatornotinALLOWED_OPERATOR:
raiseNotImplementedError("Operator must be in %s"%ALLOWED_OPERATOR)
forkey_fragmentinkey.split("__"):
field=lhs.__class__._meta.get_field(
key_fragment,
)
ifisinstance(field, ForeignObjectRel):
attr=field.get_accessor_name()
else:
attr=key_fragmentlhs=getattr(lhs, attr)
# Compare it against the RHS.# Note that the LHS will usually be a value, but in the case# of a ManyToMany or the 'other side' of a ForeignKey it# will be a RelatedManager. In this case, we need to check# if there is at least one model that matches the RHS.ifisinstance(value, Rule):
raiseNotImplementedError("value can not be a Rule")
# if isinstance(value, Rule):# if isinstance(lhs, Manager):# if not value.filter(user, lhs.all()).exists():# return False# else:# if not value.check(user, lhs):# return False# else:resolved_value=value(user) ifcallable(value) elsevalueifisinstance(lhs, Manager):
raiseNotImplementedError("lhs cannot be a Manager")
# if resolved_value not in lhs.all():# return Falseelse:
ifnotgetattr(lhs, OPERATOR_MAP[operator])(resolved_value):
returnFalse# Woohoo, everything matches!returnTrue
The text was updated successfully, but these errors were encountered:
Hello!
It would be nice to have comparison and not strict equality between lhs and value. For now, we can do:
but we can not do
and that would be terrific!
This would allow stuff like:
Are you interested in a PR?
The PR would look like this (adapted from
R
):The text was updated successfully, but these errors were encountered: