Skip to content
Merged

Sync #40

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
fix owner_condition() invoke; add 2 test cases for owner_condition()
  • Loading branch information
zhangchunlin committed Oct 10, 2019
commit 85534ca6d066e8950049eb2963eb12c9276346ff
7 changes: 7 additions & 0 deletions tests/demo/apps/apijson_demo/dbinit.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@
]

comment_list = [
{
"username" : "admin",
"to_username" : "userb",
"moment_id" : 1,
"date" : "2018-11-1",
"content" : "comment from admin",
},
{
"username" : "usera",
"to_username" : "userb",
Expand Down
5 changes: 5 additions & 0 deletions tests/demo/apps/apijson_demo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class Moment(Model):
content = Field(TEXT)
picture_list = Field(JSON, default=[])

@classmethod
def owner_condition(cls,user_id):
print("Moment: owner_condition")
return cls.c.user_id==user_id

class Comment(Model):
user_id = Reference("user")
to_id = Reference("user")
Expand Down
26 changes: 26 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ def test_apijson_get():
>>> print(d)
{'code': 400, 'msg': "'publicnotice' cannot filter with owner"}

>>> #query one with OWNER which will use owner_condition() to filter
>>> data ='''{
... "moment":{
... "@role":"OWNER"
... }
... }'''
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("usera"), middlewares=[])
Moment: owner_condition
>>> d = json_loads(r.data)
>>> print(d)
{'code': 200, 'msg': 'success', 'moment': {'user_id': 2, 'date': '2018-11-01 00:00:00', 'content': 'test moment', 'picture_list': '[]', 'id': 1}}

>>> #query one with UNKNOWN
>>> data ='''{
... "publicnotice":{
Expand Down Expand Up @@ -427,6 +439,20 @@ def test_apijson_get():
>>> print(d)
{'code': 200, 'msg': 'success', '[]': [{'comment': {'user_id': 1, 'to_id': 3, 'moment_id': 1, 'date': '2018-11-01 00:00:00', 'content': 'comment from admin', 'id': 1}}]}

>>> #query array with OWNER, the model using owner_condition
>>> data ='''{
... "[]":{
... "moment": {
... "@role":"OWNER"
... }
... }
... }'''
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("usera"), middlewares=[])
Moment: owner_condition
>>> d = json_loads(r.data)
>>> print(d)
{'code': 200, 'msg': 'success', '[]': [{'moment': {'user_id': 2, 'date': '2018-11-01 00:00:00', 'content': 'test moment', 'picture_list': '[]', 'id': 1}}]}

>>> #Association query: Two tables, one to one,ref path is absolute path
>>> data ='''{
... "moment":{},
Expand Down
2 changes: 1 addition & 1 deletion uliweb_apijson/apijson/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _get_array(self,key):
def _filter_owner(self,model,model_setting,q):
owner_filtered = False
if hasattr(model,"owner_condition"):
q = q.filter(model.owner_condition())
q = q.filter(model.owner_condition(request.user.id))
owner_filtered = True
if not owner_filtered:
user_id_field = model_setting.get("user_id_field")
Expand Down