Esempio n. 1
0
    def create(self, validated_data):
        """
        Creates the Message in the Action model
        """
        request = self.context['request']
        target_type = validated_data.get("target_content_type")
        target_id = validated_data.get("target_object_id")
        verb = validated_data.get("verb", MESSAGE)
        try:
            content_type = get_target(target_type)
        except TargetDoesNotExist:
            raise serializers.ValidationError({
                'target_type': _('Unknown target type')
            })  # yapf: disable
        else:
            try:
                target_object = \
                    content_type.get_object_for_this_type(pk=target_id)
            except content_type.model_class().DoesNotExist:
                raise serializers.ValidationError({
                    'target_id': _('target_id not found')
                })  # yapf: disable
            else:
                # check if request.user has permission to the target_object
                permission = '{}.change_{}'.format(
                    target_object._meta.app_label,
                    target_object._meta.model_name)
                if not request.user.has_perm(permission, target_object) \
                        and verb == MESSAGE:
                    message = (_("You do not have permission to add messages "
                               "to target_id %s.") % target_object)
                    raise exceptions.PermissionDenied(detail=message)
                results = action.send(
                    request.user,
                    verb=verb,
                    target=target_object,
                    description=validated_data.get("description"))

                # results will be a list of tuples with the first item in the
                # tuple being the signal handler function and the second
                # being the object.  We want to get the object of the first
                # element in the list whose function is `action_handler`

                try:
                    instance = [
                        instance for (receiver, instance) in results
                        if receiver == action_handler
                    ].pop()
                except IndexError:
                    # if you get here it means we have no instances
                    raise serializers.ValidationError(
                        "Message not created. Please retry.")
                else:
                    return instance
Esempio n. 2
0
    def create(self, validated_data):
        """
        Creates the Message in the Action model
        """
        request = self.context['request']
        target_type = validated_data.get("target_content_type")
        target_id = validated_data.get("target_object_id")
        try:
            content_type = get_target(target_type)
        except TargetDoesNotExist:
            raise serializers.ValidationError({
                'target_type': _('Unknown target type')
            })  # yapf: disable
        else:
            try:
                target_object = \
                    content_type.get_object_for_this_type(pk=target_id)
            except content_type.model_class().DoesNotExist:
                raise serializers.ValidationError({
                    'target_id': _('target_id not found')
                })  # yapf: disable
            else:
                # check if request.user has permission to the target_object
                permission = '{}.change_{}'.format(
                    target_object._meta.app_label,
                    target_object._meta.model_name)
                if not request.user.has_perm(permission, target_object):
                    message = (_("You do not have permission to add messages "
                               "to target_id %s.") % target_object)
                    raise exceptions.PermissionDenied(detail=message)
                results = action.send(
                    request.user,
                    verb=MESSAGE,
                    target=target_object,
                    description=validated_data.get("description"))

                # results will be a list of tuples with the first item in the
                # tuple being the signal handler function and the second
                # being the object.  We want to get the object of the first
                # element in the list whose function is `action_handler`

                try:
                    instance = [
                        instance for (receiver, instance) in results
                        if receiver == action_handler
                    ].pop()
                except IndexError:
                    # if you get here it means we have no instances
                    raise serializers.ValidationError(
                        "Message not created. Please retry.")
                else:
                    return instance
Esempio n. 3
0
    def filter_queryset(self, request, queryset, view):
        """
        Return a filtered queryset.
        """

        if view.action == 'list':
            target_type = request.query_params.get('target_type')

            if target_type:
                try:
                    target = get_target(target_type)
                except TargetDoesNotExist:
                    raise exceptions.ParseError(
                        "Unknown target_type {}".format(target_type))

                return queryset.filter(target_content_type=target)

            raise exceptions.ParseError(
                _("Parameter 'target_type' is missing."))

        return queryset