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

View File

@ -243,7 +243,7 @@ def test_post_add_children_view_ui_tree_normal(user: UserClient):
assert lots[1]['name'] == 'Child' assert lots[1]['name'] == 'Child'
# List format with a filter # 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 len(lots) == 1
assert lots[0]['name'] == 'Parent' assert lots[0]['name'] == 'Parent'