Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query operators to json api #74

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions safrs/jsonapi_filters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""
JSON:API filtering strategy
"""
import operator
import re

from .config import get_request_param
import sqlalchemy
import safrs
Expand Down Expand Up @@ -54,6 +57,23 @@ def jsonapi_filter(cls):
if expressions:
_expressions = []
for column, val in expressions:
pat1 = r"(?P<op1>[\>\<\=]+)(?P<val1>.+)(?P<bop1>(and)|(or))(?P<op2>[\>\<\=]+)(?P<val2>.+)"
pat2 = r"(?P<op1>[\>\<\=]+)(?P<val1>.+)"
op = {'<': operator.lt, '<=': operator.le, '>': operator.gt, '>=': operator.ge}
try:
m = re.match(pat1, val).groupdict()
op_ = sqlalchemy.or_ if m['bop1'].lower() == 'or' else sqlalchemy.and_
_expressions.append(op_(op[m['op1']](column, m['val1']), op[m['op2']](column, m['val2'])))
continue
except Exception as e:
pass
try:
m = re.match(pat2, val).groupdict()
_expressions.append(op[m['op1']](column, m['val1']))
continue
except Exception as e:
pass

if hasattr(column, "in_"):
_expressions.append(column.in_(val.split(",")))
else:
Expand Down