本文整理汇总了Python中onadata.libs.permissions.OwnerRole类的典型用法代码示例。如果您正苦于以下问题:Python OwnerRole类的具体用法?Python OwnerRole怎么用?Python OwnerRole使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OwnerRole类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_move_project_owner
def test_move_project_owner(self):
# create project and publish form to project
self._publish_xls_form_to_project()
alice_data = {'username': 'alice', 'email': '[email protected]'}
alice_profile = self._create_user_profile(alice_data)
alice = alice_profile.user
projectid = self.project.pk
self.assertFalse(OwnerRole.user_has_role(alice, self.project))
view = ProjectViewSet.as_view({
'patch': 'partial_update'
})
data_patch = {
'owner': 'http://testserver/api/v1/users/%s' % alice.username
}
request = self.factory.patch('/', data=data_patch, **self.extra)
response = view(request, pk=projectid)
self.project.reload()
self.assertEqual(response.status_code, 200)
self.assertEquals(self.project.organization, alice)
self.assertTrue(OwnerRole.user_has_role(alice, self.project))
示例2: test_widget_permission_change
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')
示例3: update
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: set_object_permissions
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)
示例5: add_xform_to_project
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
示例6: test_org_members_added_to_projects
def test_org_members_added_to_projects(self):
# create org
self._org_create()
view = OrganizationProfileViewSet.as_view({
'post': 'members',
'get': 'retrieve',
'put': 'members'
})
# create aboy
self.profile_data['username'] = "aboy"
aboy = self._create_user_profile().user
data = {'username': 'aboy',
'role': 'owner'}
request = self.factory.post(
'/', data=json.dumps(data),
content_type="application/json", **self.extra)
response = view(request, user='denoinc')
self.assertEqual(response.status_code, 201)
# create a proj
project_data = {
'owner': self.company_data['user']
}
self._project_create(project_data)
self._publish_xls_form_to_project()
# create alice
self.profile_data['username'] = "alice"
alice = self._create_user_profile().user
alice_data = {'username': 'alice',
'role': 'owner'}
request = self.factory.post(
'/', data=json.dumps(alice_data),
content_type="application/json", **self.extra)
response = view(request, user='denoinc')
self.assertEqual(response.status_code, 201)
# Assert that user added in org is added to teams in proj
self.assertTrue(OwnerRole.user_has_role(aboy, self.project))
self.assertTrue(OwnerRole.user_has_role(alice, self.project))
self.assertTrue(OwnerRole.user_has_role(aboy, self.xform))
self.assertTrue(OwnerRole.user_has_role(alice, self.xform))
# Org admins are added to owners in project
projectView = ProjectViewSet.as_view({
'get': 'retrieve'
})
request = self.factory.get('/', **self.extra)
response = projectView(request, pk=self.project.pk)
project_users = response.data.get('users')
users_in_users = [user['user'] for user in project_users]
self.assertIn('bob', users_in_users)
self.assertIn('denoinc', users_in_users)
self.assertIn('aboy', users_in_users)
self.assertIn('alice', users_in_users)
示例7: set_object_permissions
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)
示例8: test_widget_permission_create
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: set_project_perms_to_xform
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)
示例10: test_reassign_role_owner_to_editor
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))
示例11: set_object_permissions
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))
示例12: set_object_permissions
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)
示例13: test_publish_xlsform
def test_publish_xlsform(self):
view = XFormViewSet.as_view({"post": "create"})
data = {
"owner": "http://testserver/api/v1/users/bob",
"public": False,
"public_data": False,
"description": u"transportation_2011_07_25",
"downloadable": True,
"allows_sms": False,
"encrypted": False,
"sms_id_string": u"transportation_2011_07_25",
"id_string": u"transportation_2011_07_25",
"title": u"transportation_2011_07_25",
"bamboo_dataset": u"",
}
path = os.path.join(
settings.ONADATA_DIR, "apps", "main", "tests", "fixtures", "transportation", "transportation.xls"
)
with open(path) as xls_file:
post_data = {"xls_file": xls_file}
request = self.factory.post("/", data=post_data, **self.extra)
response = view(request)
self.assertEqual(response.status_code, 201)
xform = self.user.xforms.all()[0]
data.update({"url": "http://testserver/api/v1/forms/%s" % xform.pk})
self.assertDictContainsSubset(data, response.data)
self.assertTrue(OwnerRole.user_has_role(self.user, xform))
self.assertEquals("owner", response.data["users"][0]["role"])
示例14: test_publish_xlsform
def test_publish_xlsform(self):
view = XFormViewSet.as_view({
'post': 'create'
})
data = {
'owner': 'http://testserver/api/v1/users/bob',
'public': False,
'public_data': False,
'description': u'transportation_2011_07_25',
'downloadable': True,
'allows_sms': False,
'encrypted': False,
'sms_id_string': u'transportation_2011_07_25',
'id_string': u'transportation_2011_07_25',
'title': u'transportation_2011_07_25',
'bamboo_dataset': u''
}
path = os.path.join(
settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures",
"transportation", "transportation.xls")
with open(path) as xls_file:
post_data = {'xls_file': xls_file}
request = self.factory.post('/', data=post_data, **self.extra)
response = view(request)
self.assertEqual(response.status_code, 201)
xform = self.user.xforms.all()[0]
data.update({
'url':
'http://testserver/api/v1/forms/%s' % xform.pk
})
self.assertDictContainsSubset(data, response.data)
self.assertTrue(OwnerRole.user_has_role(self.user, xform))
self.assertEquals("owner", response.data['users'][0]['role'])
示例15: test_publish_xls_form_to_organization_project
def test_publish_xls_form_to_organization_project(self):
self._org_create()
project_data = {
'owner': self.company_data['user']
}
self._project_create(project_data)
self._publish_xls_form_to_project()
self.assertTrue(OwnerRole.user_has_role(self.user, self.xform))