Movatterモバイル変換


[0]ホーム

URL:


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

Commit85534ca

Browse files
committed
fix owner_condition() invoke; add 2 test cases for owner_condition()
1 parent8f3f0b1 commit85534ca

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

‎tests/demo/apps/apijson_demo/dbinit.py

Lines changed: 7 additions & 0 deletions
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",

‎tests/demo/apps/apijson_demo/models.py

Lines changed: 5 additions & 0 deletions
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+
defowner_condition(cls,user_id):
21+
print("Moment: owner_condition")
22+
returncls.c.user_id==user_id
23+
1924
classComment(Model):
2025
user_id=Reference("user")
2126
to_id=Reference("user")

‎tests/test.py

Lines changed: 26 additions & 0 deletions
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":{},

‎uliweb_apijson/apijson/views.py

Lines changed: 1 addition & 1 deletion
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
ifhasattr(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
ifnotowner_filtered:
195195
user_id_field=model_setting.get("user_id_field")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp