本文整理汇总了Python中nefertari.wrappers.apply_privacy函数的典型用法代码示例。如果您正苦于以下问题:Python apply_privacy函数的具体用法?Python apply_privacy怎么用?Python apply_privacy使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了apply_privacy函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_simple_call_with_nested
def test_simple_call_with_nested(self, mock_eng):
document_cls = Mock(
_public_fields=['name', 'desc'],
_auth_fields=['id', 'creator'],
_hidden_fields=[])
mock_eng.get_document_cls.return_value = document_cls
request = Mock(user=Mock())
data = {
'id': 1,
'_type': 'foo1',
'username': 'admin',
'creator': {
'id': 2,
'_type': 'foo2',
'creator': 'foo',
'address': 'adsasd',
}
}
filtered = wrappers.apply_privacy(request)(
result=data, is_admin=False)
assert filtered == {
'_type': 'foo1',
'id': 1,
'creator': {
'_type': 'foo2',
'creator': 'foo',
'id': 2
}
}
示例2: test_apply_privacy_item_no_document_cls
def test_apply_privacy_item_no_document_cls(self, mock_eng):
mock_eng.get_document_cls.side_effect = ValueError
request = Mock(user=Mock())
filtered = wrappers.apply_privacy(request)(
result=self.model_test_data, is_admin=True)
assert list(sorted(filtered.keys())) == [
'_type', 'desc', 'id', 'name', 'other_field', 'self']
示例3: test_apply_privacy_item_no_request
def test_apply_privacy_item_no_request(self, mock_eng):
document_cls = Mock(
_public_fields=['name', 'desc'],
_auth_fields=['id'])
mock_eng.get_document_cls.return_value = document_cls
filtered = wrappers.apply_privacy(None)(result=self.model_test_data)
assert list(sorted(filtered.keys())) == [
'_type', 'desc', 'id', 'name', 'other_field', 'self']
示例4: setup_default_wrappers
def setup_default_wrappers(self):
""" Setup defaulf wrappers.
Wrappers are applied when view method does not return instance
of Response. In this case nefertari renderers call wrappers and
handle response generation.
Note: It's important for `add_etag` wrapper be applied before
`apply_privacy` as later may remove response data that
is used to generate etag
"""
# Index
self._after_calls['index'] = [
wrappers.wrap_in_dict(self.request),
wrappers.add_meta(self.request),
wrappers.add_object_url(self.request),
wrappers.add_etag(self.request),
]
# Show
self._after_calls['show'] = [
wrappers.wrap_in_dict(self.request),
wrappers.add_meta(self.request),
wrappers.add_object_url(self.request),
]
# Create
self._after_calls['create'] = [
wrappers.wrap_in_dict(self.request),
wrappers.add_meta(self.request),
wrappers.add_object_url(self.request),
]
# Update
self._after_calls['update'] = [
wrappers.wrap_in_dict(self.request),
wrappers.add_meta(self.request),
wrappers.add_object_url(self.request),
]
# Replace
self._after_calls['replace'] = [
wrappers.wrap_in_dict(self.request),
wrappers.add_meta(self.request),
wrappers.add_object_url(self.request),
]
# Privacy wrappers
if self._auth_enabled:
for meth in ('index', 'show', 'create', 'update', 'replace'):
self._after_calls[meth] += [
wrappers.apply_privacy(self.request),
]
for meth in ('update', 'replace', 'update_many'):
self._before_calls[meth] += [
wrappers.apply_request_privacy(
self.Model, self._json_params),
]
示例5: test_apply_privacy_item_no_fields
def test_apply_privacy_item_no_fields(self, mock_eng):
document_cls = Mock(
_public_fields=['name', 'desc'],
_auth_fields=[])
mock_eng.get_document_cls.return_value = document_cls
request = Mock(user=Mock())
filtered = wrappers.apply_privacy(request)(
result=self.model_test_data, is_admin=False)
assert list(sorted(filtered.keys())) == ['_type', 'self']
示例6: test_item_non_auth
def test_item_non_auth(self, mock_eng):
document_cls = Mock(
_public_fields=['name', 'desc'],
_auth_fields=['id'],
_hidden_fields=[])
mock_eng.get_document_cls.return_value = document_cls
request = Mock(user=None)
filtered = wrappers.apply_privacy(request)(result=self.model_test_data)
assert list(sorted(filtered.keys())) == [
'_pk', '_self', '_type', 'desc', 'name']
示例7: test_collection
def test_collection(self, mock_eng):
document_cls = Mock(_public_fields=["name", "desc"], _auth_fields=["id"], _hidden_fields=[])
mock_eng.get_document_cls.return_value = document_cls
request = Mock(user=Mock())
result = {"total": 1, "count": 1, "data": [self.model_test_data]}
filtered = wrappers.apply_privacy(request)(result=result, is_admin=False)
assert list(sorted(filtered.keys())) == ["count", "data", "total"]
assert len(filtered["data"]) == 1
data = filtered["data"][0]
assert list(sorted(data.keys())) == ["_pk", "_self", "_type", "id"]
示例8: test_no_type
def test_no_type(self, mock_eng):
data = self.model_test_data.copy()
data.pop("_type")
request = Mock(user=Mock())
filtered = wrappers.apply_privacy(request)(result=data, is_admin=True)
assert list(sorted(filtered.keys())) == ["_pk", "_self", "desc", "id", "name", "other_field"]
filtered["desc"] == "User 1 data"
filtered["id"] == 1
filtered["name"] == "User1"
filtered["other_field"] == 123
filtered["_self"] == "http://example.com/1"
assert not mock_eng.get_document_cls.called
示例9: test_item_auth_calculated
def test_item_auth_calculated(self, mock_eng):
document_cls = Mock(_public_fields=["name", "desc"], _auth_fields=["id"], _hidden_fields=[])
mock_eng.get_document_cls.return_value = document_cls
class User(object):
@classmethod
def is_admin(self, obj):
return False
request = Mock(user=User())
filtered = wrappers.apply_privacy(request)(result=self.model_test_data)
assert list(sorted(filtered.keys())) == ["_pk", "_self", "_type", "id"]
示例10: test_hidden_fields_not_drop
def test_hidden_fields_not_drop(self, mock_eng):
document_cls = Mock(
_public_fields=['name', 'desc'],
_auth_fields=['id'],
_hidden_fields=['name'])
mock_eng.get_document_cls.return_value = document_cls
request = Mock(user=Mock())
filtered = wrappers.apply_privacy(request)(
result=self.model_test_data, is_admin=False,
drop_hidden=False)
assert list(sorted(filtered.keys())) == [
'_pk', '_self', '_type', 'id', 'name']
示例11: test_apply_nested_privacy_list
def test_apply_nested_privacy_list(self, mock_eng):
document_cls = Mock(_public_fields=["name", "desc"], _auth_fields=["id"], _hidden_fields=[])
mock_eng.get_document_cls.return_value = document_cls
request = Mock(user=Mock())
data = {"owner": [self.model_test_data]}
wrapper = wrappers.apply_privacy(request)
wrapper.is_admin = False
wrapper.drop_hidden = False
filtered = wrapper._apply_nested_privacy(data)
assert list(filtered.keys()) == ["owner"]
owner = filtered["owner"][0]
assert sorted(owner.keys()) == ["_pk", "_self", "_type", "id"]
示例12: test_simple_call_with_nested
def test_simple_call_with_nested(self, mock_eng):
document_cls = Mock(_public_fields=["name", "desc"], _auth_fields=["id", "creator"], _hidden_fields=[])
mock_eng.get_document_cls.return_value = document_cls
request = Mock(user=Mock())
data = {
"id": 1,
"_type": "foo1",
"username": "admin",
"creator": {"id": 2, "_type": "foo2", "creator": "foo", "address": "adsasd"},
}
filtered = wrappers.apply_privacy(request)(result=data, is_admin=False)
assert filtered == {"_type": "foo1", "id": 1, "creator": {"_type": "foo2", "creator": "foo", "id": 2}}
示例13: test_item_admin
def test_item_admin(self, mock_eng):
document_cls = Mock(_public_fields=["name", "desc"], _auth_fields=["id"], _hidden_fields=[])
mock_eng.get_document_cls.return_value = document_cls
request = Mock(user=Mock())
filtered = wrappers.apply_privacy(request)(result=self.model_test_data, is_admin=True)
assert list(sorted(filtered.keys())) == ["_pk", "_self", "_type", "desc", "id", "name", "other_field"]
filtered["_type"] == "foo"
filtered["desc"] == "User 1 data"
filtered["id"] == 1
filtered["name"] == "User1"
filtered["other_field"] == 123
filtered["_self"] == "http://example.com/1"
mock_eng.get_document_cls.assert_called_once_with("foo")
示例14: test_apply_privacy_no_type
def test_apply_privacy_no_type(self, mock_eng):
data = self.model_test_data.copy()
data.pop('_type')
request = Mock(user=Mock())
filtered = wrappers.apply_privacy(request)(
result=data, is_admin=True)
assert list(sorted(filtered.keys())) == [
'desc', 'id', 'name', 'other_field', 'self']
filtered['desc'] == 'User 1 data'
filtered['id'] == 1
filtered['name'] == 'User1'
filtered['other_field'] == 123
filtered['self'] == 'http://example.com/1'
assert not mock_eng.get_document_cls.called
示例15: test_apply_privacy_item_auth_calculated
def test_apply_privacy_item_auth_calculated(self, mock_eng):
document_cls = Mock(
_public_fields=['name', 'desc'],
_auth_fields=['id'])
mock_eng.get_document_cls.return_value = document_cls
class User(object):
@classmethod
def is_admin(self, obj):
return False
request = Mock(user=User())
filtered = wrappers.apply_privacy(request)(result=self.model_test_data)
assert list(sorted(filtered.keys())) == [
'_type', 'id', 'self']