def put(self, request, id): try: plant_instance = Plants.objects.get(pk=id) serializer = PlantSerializer(instance=plant_instance, data=request.data, partial=True) if serializer.is_valid(raise_exception=True): plant = serializer.save() response = api_response.APIResponse(200, plant.plantId, "Plant Updated").respond() return Response(response, status=status.HTTP_200_OK) except ValidationError as err: response = api_response.APIResponse(400, str(err), "validation error").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST) except Plants.DoesNotExist as not_found_error: response = api_response.APIResponse( 404, str(not_found_error), "plant with given id does not exist").respond() return Response(response, status=status.HTTP_404_NOT_FOUND) except Exception as e: response = api_response.APIResponse(400, str(e)).respond() return Response(response, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, userId): try: # print(request.data) cart_instances = Cart.objects.filter(customer=userId) if cart_instances.count() == 0: raise Cart.DoesNotExist() for cart_instance in cart_instances: for temp in request.data: # print(temp['plant'] == cart_instance.plant.plantId) if temp['plant'] == cart_instance.plant.plantId: print(temp) serializer = CartSerializer(instance=cart_instance, data=temp, partial=True) if serializer.is_valid(raise_exception=True): serializer.save() print(serializer.data) response = api_response.APIResponse(200, "", "Cart updated").respond() return Response(response, status=status.HTTP_200_OK) except ValidationError as e: response = api_response.APIResponse( 400, str(e), "Validation error occured").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST) except Cart.DoesNotExist as e: response = api_response.APIResponse( 404, "", "cart details not found for given user").respond() return Response(response, status=status.HTTP_404_NOT_FOUND) except Exception as e: response = api_response.APIResponse(400, str(e), "Data error").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, nurseryId): try: # print(request.data) order_instances = Order.objects.filter(seller=nurseryId) if order_instances.count() == 0: raise Order.DoesNotExist() for order_instance in order_instances: for temp in request.data: if temp['seller'] == order_instance.seller.nurseryId: print(temp) serializer = OrderSerializer(instance=order_instance, data=temp, partial=True) if serializer.is_valid(raise_exception=True): serializer.save() print(serializer.data) response = api_response.APIResponse( 200, "", "Order details changed").respond() return Response(response, status=status.HTTP_200_OK) except ValidationError as e: response = api_response.APIResponse( 400, str(e), "Validation error occured").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST) except Order.DoesNotExist as e: response = api_response.APIResponse( 404, "", "order details not found for given seller").respond() return Response(response, status=status.HTTP_404_NOT_FOUND) except Exception as e: response = api_response.APIResponse(400, str(e), "Data error").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, userId, plantId): try: cart_instances = Cart.objects.filter(customer=userId, plant=plantId) for cart_instance in cart_instances: cart_instance.delete() response = api_response.APIResponse(200, "", "Item deleted").respond() return Response(response, status=status.HTTP_200_OK) except Exception as e: response = api_response.APIResponse(400, str(e), "Data error").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): try: serializer = UserSerializer(data=request.data) if serializer.is_valid(raise_exception=True): id = serializer.save( password=make_password(request.data['password'])) response = api_response.APIResponse( 201, "", "User registered successfully").respond() return Response(response) # return Response(serializer.data, status=status.HTTP_201_CREATED) except Exception as e: response = api_response.APIResponse(400, str(e), "Data error").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, userId): try: queryset = Cart.objects.filter(customer=userId) if queryset.count() == 0: raise Cart.DoesNotExist() serializer = CartSerializer(queryset, many=True) response = api_response.APIResponse(200, serializer.data).respond() return Response(response, status=status.HTTP_200_OK) except Cart.DoesNotExist as e: response = api_response.APIResponse( 404, "", "cart details not found").respond() return Response(response, status=status.HTTP_404_NOT_FOUND) except Exception as e: response = api_response.APIResponse(400, str(e), "data error").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, userId): try: cart_instances = Cart.objects.filter(customer=userId) for cart_instance in cart_instances: cart_instance.delete() response = api_response.APIResponse(200, "", "Cart empty").respond return Response(response, status=status.HTTP_200_OK) except Cart.DoesNotExist as e: response = api_response.APIResponse( 404, "", "cart details not found for given user").respond() return Response(response, status=status.HTTP_404_NOT_FOUND) except Exception as e: response = api_response.APIResponse(400, str(e), "Data error").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, id): try: plant_instance = Plants.objects.get(pk=id) plant_instance.delete() response = api_response.APIResponse(200, "", "Plant deleted").respond() return Response(response, status=status.HTTP_200_OK) except Plants.DoesNotExist as not_found_error: response = api_response.APIResponse( 404, str(not_found_error), "plant with given id does not exist").respond() return Response(response, status=status.HTTP_404_NOT_FOUND) except Exception as e: response = api_response.APIResponse(400, str(e)).respond() return Response(response, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): try: serializer = CartSerializer(data=request.data) if serializer.is_valid(raise_exception=True): serializer.save() response = api_response.APIResponse( 200, serializer.data, "Items added to cart").respond() return Response(response, status=status.HTTP_200_OK) except ValidationError as e: response = api_response.APIResponse( 400, str(e), "Validation error occured").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST) except Exception as e: response = api_response.APIResponse(400, str(e), "Data error").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): try: data = request.data serializer = PlantSerializer(data) if serializer.is_valid(raise_exception=True): serializer.save() response = api_response.APIResponse(200, "", "plant added").respond() return Response(response, status=status.HTTP_200_OK) except ValidationError as err: response = api_response.APIResponse(400, str(err), "Validation error").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST) except Exception as e: response = api_response.APIResponse(400, str(e)).respond() return Response(response)
def get(self, request, nurseryId): try: queryset = Order.objects.filter(seller=nurseryId) if queryset.count() == 0: raise Order.DoesNotExist() serializer = OrderSerializer(queryset, many=True) response = api_response.APIResponse(200, serializer.data).respond() return Response(response, status=status.HTTP_200_OK) except Order.DoesNotExist as e: response = api_response.APIResponse(404, str(e), "No orders").respond() return Response(response, status=status.HTTP_404_NOT_FOUND) except Exception as e: response = api_response.APIResponse(400, str(e), "Data error").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): try: email = request.data['email'] password = request.data['password'] nursery = Nursery.objects.get(email=email) # print(check_password(password, nursery.password)) if nursery: if check_password(password, nursery.password): token = authentication.MyAuthentication().create_token( nursery.nurseryId, nursery.email) response = api_response.APIResponse(200, token).respond() return Response(data=response, status=status.HTTP_200_OK) else: response = api_response.APIResponse( 401, "", "password is incorrect").respond() return Response(response) except Exception as e: response = api_response.APIResponse( 400, str(e), "nursery does not exist").respond() return Response(response)
def post(self, request): try: email = request.data['email'] password = request.data['password'] user = User.objects.get(email=email) # print(user.userId) # print(check_password(password, user.password)) if user: if check_password(password, user.password): token = authentication.create_token(user.userId, email) response = api_response.APIResponse(200, token).respond() return Response(data=response, status=status.HTTP_200_OK) else: response = api_response.APIResponse( 401, "", "password is incorrect").respond() return Response(response, status=status.HTTP_401_UNAUTHORIZED) except Exception as e: response = api_response.APIResponse( 400, str(e), "User does not exist").respond() return Response(response, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, id=None): try: if id is None: queryset = Plants.objects.all() serializer = PlantSerializer(queryset, many=True) if queryset.count() == 0: response = api_response.APIResponse(200, "", "No plants").respond() else: queryset = Plants.objects.get(pk=id) serializer = PlantSerializer(queryset) response = api_response.APIResponse(200, serializer.data).respond() return Response(response, status=status.HTTP_200_OK) except Plants.DoesNotExist as not_found_error: response = api_response.APIResponse(404, str(not_found_error), "plant not found").respond() return Response(response, status=status.HTTP_404_NOT_FOUND) except Exception as e: response = api_response.APIResponse(400, str(e)).respond() return Response(response, status=status.HTTP_400_BAD_REQUEST)