Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dda827b

Browse files
update some comment; fix "NameError: name 'model_name' is not defined"; add 2 test cases
1 parent c32aa1d commit dda827b

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

‎tests/test.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,42 @@ def test_apijson_get():
707707
>>> print(d)
708708
{'code': 400, 'msg': "'!'(not) expression need 2 items, but get '['username$', '!', 'nickname$']'"}
709709
710+
>>> #query array with like
711+
>>> data ='''{
712+
... "[]":{
713+
... "@count":4,
714+
... "@page":0,
715+
... "user":{
716+
... "@column":"id,username,nickname,email",
717+
... "@order":"id-",
718+
... "@role":"ADMIN",
719+
... "username$":"%b%"
720+
... }
721+
... }
722+
... }'''
723+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
724+
>>> d = json_loads(r.data)
725+
>>> print(d)
726+
{'code': 200, 'msg': 'success', '[]': [{'user': {'username': 'userb', 'nickname': 'User B', 'email': 'userb@localhost', 'id': 3}}]}
727+
728+
>>> #query array with like, but gave a nonexist column
729+
>>> data ='''{
730+
... "[]":{
731+
... "@count":4,
732+
... "@page":0,
733+
... "user":{
734+
... "@column":"id,username,nickname,email",
735+
... "@order":"id-",
736+
... "@role":"ADMIN",
737+
... "nonexist$":"%b%"
738+
... }
739+
... }
740+
... }'''
741+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("admin"), middlewares=[])
742+
>>> d = json_loads(r.data)
743+
>>> print(d)
744+
{'code': 400, 'msg': "model does not have this column: 'nonexist'"}
745+
710746
>>> #Association query: Two tables, one to one,ref path is absolute path
711747
>>> data ='''{
712748
... "moment":{},

‎uliweb_apijson/apijson/views.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,26 +225,30 @@ def _expr(self,model,model_param,model_expr):
225225
raise UliwebError("unknown operator: '%s'"%(op))
226226

227227
def _get_filter_condition(self,model,model_param,item,expr=False):
228+
#item can be param key, or expr which expected to be a list
228229
if isinstance(item,list):
229230
if expr:
230231
return self._expr(model,model_param,model_expr=item)
231232
else:
232-
raise UliwebError("item can be array only in @expr: '%s'"%(item))
233+
#current implementation won't run here, but keep for safe
234+
raise UliwebError("item can be list only in @expr: '%s'"%(item))
233235
if not isinstance(item,string_types):
236+
#current implementation won't run here, but keep for safe
234237
raise UliwebError("item should be array or string: '%s'"%(item))
235238
n = item
236239
if n[0]=="@":
240+
#current implementation won't run here, but keep for safe
237241
raise UliwebError("param key should not begin with @: '%s'"%(n))
238242
if n[-1]=="$":
239243
name = n[:-1]
240244
if hasattr(model,name):
241245
return getattr(model.c,name).like(model_param[n])
242246
else:
243-
raise UliwebError("'%s' does not have '%s'"%(model_name,name))
247+
raise UliwebError("model does not have this column: '%s'"%(name))
244248
elif n[-1]=="}" and n[-2]=="{":
245249
name = n[:-2]
246250
if hasattr(model,name):
247-
# TODO
251+
# TODO: https://github.com/APIJSON/APIJSON/blob/master/Document.md#32-%E5%8A%9F%E8%83%BD%E7%AC%A6
248252
pass
249253
raise UliwebError("still not support '%s'"%(name))
250254
elif hasattr(model,n):

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /