def food_detailed_view(request, id): if request.method == 'GET': try: food_detailed = Food.objects.get(id=id) except: return JsonResponse({"404": "no food"}, safe=False) serializer = FoodSerializer(food_detailed) return JsonResponse(serializer.data, safe=False) elif request.method == 'PUT': try: food_detailed = Food.objects.get(id=id) except: return JsonResponse({"404": "no food"}, safe=False) food_detailed.name = request.data.get('name') food_detailed.price = request.data.get('price') food_detailed.description = request.data.get('description') food_detailed.ingredients = request.data.get('ingredients') food_detailed.save() serializer = FoodSerializer(food_detailed) return JsonResponse(serializer.data, safe=False) elif request.method == 'DELETE': try: food_detailed = Food.objects.get(id=id) except: return JsonResponse({"404": "no food"}, safe=False) food_detailed.delete() return JsonResponse({"204": "deleted food"}, safe=False)
def food_list_view(request): if request.method == 'GET': try: food_list = Food.objects.all() except: return JsonResponse({"404": "no food"}, safe=False) serializer = FoodSerializer(food_list, many=True) return JsonResponse(serializer.data, safe=False) if request.method == 'POST': try: category = Category.objects.get(name = request.data.get('category')) except: category = Category.objects.create(name = request.data.get('category')) try: food = Food.objects.create( name = request.data.get('name'), category = category, price = request.data.get('price'), description = request.data.get('name'), ingredients = request.data.get('name'), images = request.data.get('image') ) except: return JsonResponse({"500": "cant create food"}, safe=False) return JsonResponse(FoodSerializer(food).data, safe=False)
def food_list(request): """Return all food records owned by the current user""" if request.method == 'GET': try: foods = Food.objects.filter(owner=request.user) except TypeError as e: print(e.msg) return HttpResponse(status=403) serializer = FoodSerializer(foods, many=True) return Response(serializer.data) elif request.method == 'POST': new_food = Food.objects.create(name=request.POST['name'], owner=request.user) serializer = FoodSerializer(new_food) return Response(serializer.data, status=status.HTTP_201_CREATED)
def create(self, request): food_info = json.loads(request.body)['food'] food = Food.objects.create(id=food_info['id'], name=food_info['name'], calories=food_info['calories']) serializer = FoodSerializer(food) return Response(serializer.data, status=status.HTTP_201_CREATED)
def create(self, request): food_attrs = json.loads(request.body)['food'] food = Food(name=food_attrs['name'], calories=food_attrs['calories']) food.save() serializer = FoodSerializer(food) return Response(serializer.data)
def update(self, request, food_id=None): food = Food.objects.get(id=food_id) food_info = json.loads(request.body)['food'] food.name = food_info['name'] food.calories = food_info['calories'] food.save() serializer = FoodSerializer(food) return Response(serializer.data)
def get(self, request, id): try: category = Category.objects.get(id=id) food_list = category.food_set.all() except: return JsonResponse({"404": "no food"}, safe=False) serializer = FoodSerializer(food_list, many=True) return JsonResponse(serializer.data, safe=False)
def create(self, request): food_attrs = request.data['food'] if 'name' in food_attrs.keys() and 'calories' in food_attrs.keys(): food = Food.objects.create(name=food_attrs['name'], calories=food_attrs['calories']) serializer = FoodSerializer(food) return Response(serializer.data) else: return Response(status=status.HTTP_400_BAD_REQUEST)
def partial_update(self, request, food_id=None): foods = Food.objects.all() food = get_object_or_404(foods, id=food_id) food_attrs = json.loads(request.body)['food'] food.name = food_attrs['name'] food.calories=food_attrs['calories'] food.save() serializer = FoodSerializer(food) return Response(serializer.data)
def food_detail(request, pk): """Return details for the food with the given pk""" if request.method == 'GET': try: food = Food.objects.get(pk=pk) except TypeError as e: print(e.msg) return HttpResponse(status=403) serializer = FoodSerializer(food) return Response(serializer.data)
def partial_update(self, request, food_id=None): food = get_object_or_404(Food, id=food_id) food_attrs = request.data['food'] if 'name' in food_attrs.keys() and 'calories' in food_attrs.keys(): food.name = food_attrs['name'] food.calories = food_attrs['calories'] food.save() serializer = FoodSerializer(food) return Response(serializer.data) else: return Response(status=status.HTTP_400_BAD_REQUEST)
def list(self, request, *args, **kargs): queryset = Food.objects.all() serializer = FoodSerializer(queryset, many=True, context={'request': request}) key_set = set(request.GET.keys()) print request.GET if "tops" in request.GET: tops = request.GET["tops"] #print foodDB.objects.get(rating=2).rating #g = foodDB.objects.get(rating=2) #g.rating = 0 #g.save() #print "Qwe" queryset = Food.objects.all() #serializer = FoodSerializer(queryset, many=True, context={'request':request}) topTen = [] all = [(item['name'], item['rating']) for item in serializer.data] all.sort(key=lambda x: x[1], reverse=True) if len(all) >= tops: all = all[0:tops - 1] for item in all: topTen.append(highestRating(item[0], item[1])) topTenSer = RatingSerializer(topTen, many=True) #print c.data return Response(topTenSer.data) if len(serializer.data) > 0: temp_set = key_set.intersection(serializer.data[0].keys()) temp = [] for food in serializer.data: flag = True for key in list(temp_set): if str(food[key]) != str(request.GET[key]): flag = False if flag: temp.append(food) return Response(temp) return Response({'status': status.HTTP_400_BAD_REQUEST})
def food_create_admin(request): try: category = Category.objects.get(name = request.data.get('category')) except: category = Category.objects.create(name = request.data.get('category')) try: food = Food.objects.create( name = request.data.get('name'), category = category, price = request.data.get('price'), description = request.data.get('name'), ingredients = request.data.get('name'), images = request.data.get('image') ) except: return JsonResponse({"500": "cant create food"}, safe=False) return JsonResponse(FoodSerializer(food).data, safe=False)
def list(self, request): queryset = Menu.objects.all() serializer = MenuSerializer(queryset, many=True, context={'request': request}) queryset1 = Food.objects.all() serializer1 = FoodSerializer(queryset1, many=True, context={'request': request}) temp = [] key_set = set(request.GET.keys()) if len(serializer.data) > 0: temp_set = key_set.intersection(serializer.data[0].keys()) for menu in serializer.data: flag = True for key in list(temp_set): if menu[key] != request.GET[key]: flag = False if flag: temp.append(menu) menu['food'] = [] for item in serializer1.data: print item['menuID'] print menu['menuID'] if item['menuID'] == menu['menuID']: print "Qwe" menu['food'].append(item) #print temp[0]['menuID'] #dtemp["food"] = temp return Response(temp) else: return Response(serializer.data)
def retrieve(self, request, food_id=None): foods = Food.objects.all() food = get_object_or_404(foods, id=food_id) serializer = FoodSerializer(food) return Response(serializer.data)
def list(self, request): foods = Food.objects.all() serializer = FoodSerializer(foods, many=True) return Response(serializer.data)
def retrieve(self, request, food_id=None): food = Food.objects.get(id=food_id) serializer = FoodSerializer(food) return Response(serializer.data)
def retrieve(self, request, food_id=None): food = get_object_or_404(Food, id=food_id) serializer = FoodSerializer(food, many=False) return Response(serializer.data)