本文整理汇总了Python中onadata.libs.permissions.OwnerRole.add方法的典型用法代码示例。如果您正苦于以下问题:Python OwnerRole.add方法的具体用法?Python OwnerRole.add怎么用?Python OwnerRole.add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类onadata.libs.permissions.OwnerRole
的用法示例。
在下文中一共展示了OwnerRole.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_widget_permission_change
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole 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: set_object_permissions
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole import add [as 别名]
def set_object_permissions(sender, instance=None, created=False, **kwargs):
# seems the super is not called, have to get xform from here
xform = XForm.objects.get(pk=instance.pk)
if created:
from onadata.libs.permissions import OwnerRole
OwnerRole.add(instance.user, xform)
if instance.created_by and instance.user != instance.created_by:
OwnerRole.add(instance.created_by, xform)
from onadata.libs.utils.project_utils import set_project_perms_to_xform
set_project_perms_to_xform(xform, instance.project)
if hasattr(instance, 'has_external_choices') \
and instance.has_external_choices:
instance.xls.seek(0)
f = sheet_to_csv(instance.xls.read(), 'external_choices')
f.seek(0, os.SEEK_END)
size = f.tell()
f.seek(0)
from onadata.apps.main.models.meta_data import MetaData
data_file = InMemoryUploadedFile(
file=f,
field_name='data_file',
name='itemsets.csv',
content_type='text/csv',
size=size,
charset=None
)
MetaData.media_upload(xform, data_file)
示例3: update
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole 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
示例4: add_xform_to_project
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole 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: set_object_permissions
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole import add [as 别名]
def set_object_permissions(sender, instance=None, created=False, **kwargs):
if created:
from onadata.libs.permissions import OwnerRole
OwnerRole.add(instance.user, instance)
if instance.created_by and instance.user != instance.created_by:
OwnerRole.add(instance.created_by, instance)
from onadata.libs.utils.project_utils import set_project_perms_to_xform
set_project_perms_to_xform(instance, instance.project)
示例6: test_widget_permission_create
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole 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)
示例7: set_project_perms_to_xform
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole 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)
示例8: set_object_permissions
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole import add [as 别名]
def set_object_permissions(sender, instance=None, created=False, **kwargs):
if created:
from onadata.libs.permissions import OwnerRole
OwnerRole.add(instance.user, instance)
if instance.created_by and instance.user != instance.created_by:
OwnerRole.add(instance.created_by, instance)
from onadata.libs.utils.project_utils import set_project_perms_to_xform
set_project_perms_to_xform(instance, instance.project)
# clear cache
safe_delete('{}{}'.format(PROJ_FORMS_CACHE, instance.project.pk))
safe_delete('{}{}'.format(IS_ORG, instance.pk))
示例9: test_reassign_role_owner_to_editor
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole import add [as 别名]
def test_reassign_role_owner_to_editor(self):
self._publish_transportation_form()
alice = self._create_user('alice', 'alice')
self.assertFalse(OwnerRole.has_role(alice, self.xform))
OwnerRole.add(alice, self.xform)
self.assertTrue(OwnerRole.has_role(alice, self.xform))
EditorRole.add(alice, self.xform)
self.assertFalse(OwnerRole.has_role(alice, self.xform))
self.assertTrue(EditorRole.has_role(alice, self.xform))
示例10: set_object_permissions
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole import add [as 别名]
def set_object_permissions(sender, instance=None, created=False, **kwargs):
"""
Apply the relevant object permissions for the form to all users who should
have access to it.
"""
if instance.project:
# clear cache
safe_delete('{}{}'.format(PROJ_FORMS_CACHE, instance.project.pk))
safe_delete('{}{}'.format(PROJ_BASE_FORMS_CACHE, instance.project.pk))
# seems the super is not called, have to get xform from here
xform = XForm.objects.get(pk=instance.pk)
if created:
from onadata.libs.permissions import OwnerRole
OwnerRole.add(instance.user, xform)
if instance.created_by and instance.user != instance.created_by:
OwnerRole.add(instance.created_by, xform)
from onadata.libs.utils.project_utils import set_project_perms_to_xform_async # noqa
try:
set_project_perms_to_xform_async.delay(xform.pk,
instance.project.pk)
except OperationalError:
from onadata.libs.utils.project_utils import set_project_perms_to_xform # noqa
set_project_perms_to_xform(xform, instance.project)
if hasattr(instance, 'has_external_choices') \
and instance.has_external_choices:
instance.xls.seek(0)
f = sheet_to_csv(instance.xls.read(), 'external_choices')
f.seek(0, os.SEEK_END)
size = f.tell()
f.seek(0)
from onadata.apps.main.models.meta_data import MetaData
data_file = InMemoryUploadedFile(
file=f,
field_name='data_file',
name='itemsets.csv',
content_type='text/csv',
size=size,
charset=None
)
MetaData.media_upload(xform, data_file)
示例11: set_project_perms_to_xform
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole import add [as 别名]
def set_project_perms_to_xform(xform, project):
# allows us to still use xform.shared and xform.shared_data as before
# only switch if xform.shared is False
xform_is_shared = xform.shared or xform.shared_data
if not xform_is_shared and 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']
role_name = perm['role']
role = ROLES.get(role_name)
if user != xform.created_by:
role.add(user, xform)
else:
OwnerRole.add(user, xform)
示例12: set_object_permissions
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole import add [as 别名]
def set_object_permissions(sender, instance=None, created=False, **kwargs):
if created:
OwnerRole.add(instance.user, instance)
OwnerRole.add(instance.user, instance.xform_ptr)
if instance.created_by and instance.user != instance.created_by:
OwnerRole.add(instance.created_by, instance)
OwnerRole.add(instance.created_by, instance.xform_ptr)
set_project_perms_to_xform(instance, instance.project)
set_project_perms_to_xform(instance.xform_ptr, instance.project)
示例13: test_owner_cannot_remove_self_if_no_other_owner
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole import add [as 别名]
def test_owner_cannot_remove_self_if_no_other_owner(self):
self._project_create()
view = ProjectViewSet.as_view({
'put': 'share'
})
ManagerRole.add(self.user, self.project)
tom_data = {'username': 'tom', 'email': '[email protected]'}
bob_profile = self._create_user_profile(tom_data)
OwnerRole.add(bob_profile.user, self.project)
data = {'username': 'tom', 'remove': True, 'role': 'owner'}
request = self.factory.put('/', data=data, **self.extra)
response = view(request, pk=self.project.pk)
self.assertEqual(response.status_code, 400)
error = {'remove': [u"Project requires at least one owner"]}
self.assertEquals(response.data, error)
self.assertTrue(OwnerRole.user_has_role(bob_profile.user,
self.project))
alice_data = {'username': 'alice', 'email': '[email protected]'}
profile = self._create_user_profile(alice_data)
OwnerRole.add(profile.user, self.project)
view = ProjectViewSet.as_view({
'put': 'share'
})
data = {'username': 'tom', 'remove': True, 'role': 'owner'}
request = self.factory.put('/', data=data, **self.extra)
response = view(request, pk=self.project.pk)
self.assertEqual(response.status_code, 204)
self.assertFalse(OwnerRole.user_has_role(bob_profile.user,
self.project))
示例14: set_project_perms_to_xform
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole import add [as 别名]
def set_project_perms_to_xform(xform, project):
"""
Apply project permissions to a form, this usually happens when a new form
is being published or it is being moved to a new project.
"""
# allows us to still use xform.shared and xform.shared_data as before
# only switch if xform.shared is False
xform_is_shared = xform.shared or xform.shared_data
if not xform_is_shared and project.shared != xform.shared:
xform.shared = project.shared
xform.shared_data = project.shared
xform.save()
# clear existing permissions
for perm in get_object_users_with_permissions(
xform, with_group_users=True):
user = perm['user']
role_name = perm['role']
role = ROLES.get(role_name)
if role and (user != xform.user and project.user != user and
project.created_by != user):
role.remove_obj_permissions(user, xform)
owners = project.organization.team_set.filter(
name="{}#{}".format(project.organization.username, OWNER_TEAM_NAME),
organization=project.organization)
if owners:
OwnerRole.add(owners[0], xform)
for perm in get_object_users_with_permissions(
project, with_group_users=True):
user = perm['user']
role_name = perm['role']
role = ROLES.get(role_name)
if user == xform.created_by:
OwnerRole.add(user, xform)
else:
if role:
role.add(user, xform)
示例15: test_reassign_role_owner_to_editor
# 需要导入模块: from onadata.libs.permissions import OwnerRole [as 别名]
# 或者: from onadata.libs.permissions.OwnerRole import add [as 别名]
def test_reassign_role_owner_to_editor(self):
"""
Test role reassignment owner to editor.
"""
self._publish_transportation_form()
alice = self._create_user('alice', 'alice')
self.assertFalse(OwnerRole.user_has_role(alice, self.xform))
OwnerRole.add(alice, self.xform)
self.assertTrue(OwnerRole.user_has_role(alice, self.xform))
self.assertTrue(
OwnerRole.has_role(perms_for(alice, self.xform), self.xform))
EditorRole.add(alice, self.xform)
self.assertFalse(OwnerRole.user_has_role(alice, self.xform))
self.assertTrue(EditorRole.user_has_role(alice, self.xform))
self.assertFalse(
OwnerRole.has_role(perms_for(alice, self.xform), self.xform))
self.assertTrue(
EditorRole.has_role(perms_for(alice, self.xform), self.xform))