示例#1
0
    def test_item_serializer(self):
        items = Item.objects.all()
        serializer = ItemSerializer(data=list(items), many=True)
        serializer.is_valid()
        logging.info('Serializer data: %s' % serializer.data)

        expected_response = full_category_expected_response().get('subcategories')
        expected_response = expected_response[0].get('items')
        logging.info('Expected response: %s' % expected_response)

        self.assertEqual(serializer.data, expected_response)
示例#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 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)
示例#5
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)
示例#6
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)
示例#7
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)
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)
示例#9
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)
示例#10
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)
示例#11
0
    def post(self, request, *args, **kwargs):

        # ① user_objを取得
        token = getTokenFromHeader(self)
        user_obj = getUserByToken(token)
        # print(token)

        # ② category_objを取得
        # print("REQUEST_DATA")
        # print(request.data)
        strJsonData = request.data["jsonData"]
        jsonData = json.loads(strJsonData)
        # print(jsonData)

        categoryNumber = jsonData.pop("category")["number"]
        # print("CATEGORY_NAME" + categoryNumber)
        category_obj = Category.objects.get(number=categoryNumber)
        jsonData["category"] = category_obj.id

        # ③ title, description, adm0, adm1, adm2を取得
        # serializer.is_valid()がTrueのとき各要素を取得する

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

        serializer = ItemSerializer(data=jsonData)

        if serializer.is_valid():
            print("SERIALIZER.IS_VALID : TRUE")

            item_obj = Item.objects.create(
                title=jsonData["title"],
                user=user_obj,
                description=jsonData["description"],
                price=jsonData["price"],
                category=category_obj,
                adm0=jsonData["adm0"],
                adm1=jsonData["adm1"],
                adm2=serializer.validated_data["adm2"])

            try:
                point = jsonData["point"]
                item_obj.point = point
                item_obj.save()
            except:
                pass
            try:
                radius = jsonData["radius"]
                item_obj.radius = radius
                item_obj.save()
            except:
                pass

            url = getItemDetailUrl(item_obj.id)

            #print("ItemObject生成成功")

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

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

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

        elif serializer.is_valid() == False:
            print("SERIALIZER.IS_VALID : False")
            for ele in serializer:
                print(ele)

            return Response({"result": "fail"})
示例#12
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})