Esempio n. 1
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)
Esempio n. 2
0
 def get(self, request, *args, **kwargs):
     """
     List all public tags.
     """
     tags = CoreTag.objects.all()
     serializer = TagSerializer(tags, many=True)
     return Response(serializer.data)
Esempio n. 3
0
 def get(self, request, tag_slug, *args, **kwargs):
     """
     Return the credential information for this tag
     """
     try:
         tag = CoreTag.objects.get(name__iexact=tag_slug)
     except CoreTag.DoesNotExist:
         return Response(['Tag does not exist'],
                         status=status.HTTP_404_NOT_FOUND)
     serializer = TagSerializer(tag)
     return Response(serializer.data)
Esempio n. 4
0
 def get(self, request, provider_uuid, identity_uuid, instance_id, *args,
         **kwargs):
     """
     List all public tags.
     """
     core_instance = get_core_instance(request, provider_uuid,
                                       identity_uuid, instance_id)
     if not core_instance:
         instance_not_found(instance_id)
     tags = core_instance.tags.all()
     serializer = TagSerializer(tags, many=True)
     return Response(serializer.data)
Esempio n. 5
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)
Esempio n. 6
0
 def get(self, request, provider_uuid, identity_uuid, instance_id,
         tag_slug, *args, **kwargs):
     """
     Return the credential information for this tag
     """
     core_instance = get_core_instance(request, provider_uuid,
                                       identity_uuid, instance_id)
     if not core_instance:
         instance_not_found(instance_id)
     try:
         tag = core_instance.tags.get(name__iexact=tag_slug)
     except CoreTag.DoesNotExist:
         return Response(['Tag does not exist'],
                         status=status.HTTP_404_NOT_FOUND)
     serializer = TagSerializer(tag)
     return Response(serializer.data)
Esempio n. 7
0
    def get(self, request, provider_uuid, identity_uuid, instance_id, tag_slug,
            *args, **kwargs):
        """
        Return the credential information for this tag
        """
        try:
            core_instance = get_core_instance(request, provider_uuid,
                                              identity_uuid, instance_id)
        except ProviderNotActive as pna:
            return inactive_provider(pna)
        except Exception as e:
            return failure_response(status.HTTP_409_CONFLICT, e.message)

        if not core_instance:
            instance_not_found(instance_id)
        try:
            tag = core_instance.tags.get(name__iexact=tag_slug)
        except CoreTag.DoesNotExist:
            return Response(['Tag does not exist'],
                            status=status.HTTP_404_NOT_FOUND)
        serializer = TagSerializer(tag)
        return Response(serializer.data)