Implementing filter and sort in back
This commit is contained in:
@@ -12,55 +12,6 @@ from .schemas import Writer, Reader
|
||||
from ..db import get_db_client
|
||||
|
||||
|
||||
def parse_sort(sort_by):
|
||||
if not sort_by:
|
||||
return []
|
||||
|
||||
fields = []
|
||||
for field in sort_by.split(','):
|
||||
direction, column = field.split('(')
|
||||
fields.append((column[:-1], 1 if direction == 'asc' else -1))
|
||||
|
||||
return fields
|
||||
|
||||
|
||||
def Or(filters):
|
||||
return {'$or': filters}
|
||||
|
||||
|
||||
def parse_query(query: str, model):
|
||||
if query is None:
|
||||
return {}
|
||||
|
||||
and_array = []
|
||||
for criterion in query.split(' AND '):
|
||||
[column, operator, value] = criterion.split(' ', 2)
|
||||
column = column.lower()
|
||||
operand = None
|
||||
if column == 'fulltext':
|
||||
if not model.Settings.fulltext_search:
|
||||
continue
|
||||
|
||||
or_array = []
|
||||
for field in model.Settings.fulltext_search:
|
||||
words_and_array = []
|
||||
for word in value.split(' '):
|
||||
words_and_array.append(RegEx(field, word, 'i'))
|
||||
or_array.append(And(*words_and_array) if len(words_and_array) > 1 else words_and_array[0])
|
||||
operand = Or(or_array) if len(or_array) > 1 else or_array[0]
|
||||
|
||||
elif operator == 'eq':
|
||||
operand = Eq(column, value)
|
||||
elif operator == 'in':
|
||||
operand = In(column, value.split(','))
|
||||
|
||||
if operand:
|
||||
and_array.append(operand)
|
||||
|
||||
if and_array:
|
||||
return And(*and_array) if len(and_array) > 1 else and_array[0]
|
||||
else:
|
||||
return {}
|
||||
#instance: str="westside", firm: str="cht",
|
||||
def get_tenant_db_cursor(db_client=Depends(get_db_client)):
|
||||
instance = "westside"
|
||||
@@ -80,7 +31,7 @@ def get_crud_router(model: CrudDocument, model_create: Writer, model_read: Reade
|
||||
|
||||
@router.get("/", response_model=Page[model_read], response_description=f"{model_name} records retrieved")
|
||||
async def read_list(filters: model_filter=FilterDepends(model_filter), db=Depends(get_logged_tenant_db_cursor)) -> Page[model_read]:
|
||||
return await paginate(model.list(db, filters))
|
||||
return await paginate(**model.find(db, filters))
|
||||
|
||||
@router.post("/", response_description=f"{model_name} added to the database")
|
||||
async def create(schema: model_create, db=Depends(get_logged_tenant_db_cursor)) -> model_read:
|
||||
|
||||
Reference in New Issue
Block a user