Example #1
0
 def get_queryset(self):
     """
     Optionally restricts the returned books to a given authors or published date,
     by filtering against a `author` and `published_date` query parameters in the URL.
     """
     queryset = Book.objects.all()
     ordering = self.request.query_params.get('sort', None)
     authors = self.request.query_params.getlist('author', None)
     published_date = self.request.query_params.get('published_date', None)
     if published_date:
         queryset = queryset.filter(published_date=published_date)
     if authors:
         queryset_list = [queryset.filter(authors__icontains=author) for author in authors if author is not None]
         queryset = QuerySet.union(*queryset_list)
     if ordering:
         queryset = queryset.order_by(ordering)
     return queryset
Example #2
0
def search(request):
    if request.method == 'GET':
        if request.GET.get("searchbar",False):
            search = request.GET.get("searchbar")
            result_sets = []
            author_results = Thread.objects.filter(author__username__icontains=search)
            if author_results:
                result_sets.append(author_results)
            thread_results = Thread.objects.filter(tbody__body__icontains=search)
            if thread_results:
                result_sets.append(thread_results)
            results = QuerySet.union(*result_sets)
            print(results[0])
            return render(request,
                          'search_results.html',
                          {'results':results})
        form = forms.SimpleSearchForm()
        return render(request,
                      'search.html',
                      {'form':form})
Example #3
0
    def get_queryset(self):
        filter_id = self.request.query_params.get('id')
        filter_author = self.request.query_params.get('author')
        filter_post = self.request.query_params.get('post')
        filter_content = self.request.query_params.get('content')

        # If ID is provided, get that comment
        if filter_id:
            return Comment.objects.filter(comment_id__exact=comment_id)
        # otherwise build a queryset with all matching comment
        queryset = QuerySet(model=Comment)
        if filter_author:
            queryset.union(Comment.objects.filter(author__exact=filter_author))
        if filter_post:
            queryset.union(Comment.objects.filter(post__exact=filter_post))
        if filter_content:
            queryset.union(Comment.objects.filter(content__contains=filter_content))
        if not queryset:
            return Comment.objects.all()
        return queryset
Example #4
0
    def get_queryset(self):
        filter_id = self.request.query_params.get('id')
        filter_title = self.request.query_params.get('title')
        filter_author = self.request.query_params.get('author')
        filter_type = self.request.query_params.get('type')

        # If ID is provided, get that post
        if filter_id:
            return Post.objects.filter(post_id__exact=filter_id)
        # otherwise build a queryset with all matching posts
        queryset = QuerySet(model=Post)
        if filter_title:
            queryset.union(Post.objects.filter(title__contains=filter_title))
        if filter_author:
            queryset.union(Post.objects.filter(author__exact=filter_author))
        if filter_type:
            queryset.union(Post.objects.filter(post_type__exact=filter_type))
        if not queryset:
            return Post.objects.all()
        return queryset
    def _ingredient_intersect(self, ing_qs_list):
        """Returns a QuerySet of recipes shared between ingredients"""

        #return QuerySet.intersection(*ing_qs_list)
        return QuerySet.union(*ing_qs_list)