예제 #1
0
    def share(self, request, *args, **kwargs):
        self.object = self.get_object()
        data = dict(request.DATA.items() + [('project', self.object.pk)])
        serializer = ShareProjectSerializer(data=data)

        if serializer.is_valid():
            serializer.save()
        else:
            return Response(data=serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        return Response(status=status.HTTP_204_NO_CONTENT)
예제 #2
0
    def share(self, request, *args, **kwargs):
        self.object = self.get_object()
        data = merge_dicts(request.data.dict(), {'project': self.object.pk})

        remove = data.get("remove")
        if remove and remove is not isinstance(remove, bool):
            remove = strtobool(remove)

        if remove:
            serializer = RemoveUserFromProjectSerializer(data=data)
        else:
            serializer = ShareProjectSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            email_msg = data.get('email_msg')
            if email_msg:
                # send out email message.
                user = serializer.instance.user
                send_mail(SHARE_PROJECT_SUBJECT.format(self.object.name),
                          email_msg,
                          DEFAULT_FROM_EMAIL,
                          (user.email, ))
        else:
            return Response(data=serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        return Response(status=status.HTTP_204_NO_CONTENT)
예제 #3
0
    def share(self, request, *args, **kwargs):
        self.object = self.get_object()
        data = dict(request.data.items() + [('project', self.object.pk)])
        serializer = ShareProjectSerializer(data=data)

        if serializer.is_valid():
            if data.get("remove"):
                serializer.remove_user()
            else:
                serializer.save()
                email_msg = data.get('email_msg')

                if email_msg:
                    # send out email message.
                    # serializer.object doesn't exist anymore in DRF, so
                    # we have to pull the user from the DB
                    username = serializer.validated_data['username']
                    user = User.objects.get(username=username)
                    send_mail(SHARE_PROJECT_SUBJECT.format(self.object.name),
                              email_msg, DEFAULT_FROM_EMAIL, (user.email, ))

        else:
            return Response(data=serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        return Response(status=status.HTTP_204_NO_CONTENT)
예제 #4
0
    def share(self, request, *args, **kwargs):
        self.object = self.get_object()
        data = dict(request.data.items() + [('project', self.object.pk)])
        serializer = ShareProjectSerializer(data=data)

        if serializer.is_valid():
            if data.get("remove"):
                serializer.remove_user()
            else:
                serializer.save()
                email_msg = data.get('email_msg')

                if email_msg:
                    # send out email message.
                    # serializer.object doesn't exist anymore in DRF, so
                    # we have to pull the user from the DB
                    username = serializer.validated_data['username']
                    user = User.objects.get(username=username)
                    send_mail(SHARE_PROJECT_SUBJECT.format(self.object.name),
                              email_msg,
                              DEFAULT_FROM_EMAIL,
                              (user.email, ))

        else:
            return Response(data=serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        return Response(status=status.HTTP_204_NO_CONTENT)
예제 #5
0
    def share(self, request, *args, **kwargs):
        self.object = self.get_object()
        data = dict(request.DATA.items() + [('project', self.object.pk)])
        serializer = ShareProjectSerializer(data=data)

        if serializer.is_valid():
            if data.get("remove"):
                serializer.remove_user()
            else:
                serializer.save()
                email_msg = data.get('email_msg')

                if email_msg:
                    # send out email message.
                    user = serializer.object.user
                    send_mail(SHARE_PROJECT_SUBJECT.format(self.object.name),
                              email_msg,
                              DEFAULT_FROM_EMAIL,
                              (user.email, ))

        else:
            return Response(data=serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        return Response(status=status.HTTP_204_NO_CONTENT)
예제 #6
0
    def test_shares_project(self):
        """
        Test that the ShareProjectSerializer shares the projects to users
        """
        self._publish_xls_form_to_project()
        project = Project.objects.last()

        user_joe = self._create_user('joe', 'joe')

        self.assertFalse(ReadOnlyRole.user_has_role(user_joe, project))

        data = {
            'project': project.id,
            'username': '******',
            'role': ReadOnlyRole.name
        }

        serializer = ShareProjectSerializer(data=data)
        self.assertTrue(serializer.is_valid())
        serializer.save()
        self.assertTrue(ReadOnlyRole.user_has_role(user_joe, project))

        # Test that it can share to multiple users
        user_dave = self._create_user('dave', 'dave')
        user_jake = self._create_user('jake', 'jake')

        self.assertFalse(ReadOnlyRole.user_has_role(user_dave, project))
        self.assertFalse(ReadOnlyRole.user_has_role(user_jake, project))

        data = {
            'project': project.id,
            'username': '******',
            'role': ReadOnlyRole.name
        }

        serializer = ShareProjectSerializer(data=data)
        self.assertTrue(serializer.is_valid())
        serializer.save()
        self.assertTrue(ReadOnlyRole.user_has_role(user_dave, project))
        self.assertTrue(ReadOnlyRole.user_has_role(user_jake, project))
예제 #7
0
    def test_error_on_non_existing_user(self):
        """
        Test that an error is raised when user(s) passed does not
        exist
        """

        self._publish_xls_form_to_project()

        project = Project.objects.last()

        data = {
            'project': project.id,
            'username': '******',
            'role': ReadOnlyRole.name
        }

        serializer = ShareProjectSerializer(data=data)
        self.assertFalse(serializer.is_valid())
        self.assertEqual(str(serializer.errors['username'][0]),
                         "The following user(s) does/do not exist: doe")

        data = {
            'project': project.id,
            'username': '******',
            'role': ReadOnlyRole.name
        }

        serializer = ShareProjectSerializer(data=data)
        self.assertFalse(serializer.is_valid())
        self.assertEqual(str(serializer.errors['username'][0]),
                         "The following user(s) does/do not exist: doe, john")
예제 #8
0
    def share(self, request, *args, **kwargs):
        self.object = self.get_object()
        data = dict(request.DATA.items() + [('project', self.object.pk)])
        serializer = ShareProjectSerializer(data=data)

        if serializer.is_valid():
            if data.get("remove"):
                serializer.remove_user()
            else:
                serializer.save()
        else:
            return Response(data=serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        return Response(status=status.HTTP_204_NO_CONTENT)
예제 #9
0
    def share(self, request, *args, **kwargs):
        self.object = self.get_object()
        data = dict(request.DATA.items() + [('project', self.object.pk)])
        if data.get("remove"):
            serializer = RemoveUserFromProjectSerializer(data=data)
        else:
            serializer = ShareProjectSerializer(data=data)

        if serializer.is_valid():
            serializer.save()

            email_msg = data.get('email_msg')

            if email_msg:
                # send out email message.
                user = serializer.object.user
                send_mail(SHARE_PROJECT_SUBJECT.format(self.object.name),
                          email_msg, DEFAULT_FROM_EMAIL, (user.email, ))

        else:
            return Response(data=serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        return Response(status=status.HTTP_204_NO_CONTENT)
예제 #10
0
    def test_error_on_share_to_owner(self):
        """
        Test that a validation error is raised when
        trying to share a project to the owner
        """

        self._publish_xls_form_to_project()

        project = Project.objects.last()

        data = {
            'project': project.id,
            'username': self.user.username,
            'role': ReadOnlyRole.name
        }

        serializer = ShareProjectSerializer(data=data)
        self.assertFalse(serializer.is_valid())
        self.assertEqual(
            str(serializer.errors['username'][0]),
            "Cannot share project with the owner (%(value)s)" %
            {"value": self.user.username})

        # Test that this fails even when multiple users are passed
        self._create_user('joe', 'joe')
        self._create_user('jake', 'jake')

        data = {
            'project': project.id,
            'username': '******' % {
                "user": self.user.username,
            },
            'role': ReadOnlyRole.name
        }

        serializer = ShareProjectSerializer(data=data)
        self.assertFalse(serializer.is_valid())
        self.assertEqual(
            str(serializer.errors['username'][0]),
            "Cannot share project with the owner (%(value)s)" %
            {"value": self.user.username})
예제 #11
0
    def test_error_on_inactive_user(self):
        """
        Test that an error is raised when user(s) passed does not
        exist
        """

        self._publish_xls_form_to_project()

        project = Project.objects.last()
        user_dave = self._create_user('dave', 'dave')
        user_dave.is_active = False
        user_dave.save()

        data = {
            'project': project.id,
            'username': '******',
            'role': ReadOnlyRole.name
        }

        serializer = ShareProjectSerializer(data=data)
        self.assertFalse(serializer.is_valid())
        self.assertEqual(str(serializer.errors['username'][0]),
                         "The following user(s) is/are not active: dave")

        user_john = self._create_user('john', 'john')
        user_john.is_active = False
        user_john.save()

        data = {
            'project': project.id,
            'username': '******',
            'role': ReadOnlyRole.name
        }

        serializer = ShareProjectSerializer(data=data)
        self.assertFalse(serializer.is_valid())
        self.assertEqual(
            str(serializer.errors['username'][0]),
            "The following user(s) is/are not active: dave, john")