def put(self, request, pk=None): dish = request.get_object(pk) serializer = DishSerializer(instance=dish, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def put(self, request, id): dish = self.get_object(id) serializer = DishSerializer(instance=dish, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'error': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get(self, request, format=None): try: search_query = request.query_params.get('search') dishes = Dish().search_dish(search_query) if not dishes == None: return Response(DishSerializer(dishes, many=True).data) return Response(None) except: return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_retrieving_dish_list(self): """Test for retrieving a list of all dishes""" sample_dishes() res = self.client.get(DISH_URL) dishes = Dish.objects.all() serializer = DishSerializer(dishes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def test_ignoring_upper_lower_case_in_search(self): """Test that uppercase or lowercase are ignored in a search""" sample_dishes() res = self.client.get(DISH_URL, {'search': 'sCHniTzEl'}) dish = Dish.objects.filter(name='Schnitzel') serializer = DishSerializer(dish, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def test_searching_partial_dish_names(self): """Test for searching a partial dish name""" sample_dishes() res = self.client.get(DISH_URL, {'search': 'ghetti'}) dish = Dish.objects.filter(name='Spaghetti') serializer = DishSerializer(dish, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def get(self, request, format=None): try: dish = Dish().random_dish() if dish == None: return Response(status=status.HTTP_400_BAD_REQUEST) ingredients = DishIngredientSerializer( DishIngredient.objects.filter(dish_id=dish.id), many=True).data dish_data = DishSerializer(dish, many=False).data return Response({'dish': dish_data, 'ingredients': ingredients}) except: return Response(status=status.HTTP_400_BAD_REQUEST)
def put(self, request, format=None): try: dish_id = int(request.query_params.get('id', '')) event = int(request.query_params.get('rate', '')) dish = Dish.objects.get(id=dish_id) return Response({ 'result': dish.rate(event), 'dish': DishSerializer(dish, many=False).data }) except: return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_filtering_dishes_by_name_asc(self): """Test for filtering dishes by name ascending""" sample_dishes() res = self.client.get(DISH_URL, {'ordering': 'name'}) dishes = Dish.objects.all().order_by('name') serializer = DishSerializer(dishes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def test_searching_for_dish_name(self): """Test for searching for a dish name""" sample_dishes() res = self.client.get(DISH_URL, {'search': 'Schnitzel'}) dish = Dish.objects.filter(name='Schnitzel') serializer = DishSerializer(dish, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def get(self, request, format=None): try: ingredients = request.query_params.get('ingredients') if ingredients == None: return Response( {'message': 'Query params cann\'t equals null'}, status=status.HTTP_400_BAD_REQUEST) chains = DishIngredient() dishes = chains.get_chains(ingredients.split(',')) return Response(DishSerializer(dishes, many=True).data) except: return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def dish_list(request): if request.method == 'GET': dish = Dish.objects.filter(name__contains='5').order_by('-id') serializer = DishSerializer(dish, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = DishSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def get(self, request, format=None): try: dish_id = request.query_params.get('id') if dish_id == None: return Response( {'message': 'Query params cann\'t equals null'}, status=status.HTTP_400_BAD_REQUEST) recipe = Dish.objects.get(id=dish_id) ingredients = DishIngredient.objects.filter(dish_id=dish_id) return Response({ 'recipe': DishSerializer(recipe, many=False).data, 'ingredients': DishIngredientSerializer(ingredients, many=True).data }) except: return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def dish_detail(request, about_id): try: about = Dish.objects.get(id=about_id) except Dish.DoesNotExist as e: return Response({'message': str(e)}, status=400) if request.method == 'GET': serializer = DishSerializer(about) return Response(serializer.data) elif request.method == 'PUT': serializer = DishSerializer(instance=about, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors) elif request.method == 'DELETE': about.delete() return Response({'message': 'deleted'}, status=204)
def get(self, request, pk=None): dish = request.get_object(pk) serializer = DishSerializer(dish) return Response(serializer.data)
def post(self, request): serializer = DishSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def get(self, request): dishes = Dish.objects.all() serializer = DishSerializer(dishes, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def get(self, request, id): menu = self.get_object(id) dishes = menu.dishes serializer = DishSerializer(dishes, many=True) return Response(serializer.data)