def getDetailedUser(id): userModelObj = User.objects.get(id=id) # serializer = UserReadSerializer(user) user = UserReadSerializer(userModelObj).data foods = Food.objects.filter(user=id) serializer = FoodPureSerializer(foods, many=True) data = serializer.data for food in data: FoodService.calculateRate(food) user['foods'] = data restaurants = Restaurant.objects.filter(user=id) serializer = RestaurantDetailSerializer(restaurants, many=True) data = serializer.data for restaurant in data: RestaurantService.calculateRate(restaurant) user['restaurants'] = data user['isServer'] = len(data) > 0 serializer = DietReadSerializer(userModelObj.diet_set, many=True) user['diets'] = serializer.data foodComments = FoodComment.objects.filter(user=id) user['foodComments'] = FoodCommentPureSerializer(foodComments, many=True).data foodRates = FoodRate.objects.filter(user=id) user['foodRates'] = FoodRatePureSerializer(foodRates, many=True).data restaurantComments = RestaurantComment.objects.filter(user=id) user['restaurantComments'] = RestaurantCommentPureSerializer(restaurantComments, many=True).data restaurantRates = RestaurantRate.objects.filter(user=id) user['restaurantRates'] = RestaurantRatePureSerializer(restaurantRates, many=True).data return user
def restaurant(req, restaurantId): """ Retrive, modify or delete single restaurant by id """ try: restaurant = Restaurant.objects.get(id=restaurantId) except Restaurant.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if req.method == 'GET': serializer = RestaurantDetailSerializer(restaurant) data = serializer.data RestaurantService.calculateRate(data) return Response(data) elif req.method == 'PUT': req.data['user'] = req.user.id serializer = RestaurantSerializer(restaurant, data=req.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif req.method == 'DELETE': restaurant.delete() return HttpResponse(status=status.HTTP_204_NO_CONTENT)
def searchRestaurants(q): restaurants = Restaurant.objects.filter(slug__startswith=q) if len(restaurants) == 0: restaurants = Restaurant.objects.filter(slug__contains=q) serializer = RestaurantPureSerializer(restaurants, many=True) data = serializer.data for restaurant in data: RestaurantService.calculateRate(restaurant) return serializer.data
def myRestaurants(req): """ Get all restaurants, or create a new one """ if req.method == 'GET': restaurants = Restaurant.objects.filter(user=req.user.id) serializer = RestaurantDetailSerializer(restaurants, many=True) data = serializer.data for restaurant in data: RestaurantService.calculateRate(restaurant) return Response(data)
def restaurants(req): """ Get all restaurants, or create a new one """ if req.method == 'GET': restaurants = Restaurant.objects.all() serializer = RestaurantDetailSerializer(restaurants, many=True) data = serializer.data for restaurant in data: RestaurantService.calculateRate(restaurant) return Response(data) elif req.method == 'POST': req.data['user'] = req.user.id serializer = RestaurantSerializer(data=req.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 getDetailedUser(id): userModelObj = User.objects.get(id=id) # serializer = UserReadSerializer(user) user = UserReadSerializer(userModelObj).data foods = Food.objects.filter(user=id) serializer = FoodPureSerializer(foods, many=True) data = serializer.data for food in data: FoodService.calculateRate(food) user['foods'] = data restaurants = Restaurant.objects.filter(user=id) serializer = RestaurantDetailSerializer(restaurants, many=True) data = serializer.data for restaurant in data: RestaurantService.calculateRate(restaurant) user['restaurants'] = data user['isServer'] = len(data) > 0 serializer = DietReadSerializer(userModelObj.diet_set, many=True) user['diets'] = serializer.data foodComments = FoodComment.objects.filter(user=id) user['foodComments'] = FoodCommentPureSerializer(foodComments, many=True).data foodRates = FoodRate.objects.filter(user=id) user['foodRates'] = FoodRatePureSerializer(foodRates, many=True).data restaurantComments = RestaurantComment.objects.filter(user=id) user['restaurantComments'] = RestaurantCommentPureSerializer( restaurantComments, many=True).data restaurantRates = RestaurantRate.objects.filter(user=id) user['restaurantRates'] = RestaurantRatePureSerializer(restaurantRates, many=True).data return user