def post(self, request: Request, product_id: int, format: str = None): try: Product.objects.filter(id=product_id, seller=request.user).exists() except Item.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) request.data.update({"product": product_id}) serializer = ItemSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save() return Response(status=status.HTTP_204_NO_CONTENT)
def item_create(request): if request.method == 'PUT': data = JSONParser().parse(request) # create item item = Item() item_data = {} item_data['item_name'] = data['item_name'] item_data['item_price'] = data['item_price'] item_data['frequency'] = data['frequency'] item_data['classify'] = data['classify'] serializer = ItemSerializer(item, data=item_data) if serializer.is_valid(): serializer.save() item_id = item.id # create step step_list = data['steps'] for step_val in step_list: step = Step() step_data = step_val step_data['belong_item'] = item_id serializer = StepSerializer(step, step_data) if serializer.is_valid(): serializer.save() else: return JsonResponse(serializer.errors, status=400) product_list = data['product'] for product_val in product_list: item_product = Item_Product() product_data = product_val product_data['item'] = item_id product_data['product'] = Product.objects.get( name=product_data['name']).id serializer = ItemProductSerializer(item_product, product_data) if serializer.is_valid(): serializer.save() else: return JsonResponse(serializer.errors, status=400) instrument_list = data['instrument'] for instrument_val in instrument_list: item_instrument = Item_Instrument() instrument_data = instrument_val instrument_data['item'] = item_id instrument_data['instrument'] = Instrument.objects.get( name=instrument_data['name']).id serializer = ItemInstrumentSerializer(item_instrument, instrument_data) if serializer.is_valid(): serializer.save() else: return JsonResponse(serializer.errors, status=400) return JsonResponse({"text": "success"}) return JsonResponse(serializer.errors, status=400)
def patch( self, request: Request, product_id: int, item_id: int, format: str = None ): try: item = Item.objects.get( id=item_id, product__id=product_id, product__seller=request.user ) except Item.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = ItemSerializer(item, data=request.data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() return Response(status=status.HTTP_204_NO_CONTENT)
def create(self, request): Item_all_obj = ItemSerializer(data=request.data) if Item_all_obj.is_valid(): new_obj = Item_all_obj.save() new_obj_ser = ItemSerializer(new_obj) return Response(new_obj_ser.data) else: return Response(new_obj_ser.errors)
def item_list(request, format=None): """List all code items, or create a new item.""" if request.method == 'GET': items = Item.get_active() 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 ItemList(request): if request.method == 'GET': items = Items.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_detail(request, pk, format=None): """Retrieve, update or delete a code item.""" 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, partial=True) 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)
def ItemDetail(request, pk): try: item = Items.objects.get(pk=pk) except Items.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)