def list_articles(request): ''' Return a list of all existing articles. ''' queryset = Article.objects.all().prefetch_related('commentaries') serializer = ArticleListSerializer(instance=queryset, many=True) return Response(serializer.data)
def list_articles_for_author(request, slug): ''' Return a list of all articles for an author ''' author = get_object_or_404(Author, slug=slug) queryset = author.articles.all().prefetch_related('commentaries') serializer = ArticleListSerializer(instance=queryset, many=True) return Response(serializer.data)
def list_articles(request): ''' Return a list of all existing articles. ''' queryset = filter_and_sort_articles(request) queryset = queryset.filter(is_live=True) queryset = queryset.prefetch_related('commentaries', 'authors') serializer = ArticleListSerializer(instance=queryset, many=True) paginator = PageNumberPagination() paginator.page_size = int(request.GET.get('page_size', 10)) result_page = paginator.paginate_queryset(queryset, request) serializer = ArticleListSerializer(instance=result_page, many=True) return Response(serializer.data)
def search_articles(request): q = request.GET.get('q', '') page_size = int(request.GET.get('page_size', 25)) if q: logger.warning("Query: %s" % q) search_vector = SearchVector('title', 'abstract', 'authors__last_name') search_rank = SearchRank(search_vector, q) queryset=Article.objects.annotate(rank=search_rank) \ .order_by('-rank') \ .filter(rank__gt=0) else: queryset = Article.objects.order_by('updated')[:10] paginator = PageNumberPagination() paginator.page_size = page_size result_page = paginator.paginate_queryset(queryset, request) serializer = ArticleListSerializer(instance=result_page, many=True) return Response(serializer.data)