Skip to content
Merged

Sync #41

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update some comment; fix "NameError: name 'model_name' is not defined…
…"; add 2 test cases
  • Loading branch information
zhangchunlin committed Oct 11, 2019
commit dda827bcdc8731dc47dc222ae09ab8a433923682
36 changes: 36 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,42 @@ def test_apijson_get():
>>> print(d)
{'code': 400, 'msg': "'!'(not) expression need 2 items, but get '['username$', '!', 'nickname$']'"}

>>> #query array with like
>>> data ='''{
... "[]":{
... "@count":4,
... "@page":0,
... "user":{
... "@column":"id,username,nickname,email",
... "@order":"id-",
... "@role":"ADMIN",
... "username$":"%b%"
... }
... }
... }'''
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
>>> d = json_loads(r.data)
>>> print(d)
{'code': 200, 'msg': 'success', '[]': [{'user': {'username': 'userb', 'nickname': 'User B', 'email': 'userb@localhost', 'id': 3}}]}

>>> #query array with like, but gave a nonexist column
>>> data ='''{
... "[]":{
... "@count":4,
... "@page":0,
... "user":{
... "@column":"id,username,nickname,email",
... "@order":"id-",
... "@role":"ADMIN",
... "nonexist$":"%b%"
... }
... }
... }'''
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
>>> d = json_loads(r.data)
>>> print(d)
{'code': 400, 'msg': "model does not have this column: 'nonexist'"}

>>> #Association query: Two tables, one to one,ref path is absolute path
>>> data ='''{
... "moment":{},
Expand Down
10 changes: 7 additions & 3 deletions uliweb_apijson/apijson/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,26 +225,30 @@ def _expr(self,model,model_param,model_expr):
raise UliwebError("unknown operator: '%s'"%(op))

def _get_filter_condition(self,model,model_param,item,expr=False):
#item can be param key, or expr which expected to be a list
if isinstance(item,list):
if expr:
return self._expr(model,model_param,model_expr=item)
else:
raise UliwebError("item can be array only in @expr: '%s'"%(item))
#current implementation won't run here, but keep for safe
raise UliwebError("item can be list only in @expr: '%s'"%(item))
if not isinstance(item,string_types):
#current implementation won't run here, but keep for safe
raise UliwebError("item should be array or string: '%s'"%(item))
n = item
if n[0]=="@":
#current implementation won't run here, but keep for safe
raise UliwebError("param key should not begin with @: '%s'"%(n))
if n[-1]=="$":
name = n[:-1]
if hasattr(model,name):
return getattr(model.c,name).like(model_param[n])
else:
raise UliwebError("'%s' does not have '%s'"%(model_name,name))
raise UliwebError("model does not have this column: '%s'"%(name))
elif n[-1]=="}" and n[-2]=="{":
name = n[:-2]
if hasattr(model,name):
# TODO
# TODO: https://github.com/APIJSON/APIJSON/blob/master/Document.md#32-%E5%8A%9F%E8%83%BD%E7%AC%A6
pass
raise UliwebError("still not support '%s'"%(name))
elif hasattr(model,n):
Expand Down