本文整理汇总了Python中onadata.libs.permissions.ReadOnlyRole.add方法的典型用法代码示例。如果您正苦于以下问题:Python ReadOnlyRole.add方法的具体用法?Python ReadOnlyRole.add怎么用?Python ReadOnlyRole.add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类onadata.libs.permissions.ReadOnlyRole
的用法示例。
在下文中一共展示了ReadOnlyRole.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_widget_permission_change
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def test_widget_permission_change(self):
self._create_widget()
alice_data = {'username': 'alice', 'email': '[email protected]'}
self._login_user_and_profile(alice_data)
data = {
'title': "Widget those",
}
OwnerRole.add(self.user, self.project)
OwnerRole.add(self.user, self.xform)
request = self.factory.patch('/', data=data, **self.extra)
response = self.view(request, pk=self.widget.pk)
self.assertEquals(response.status_code, 200)
self.assertEquals(response.data['title'], 'Widget those')
ReadOnlyRole.add(self.user, self.project)
ReadOnlyRole.add(self.user, self.xform)
request = self.factory.patch('/', data=data, **self.extra)
response = self.view(request, pk=self.widget.pk)
self.assertEquals(response.status_code, 200)
self.assertEquals(response.data['title'], 'Widget those')
示例2: update
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def update(self, instance, validated_data):
metadata = JsonField.to_json(validated_data.get('metadata'))
if metadata is None:
metadata = dict()
owner = validated_data.get('organization')
if self.partial and metadata:
if not isinstance(instance.metadata, dict):
instance.metadata = {}
instance.metadata.update(metadata)
validated_data['metadata'] = instance.metadata
if self.partial and owner:
# give the new owner permissions
set_owners_permission(owner, instance)
if is_organization(owner.profile):
owners_team = get_organization_owners_team(owner.profile)
members_team = get_organization_members_team(owner.profile)
OwnerRole.add(owners_team, instance)
ReadOnlyRole.add(members_team, instance)
# clear cache
safe_delete('{}{}'.format(PROJ_PERM_CACHE, instance.pk))
project = super(ProjectSerializer, self)\
.update(instance, validated_data)
project.xform_set.exclude(shared=project.shared)\
.update(shared=project.shared, shared_data=project.shared)
return instance
示例3: test_get_xform_list_other_user_with_readonly_role
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def test_get_xform_list_other_user_with_readonly_role(self):
request = self.factory.get('/')
response = self.view(request)
alice_data = {'username': 'alice', 'email': '[email protected]'}
alice_profile = self._create_user_profile(alice_data)
ReadOnlyRole.add(alice_profile.user, self.xform)
self.assertTrue(
ReadOnlyRole.user_has_role(alice_profile.user, self.xform)
)
auth = DigestAuth('alice', 'bobbob')
request.META.update(auth(request.META, response))
response = self.view(request)
self.assertEqual(response.status_code, 200)
content = response.render().content
self.assertNotIn(self.xform.id_string, content)
self.assertEqual(
content, '<?xml version="1.0" encoding="utf-8"?>\n<xforms '
'xmlns="http://openrosa.org/xforms/xformsList"></xforms>')
self.assertTrue(response.has_header('X-OpenRosa-Version'))
self.assertTrue(
response.has_header('X-OpenRosa-Accept-Content-Length'))
self.assertTrue(response.has_header('Date'))
self.assertEqual(response['Content-Type'], 'text/xml; charset=utf-8')
示例4: add_xform_to_project
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def add_xform_to_project(xform, project, creator):
"""Adds an xform to a project"""
# remove xform from any previous relation to a project
xform.projectxform_set.all().delete()
# make new connection
instance = ProjectXForm.objects.create(
xform=xform, project=project, created_by=creator)
instance.save()
# check if the project is a public and make the form public
if project.shared != xform.shared:
xform.shared = project.shared
xform.shared_data = project.shared
xform.save()
for perm in get_object_users_with_permissions(project):
user = perm['user']
if user != creator:
ReadOnlyRole.add(user, xform)
else:
OwnerRole.add(user, xform)
return instance
示例5: save
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def save(self, **kwargs):
role = ROLES.get(self.role)
if role and self.user and self.project:
role.add(self.user, self.project)
# add readonly role to forms under the project
for px in self.project.projectxform_set.all():
ReadOnlyRole.add(self.user, px.xform)
示例6: test_data_list_filter_by_user
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def test_data_list_filter_by_user(self):
view = DataViewSet.as_view({'get': 'list'})
formid = self.xform.pk
bobs_data = {
u'id': formid,
u'id_string': u'transportation_2011_07_25',
u'title': 'transportation_2011_07_25',
u'description': 'transportation_2011_07_25',
u'url': u'http://testserver/api/v1/data/%s' % formid
}
previous_user = self.user
self._create_user_and_login('alice', 'alice')
self.assertEqual(self.user.username, 'alice')
self.assertNotEqual(previous_user, self.user)
ReadOnlyRole.add(self.user, self.xform)
# publish alice's form
self._publish_transportation_form()
self.extra = {
'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token}
formid = self.xform.pk
alice_data = {
u'id': formid,
u'id_string': u'transportation_2011_07_25',
u'title': 'transportation_2011_07_25',
u'description': 'transportation_2011_07_25',
u'url': u'http://testserver/api/v1/data/%s' % formid
}
request = self.factory.get('/', **self.extra)
response = view(request)
self.assertEqual(response.status_code, 200)
# should be both bob's and alice's form
self.assertEqual(sorted(response.data),
sorted([bobs_data, alice_data]))
# apply filter, see only bob's forms
request = self.factory.get('/', data={'owner': 'bob'}, **self.extra)
response = view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [bobs_data])
# apply filter, see only alice's forms
request = self.factory.get('/', data={'owner': 'alice'}, **self.extra)
response = view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [alice_data])
# apply filter, see a non existent user
request = self.factory.get('/', data={'owner': 'noone'}, **self.extra)
response = view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [])
示例7: test_project_users_get_readonly_role_on_add_form
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def test_project_users_get_readonly_role_on_add_form(self):
self._project_create()
alice_data = {'username': 'alice', 'email': '[email protected]calhost.com'}
alice_profile = self._create_user_profile(alice_data)
ReadOnlyRole.add(alice_profile.user, self.project)
self.assertTrue(ReadOnlyRole.user_has_role(alice_profile.user,
self.project))
self._publish_xls_form_to_project()
self.assertTrue(ReadOnlyRole.user_has_role(alice_profile.user,
self.xform))
self.assertFalse(OwnerRole.user_has_role(alice_profile.user,
self.xform))
示例8: test_widget_permission_create
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def test_widget_permission_create(self):
alice_data = {'username': 'alice', 'email': '[email protected]'}
self._login_user_and_profile(alice_data)
view = WidgetViewSet.as_view({
'post': 'create'
})
data = {
'title': "Widget that",
'content_object': 'http://testserver/api/v1/forms/%s' %
self.xform.pk,
'description': "Test widget",
'aggregation': "Sum",
'widget_type': "charts",
'view_type': "horizontal-bar",
'column': "age",
'group_by': ''
}
# to do: test random user with auth but no perms
request = self.factory.post('/', data=json.dumps(data),
content_type="application/json",
**self.extra)
response = view(request)
self.assertEquals(response.status_code, 400)
# owner
OwnerRole.add(self.user, self.project)
request = self.factory.post('/', data=json.dumps(data),
content_type="application/json",
**self.extra)
response = view(request)
self.assertEquals(response.status_code, 201)
# readonly
ReadOnlyRole.add(self.user, self.project)
request = self.factory.post('/', data=json.dumps(data),
content_type="application/json",
**self.extra)
response = view(request)
self.assertEquals(response.status_code, 201)
# dataentryonlyrole
DataEntryOnlyRole.add(self.user, self.project)
request = self.factory.post('/', data=json.dumps(data),
content_type="application/json",
**self.extra)
response = view(request)
self.assertEquals(response.status_code, 201)
示例9: save
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def save(self, **kwargs):
if self.remove:
self.remove_user()
else:
role = ROLES.get(self.role)
if role and self.user and self.project:
role.add(self.user, self.project)
# add readonly role to forms under the project
for xform in self.project.xform_set.all():
ReadOnlyRole.add(self.user, xform)
示例10: set_project_perms_to_xform
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def set_project_perms_to_xform(xform, project):
if project.shared != xform.shared:
xform.shared = project.shared
xform.shared_data = project.shared
xform.save()
for perm in get_object_users_with_permissions(project):
user = perm["user"]
if user != xform.created_by:
ReadOnlyRole.add(user, xform)
else:
OwnerRole.add(user, xform)
示例11: test_reassign_role
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def test_reassign_role(self):
self._publish_transportation_form()
alice = self._create_user('alice', 'alice')
self.assertFalse(ManagerRole.has_role(alice, self.xform))
ManagerRole.add(alice, self.xform)
self.assertTrue(ManagerRole.has_role(alice, self.xform))
ReadOnlyRole.add(alice, self.xform)
self.assertFalse(ManagerRole.has_role(alice, self.xform))
self.assertTrue(ReadOnlyRole.has_role(alice, self.xform))
示例12: test_form_list_filter_by_user
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def test_form_list_filter_by_user(self):
# publish bob's form
self._publish_xls_form_to_project()
previous_user = self.user
alice_data = {'username': 'alice', 'email': '[email protected]'}
self._login_user_and_profile(extra_post_data=alice_data)
self.assertEqual(self.user.username, 'alice')
self.assertNotEqual(previous_user, self.user)
ReadOnlyRole.add(self.user, self.xform)
view = XFormViewSet.as_view({
'get': 'retrieve'
})
request = self.factory.get('/', **self.extra)
response = view(request, pk=self.xform.pk)
bobs_form_data = response.data
# publish alice's form
self._publish_xls_form_to_project()
request = self.factory.get('/', **self.extra)
response = self.view(request)
self.assertNotEqual(response.get('Last-Modified'), None)
self.assertEqual(response.status_code, 200)
# should be both bob's and alice's form
self.assertEqual(sorted(response.data),
sorted([bobs_form_data, self.form_data]))
# apply filter, see only bob's forms
request = self.factory.get('/', data={'owner': 'bob'}, **self.extra)
response = self.view(request)
self.assertNotEqual(response.get('Last-Modified'), None)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [bobs_form_data])
# apply filter, see only alice's forms
request = self.factory.get('/', data={'owner': 'alice'}, **self.extra)
response = self.view(request)
self.assertNotEqual(response.get('Last-Modified'), None)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [self.form_data])
# apply filter, see a non existent user
request = self.factory.get('/', data={'owner': 'noone'}, **self.extra)
response = self.view(request)
self.assertEqual(response.status_code, 200)
self.assertNotEqual(response.get('Last-Modified'), None)
self.assertEqual(response.data, [])
示例13: test_data_list_filter_by_user
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def test_data_list_filter_by_user(self):
self._make_submissions()
view = DataViewSet.as_view({'get': 'list'})
formid = self.xform.pk
bobs_data = _data_list(formid)[0]
previous_user = self.user
self._create_user_and_login('alice', 'alice')
self.assertEqual(self.user.username, 'alice')
self.assertNotEqual(previous_user, self.user)
ReadOnlyRole.add(self.user, self.xform)
# publish alice's form
self._publish_transportation_form()
self.extra = {
'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token}
formid = self.xform.pk
alice_data = _data_list(formid)[0]
request = self.factory.get('/', **self.extra)
response = view(request)
self.assertEqual(response.status_code, 200)
# should be both bob's and alice's form
self.assertEqual(sorted(response.data),
sorted([bobs_data, alice_data]))
# apply filter, see only bob's forms
request = self.factory.get('/', data={'owner': 'bob'}, **self.extra)
response = view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [bobs_data])
# apply filter, see only alice's forms
request = self.factory.get('/', data={'owner': 'alice'}, **self.extra)
response = view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [alice_data])
# apply filter, see a non existent user
request = self.factory.get('/', data={'owner': 'noone'}, **self.extra)
response = view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [])
示例14: test_widget_permission_get
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def test_widget_permission_get(self):
self._create_widget()
alice_data = {'username': 'alice', 'email': '[email protected]'}
self._login_user_and_profile(alice_data)
request = self.factory.get('/', **self.extra)
response = self.view(request, pk=self.widget.pk)
self.assertEquals(response.status_code, 404)
# assign alice the perms
ReadOnlyRole.add(self.user, self.project)
request = self.factory.get('/', **self.extra)
response = self.view(request, formid=self.xform.pk,
pk=self.widget.pk)
self.assertEquals(response.status_code, 200)
示例15: test_project_filter_by_owner
# 需要导入模块: from onadata.libs.permissions import ReadOnlyRole [as 别名]
# 或者: from onadata.libs.permissions.ReadOnlyRole import add [as 别名]
def test_project_filter_by_owner(self):
self._project_create()
alice_data = {'username': 'alice', 'email': '[email protected]'}
self._login_user_and_profile(alice_data)
ReadOnlyRole.add(self.user, self.project)
view = ProjectViewSet.as_view({
'get': 'retrieve'
})
request = self.factory.get('/', **self.extra)
response = view(request, pk=self.project.pk)
updated_project_data = response.data
self._project_create({'name': 'another project'})
# both bob's and alice's projects
request = self.factory.get('/', **self.extra)
response = self.view(request)
self.assertEqual(response.status_code, 200)
self.assertIn(updated_project_data, response.data)
self.assertIn(self.project_data, response.data)
# only bob's project
request = self.factory.get('/', {'owner': 'bob'}, **self.extra)
response = self.view(request)
self.assertEqual(response.status_code, 200)
self.assertIn(updated_project_data, response.data)
self.assertNotIn(self.project_data, response.data)
# only alice's project
request = self.factory.get('/', {'owner': 'alice'}, **self.extra)
response = self.view(request)
self.assertEqual(response.status_code, 200)
self.assertNotIn(updated_project_data, response.data)
self.assertIn(self.project_data, response.data)
# none existent user
request = self.factory.get('/', {'owner': 'noone'}, **self.extra)
response = self.view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, [])