def update_tag(self, request, tag_slug, partial=False):
        user = request.user
        tag = CoreTag.objects.get(name__iexact=tag_slug)
        if not user.is_staff and user != tag.user:
            return Response(
                [
                    "Only the tag creator can update a tag."
                    "Contact support if you need to change "
                    "a tag that is not yours."
                ],
                status=status.HTTP_400_BAD_REQUEST
            )
        # Allowed to update tags..
        data = request.data.copy()
        if tag.user:
            data['user'] = tag.user
        else:
            data['user'] = user

        # Tag names are immutable
        data['name'] = tag.name
        serializer = TagSerializer(tag, data=data, partial=partial)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #2
0
    def post(self, request, provider_uuid, identity_uuid, instance_id, *args,
             **kwargs):
        """Create a new tag resource
        Params:name -- Name of the new Tag
        Returns:
        Status Code: 201 Body: A new Tag object
        Status Code: 400 Body: Errors (Duplicate/Invalid Name)
        """
        user = request.user
        data = request.data.copy()
        if 'name' not in data:
            return Response("Missing 'name' in POST data",
                            status=status.HTTP_400_BAD_REQUEST)

        core_instance = get_core_instance(request, provider_uuid,
                                          identity_uuid, instance_id)
        if not core_instance:
            instance_not_found(instance_id)

        same_name_tags = CoreTag.objects.filter(name__iexact=data['name'])
        if same_name_tags:
            add_tag = same_name_tags[0]
        else:
            data['user'] = user.username
            data['name'] = data['name'].lower()
            serializer = TagSerializer(data=data)
            if not serializer.is_valid():
                return Response(serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
            add_tag = serializer.save()
        core_instance.tags.add(add_tag)
        return Response(status=status.HTTP_204_NO_CONTENT)
Beispiel #3
0
    def post(self, request, provider_uuid, identity_uuid, instance_id,
             *args, **kwargs):
        """Create a new tag resource
        Params:name -- Name of the new Tag
        Returns:
        Status Code: 201 Body: A new Tag object
        Status Code: 400 Body: Errors (Duplicate/Invalid Name)
        """
        user = request.user
        data = request.data.copy()
        if 'name' not in data:
            return Response("Missing 'name' in POST data",
                            status=status.HTTP_400_BAD_REQUEST)

        core_instance = get_core_instance(request,
                                          provider_uuid,
                                          identity_uuid,
                                          instance_id)
        if not core_instance:
            instance_not_found(instance_id)

        same_name_tags = CoreTag.objects.filter(name__iexact=data['name'])
        if same_name_tags:
            add_tag = same_name_tags[0]
        else:
            data['user'] = user.username
            data['name'] = data['name'].lower()
            serializer = TagSerializer(data=data)
            if not serializer.is_valid():
                return Response(
                    serializer.errors, status=status.HTTP_400_BAD_REQUEST)
            add_tag = serializer.save()
        core_instance.tags.add(add_tag)
        return Response(status=status.HTTP_204_NO_CONTENT)
Beispiel #4
0
    def update_tag(self, request, tag_slug, partial=False):
        user = request.user
        tag = CoreTag.objects.get(name__iexact=tag_slug)
        if not user.is_staff and user != tag.user:
            return Response([
                "Only the tag creator can update a tag."
                "Contact support if you need to change "
                "a tag that is not yours."],
                status=status.HTTP_400_BAD_REQUEST)
        # Allowed to update tags..
        data = request.data.copy()
        if tag.user:
            data['user'] = tag.user
        else:
            data['user'] = user

        # Tag names are immutable
        data['name'] = tag.name
        serializer = TagSerializer(tag, data=data, partial=partial)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)