Пример #1
0
 def post(self, request):
     helpCenter = json.loads(request.query_params.get('data', {}))
     serializer = ItemSerializer(data=helpCenter)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #2
0
 def put(self, request, itemId):
     item = self.get_object(itemId)
     serializer = ItemSerializer(item, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #3
0
    def post(self, request):
        serializer = ItemSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #4
0
 def put(self, request, pk, format=None):
     item = self.get_object(pk)
     serializer = ItemSerializer(item, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #5
0
 def post(self, request):
     serializer = ItemSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         response_data = {}
         response_data["success"] = "True"
         response_data["message"] = "Settings updated successfully."
         return Response(response_data, status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #6
0
def item_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        items = Item.objects.all()
        serializer = ItemSerializer(items, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = ItemSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #7
0
def item_list(request, pk=0):
    """
    List all code item, or create a new sitem.
    """
    if request.method == 'GET':
        items = Item.objects.all()
        if int(pk) > 0:
            items = Item.objects.filter(pk=pk)
        serializer = ItemSerializer(items, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = ItemSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Пример #8
0
def item_detail(request, pk):
    """
    Retrieve, update or delete a code snippet.
    """
    try:
        item = Item.objects.get(pk=pk)
    except Item.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = ItemSerializer(item)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = ItemSerializer(item, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        item.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Пример #9
0
 def post(self, request, format=None):
     serializer = ItemSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #10
0
    def patch(self, request, *args, **kwargs):
        """
        endpoint: /api/items/<int:pk>/
        name: -
        """
        """テスト項目
        タイトルのみの変更の場合にはタイトルが変更される。
        カテゴリーだけの変更の場合にはカテゴリーの変更が反映される。

        """
        pk = self.kwargs["pk"]
        item_obj = Item.objects.get(id=pk)

        print("ここ通っている???")
        print(request.data)

        strJsonData = request.data["jsonData"]
        jsonData = json.loads(strJsonData)

        # categoryName = jsonData.pop("category")["name"]
        # print("CATEGORY_NAME" + categoryName)
        # category_obj = Category.objects.get(name=categoryName)

        imagesData = request.FILES
        # print("IMAGES_DATA    :   ", imagesData)

        isChanged = self.isChangedCategoryValue(item_obj, jsonData)
        if isChanged is True:
            category_obj = Category.objects.get(
                number=jsonData["category"]["number"])
            item_obj.category = category_obj
            item_obj.save()

        serializer = ItemSerializer(item_obj, data=jsonData, partial=True)
        print("is_valid? : ", serializer.is_valid())
        if serializer.is_valid():
            serializer.save()

            itemId = serializer.instance.id
            url = getItemDetailUrl(itemId)

            if len(request.FILES.keys()) == 0:
                return Response({"result": "success", "detail": url})

            # if len(request.FILES.keys()) > 0:
            print("TRUEが🐸")
            # item_objに画像データを追加修正する。
            try:
                serializer.instance.image1 = imagesData[
                    SerializerContextKey.IMAGE1]
                # item_obj.image1 = imagesData[SerializerContextKey.IMAGE1]
            except:
                pass
            try:
                serializer.instance.image2 = imagesData[
                    SerializerContextKey.IMAGE2]
                # item_obj.image2 = imagesData[SerializerContextKey.IMAGE2]
            except:
                pass
            try:
                serializer.instance.image3 = imagesData[
                    SerializerContextKey.IMAGE3]
                # item_obj.image3 = imagesData[SerializerContextKey.IMAGE3]
            except:
                pass

            serializer.instance.save()
            return Response({"result": "success", "detail": url})

        return Response({"result": "success", "detail": url})