def create(self, validated_data): created_instances = [] for username in validated_data.pop('username').split(','): validated_data['username'] = username instance = ShareProject(**validated_data) instance.save() created_instances.append(instance) return created_instances
def restore_object(self, attrs, instance=None): if instance: return attrs_to_instance(attrs, instance) project = Project.objects.get(pk=self.init_data.get('project')) self.init_data['project'] = project return ShareProject(**self.init_data)
def test_project_all_users_can_share_remove_themselves(self): self._publish_xls_form_to_project() alice_data = {'username': '******', 'email': '*****@*****.**'} self._login_user_and_profile(alice_data) view = ProjectViewSet.as_view({ 'put': 'share' }) data = {'username': '******', 'remove': True} for role_name, role_class in role.ROLES.iteritems(): ShareProject(self.project, 'alice', role_name).save() self.assertTrue(role_class.user_has_role(self.user, self.project)) self.assertTrue(role_class.user_has_role(self.user, self.xform)) data['role'] = role_name request = self.factory.put('/', data=data, **self.extra) response = view(request, pk=self.project.pk) self.assertEqual(response.status_code, 204) self.assertFalse(role_class.user_has_role(self.user, self.project)) self.assertFalse(role_class.user_has_role(self.user, self.xform))
def restore_object(self, attrs, instance=None): if instance is not None: instance.project = attrs.get('project', instance.project) instance.username = attrs.get('username', instance.username) instance.role = attrs.get('role', instance.role) return instance return ShareProject(**attrs)
def remove_user_from_organization(organization, user): """Remove a user from an organization""" team = get_organization_members_team(organization) remove_user_from_team(team, user) owners_team = get_organization_owners_team(organization) remove_user_from_team(owners_team, user) role = get_role_in_org(user, organization) # Remove user from all org projects for project in organization.user.project_org.all(): ShareProject(project, user.username, role, remove=True).save()
def test_project_manager_can_assign_form_to_project(self): view = ProjectViewSet.as_view({ 'post': 'forms', 'get': 'retrieve' }) self._publish_xls_form_to_project() # alice user as manager to both projects alice_data = {'username': '******', 'email': '*****@*****.**'} alice_profile = self._create_user_profile(alice_data) ShareProject(self.project, 'alice', 'manager').save() self.assertTrue(ManagerRole.user_has_role(alice_profile.user, self.project)) formid = self.xform.pk old_project = self.project project_name = u'another project' self._project_create({'name': project_name}) self.assertTrue(self.project.name == project_name) ShareProject(self.project, 'alice', 'manager').save() self.assertTrue(ManagerRole.user_has_role(alice_profile.user, self.project)) self._login_user_and_profile(alice_data) project_id = self.project.pk post_data = {'formid': formid} request = self.factory.post('/', data=post_data, **self.extra) response = view(request, pk=project_id) self.assertEqual(response.status_code, 201) self.assertTrue(self.project.xform_set.filter(pk=self.xform.pk)) self.assertFalse(old_project.xform_set.filter(pk=self.xform.pk)) # check if form added appears in the project details request = self.factory.get('/', **self.extra) response = view(request, pk=self.project.pk) self.assertIn('forms', response.data.keys()) self.assertEqual(len(response.data['forms']), 1)
def _set_organization_role_to_user(organization, user, role): role_cls = ROLES.get(role) role_cls.add(user, organization) owners_team = get_organization_owners_team(organization) # add the owner to owners team if role == OwnerRole.name: add_user_to_team(owners_team, user) # add user to org projects for project in organization.user.project_org.all(): ShareProject(project, user.username, role).save() if role != OwnerRole.name: remove_user_from_team(owners_team, user)
def remove_user_from_organization(organization, user): """Remove a user from an organization""" team = get_organization_members_team(organization) remove_user_from_team(team, user) owners_team = get_organization_owners_team(organization) remove_user_from_team(owners_team, user) role = get_role_in_org(user, organization) role_cls = ROLES.get(role) if role_cls: # Remove object permissions role_cls.remove_obj_permissions(user, organization) role_cls.remove_obj_permissions(user, organization.userprofile_ptr) # Remove user from all org projects for project in organization.user.project_org.all(): ShareProject(project, user.username, role, remove=True).save()
def restore_object(self, attrs, instance=None): if instance: return attrs_to_instance(attrs, instance) return ShareProject(**attrs)
def remove_user(self): obj = ShareProject(**self.validated_data) obj.remove_user()
def create(self, validated_data): project = ShareProject(**validated_data) project.save() return project
def create(self, validated_data): instance = ShareProject(**validated_data) instance.save() return instance