Change lot view's filter for search

This commit is contained in:
Xavier Bustamante Talavera 2018-10-08 17:51:51 +02:00
parent df31074775
commit 923ed8af68
2 changed files with 6 additions and 9 deletions

View file

@ -6,7 +6,6 @@ from typing import List, Set
import marshmallow as ma
from flask import jsonify, request
from marshmallow import Schema as MarshmallowSchema, fields as f
from teal import query
from teal.marshmallow import EnumField
from teal.resource import View
@ -15,10 +14,6 @@ from ereuse_devicehub.resources.device.models import Device
from ereuse_devicehub.resources.lot.models import Lot, Path
class Filters(query.Query):
name = query.ILike(Lot.name)
class LotFormat(Enum):
UiTree = 'UiTree'
@ -30,7 +25,7 @@ class LotView(View):
method (GET collection) endpoint
"""
format = EnumField(LotFormat, missing=None)
filter = f.Nested(Filters, missing=[])
search = f.Str(missing=None)
def post(self):
l = request.get_json()
@ -73,8 +68,10 @@ class LotView(View):
'url': request.path
})
else:
query = Lot.query.filter(*args['filter'])
lots = query.paginate(per_page=6)
query = Lot.query
if args['search']:
query = query.filter(Lot.name.ilike(args['search'] + '%'))
lots = query.paginate(per_page=6 if args['search'] else 30)
ret = {
'items': self.schema.dump(lots.items, many=True, nested=0),
'pagination': {

View file

@ -243,7 +243,7 @@ def test_post_add_children_view_ui_tree_normal(user: UserClient):
assert lots[1]['name'] == 'Child'
# List format with a filter
lots = user.get(res=Lot, query=[('filter', {'name': 'pa'})])[0]['items']
lots = user.get(res=Lot, query=[('search', 'pa')])[0]['items']
assert len(lots) == 1
assert lots[0]['name'] == 'Parent'