Skip to content

Commit 85534ca

Browse files
committed
fix owner_condition() invoke; add 2 test cases for owner_condition()
1 parent 8f3f0b1 commit 85534ca

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

Diff for: tests/demo/apps/apijson_demo/dbinit.py

+7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@
8383
]
8484

8585
comment_list = [
86+
{
87+
"username" : "admin",
88+
"to_username" : "userb",
89+
"moment_id" : 1,
90+
"date" : "2018-11-1",
91+
"content" : "comment from admin",
92+
},
8693
{
8794
"username" : "usera",
8895
"to_username" : "userb",

Diff for: tests/demo/apps/apijson_demo/models.py

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class Moment(Model):
1616
content = Field(TEXT)
1717
picture_list = Field(JSON, default=[])
1818

19+
@classmethod
20+
def owner_condition(cls,user_id):
21+
print("Moment: owner_condition")
22+
return cls.c.user_id==user_id
23+
1924
class Comment(Model):
2025
user_id = Reference("user")
2126
to_id = Reference("user")

Diff for: tests/test.py

+26
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ def test_apijson_get():
157157
>>> print(d)
158158
{'code': 400, 'msg': "'publicnotice' cannot filter with owner"}
159159
160+
>>> #query one with OWNER which will use owner_condition() to filter
161+
>>> data ='''{
162+
... "moment":{
163+
... "@role":"OWNER"
164+
... }
165+
... }'''
166+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("usera"), middlewares=[])
167+
Moment: owner_condition
168+
>>> d = json_loads(r.data)
169+
>>> print(d)
170+
{'code': 200, 'msg': 'success', 'moment': {'user_id': 2, 'date': '2018-11-01 00:00:00', 'content': 'test moment', 'picture_list': '[]', 'id': 1}}
171+
160172
>>> #query one with UNKNOWN
161173
>>> data ='''{
162174
... "publicnotice":{
@@ -427,6 +439,20 @@ def test_apijson_get():
427439
>>> print(d)
428440
{'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}}]}
429441
442+
>>> #query array with OWNER, the model using owner_condition
443+
>>> data ='''{
444+
... "[]":{
445+
... "moment": {
446+
... "@role":"OWNER"
447+
... }
448+
... }
449+
... }'''
450+
>>> r = handler.post('/apijson/get', data=data, pre_call=pre_call_as("usera"), middlewares=[])
451+
Moment: owner_condition
452+
>>> d = json_loads(r.data)
453+
>>> print(d)
454+
{'code': 200, 'msg': 'success', '[]': [{'moment': {'user_id': 2, 'date': '2018-11-01 00:00:00', 'content': 'test moment', 'picture_list': '[]', 'id': 1}}]}
455+
430456
>>> #Association query: Two tables, one to one,ref path is absolute path
431457
>>> data ='''{
432458
... "moment":{},

Diff for: uliweb_apijson/apijson/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def _get_array(self,key):
189189
def _filter_owner(self,model,model_setting,q):
190190
owner_filtered = False
191191
if hasattr(model,"owner_condition"):
192-
q = q.filter(model.owner_condition())
192+
q = q.filter(model.owner_condition(request.user.id))
193193
owner_filtered = True
194194
if not owner_filtered:
195195
user_id_field = model_setting.get("user_id_field")

0 commit comments

Comments
 (0)