Exemple #1
0
    def get_queryset(self):
        query = Recipe.objects
        filter_set = {}

        # If user is anonymous, restrict recipes to public.
        if not self.request.user.is_authenticated:
            filter_set['public'] = True

        if 'cuisine' in self.request.query_params:
            try:
                filter_set['cuisine__in'] = Cuisine.objects.filter(
                    slug__in=self.request.query_params.get('cuisine').split(
                        ','))
            except:
                return []

        if 'course' in self.request.query_params:
            try:
                filter_set['course__in'] = Course.objects.filter(
                    slug__in=self.request.query_params.get('course').split(
                        ','))
            except:
                return []

        if 'search' in self.request.query_params:
            query = get_search_results([
                'title', 'ingredient_groups__ingredients__title', 'tags__title'
            ], query, self.request.query_params.get('search')).distinct()

        query = query.filter(**filter_set)

        return query.values('rating').annotate(
            total=Count('id', distinct=True)).order_by('-rating')
Exemple #2
0
    def get_queryset(self):
        query = Recipe.objects

        filter_set = {}
        if 'cuisine' in self.request.query_params:
            try:
                filter_set['cuisine'] = Cuisine.objects.get(
                    slug=self.request.query_params.get('cuisine'))
            except:
                return []

        if 'course' in self.request.query_params:
            try:
                filter_set['course'] = Course.objects.get(
                    slug=self.request.query_params.get('course'))
            except:
                return []

        if 'search' in self.request.query_params:
            query = get_search_results([
                'title', 'ingredient_groups__ingredients__title', 'tags__title'
            ], query, self.request.query_params.get('search')).distinct()

        query = query.filter(**filter_set)

        return query.values('rating').annotate(
            total=Count('id', distinct=True)).order_by('-rating')
Exemple #3
0
    def get_queryset(self):
        query = Recipe.objects
        filter_set = {}

        # If user is anonymous, restrict recipes to public.
        if not self.request.user.is_authenticated:
            filter_set['public'] = True

        if 'course' in self.request.query_params:
            try:
                filter_set['course__in'] = Course.objects.filter(
                    slug__in=self.request.query_params.get('course').split(',')
                )
            except:
                return []

        if 'search' in self.request.query_params:
            query = get_search_results(
                ['title', 'ingredient_groups__ingredients__title', 'tags__title'],
                query,
                self.request.query_params.get('search')
            ).distinct()

        query = query.filter(**filter_set)
        if 'rating' in self.request.query_params:
            # TODO: this many not be very efficient on huge query sets.
            # I don't think I will ever get to the point of this mattering
            query = query.annotate(rating_avg=Avg('rating__rating'))
            query = [
                recipe.id for recipe in query
                if str(convert_rating_to_int(recipe.rating_avg)) in self.request.query_params.get('rating').split(',')
            ]

        return Cuisine.objects.filter(recipe__in=query).annotate(total=Count('recipe', distinct=True))
Exemple #4
0
    def test_get_search_no_results(self):
        """ Run a search that will return no data """
        query = get_search_results(
            ['title', 'ingredient_groups__ingredients__title', 'tags__title'],
            Recipe.objects,
            'blue berry'
        ).distinct()

        self.assertTrue(len(query.all()) == 0)
Exemple #5
0
    def get(self, request, *args, **kwargs):
        query = Recipe.objects
        filter_set = {}

        # If user is anonymous, restrict recipes to public.
        if not self.request.user.is_authenticated:
            filter_set['public'] = True

        if 'cuisine' in self.request.query_params:
            try:
                filter_set['cuisine__in'] = Cuisine.objects.filter(
                    slug__in=self.request.query_params.get('cuisine').split(
                        ','))
            except:
                return []

        if 'course' in self.request.query_params:
            try:
                filter_set['course__in'] = Course.objects.filter(
                    slug__in=self.request.query_params.get('course').split(
                        ','))
            except:
                return []

        if 'search' in self.request.query_params:
            query = get_search_results([
                'title', 'ingredient_groups__ingredients__title', 'tags__title'
            ], query, self.request.query_params.get('search')).distinct()

        results = {
            5: 0,
            4: 0,
            3: 0,
            2: 0,
            1: 0,
            0: 0,
        }
        query = query.filter(**filter_set)
        # TODO: this many not be very efficient on huge query sets.
        # I don't think I will ever get to the point of this mattering
        for x in query.annotate(rating_avg=Avg('rating__rating')):
            results[convert_rating_to_int(x.rating_avg)] += 1

        return Response({
            'results': [{
                "rating": k,
                "total": v
            } for k, v in results.items()]
        })