Skip to content

Commit

Permalink
feat(search functionality): Add search functionality
Browse files Browse the repository at this point in the history
This commit delivers a feature that adds search
functionality and custom filtering to articles based
on parameters

[Delivers #162163278]
  • Loading branch information
Nta1e committed Dec 20, 2018
1 parent 42485c5 commit e0a6ad5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ class Meta:

class UpdateRetrieveArticleViewSerializer(serializers.ModelSerializer):
author = ProfileSerializer(read_only=True)

tagList = TagField(many=True, required=False, source='tags')
class Meta:
model = Article
"""
List all of the fields that could possibly be included in a request
or response, this includes fields specified explicitly above.
"""
fields = ['title', 'body', 'description',
fields = ['title', 'body', 'description', 'tagList',
'author', 'slug', 'published', 'created_at', 'updated_at', ]


Expand Down
9 changes: 6 additions & 3 deletions authors/apps/articles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
ArticleDeleteAPIView,
FavouritesView,
LikeArticleView,
ReadingView
ReadingView,
SearchArticlesAPIView
)

urlpatterns = [
path('articles', RetrieveArticlesAPIView.as_view(),),
path('articles', SearchArticlesAPIView.as_view()),
path('articles', RetrieveArticlesAPIView.as_view()),
path('articles/<str:slug>/like/',
LikeArticleView.as_view(), name='article-like'),
path('articles/<str:slug>/', CreateArticleView.as_view()),
path('articles/<slug>/<count>', ReadingView.as_view(), name='reading'),
path('articles/', CreateArticleView.as_view(), name='article-create'),
path('<slug>/tags/', ArticleTagsAPIView.as_view(), name='article-tags'),
path('<slug>/tags/<tag>/', ArticleDeleteAPIView.as_view(), name='delete-tag'),
path('articles/<slug>/favorite/', FavouritesView.as_view())
path('articles/<slug>/favorite/', FavouritesView.as_view()),

]
18 changes: 16 additions & 2 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

import uuid, readtime
from django.shortcuts import render
from rest_framework.filters import SearchFilter, OrderingFilter
from django_filters.rest_framework import DjangoFilterBackend
from django_filters import rest_framework as filters
from rest_framework import generics, mixins, status, viewsets
from rest_framework import status, exceptions
from rest_framework.generics import(
Expand Down Expand Up @@ -225,10 +228,8 @@ def put(self, request, slug):

class RetrieveArticlesAPIView(GenericAPIView):
serializer_class = UpdateRetrieveArticleViewSerializer

permission_classes = (IsAuthenticated,)
renderer_classes = (ListArticlesJSONRenderer,)

def get(self, request):
"""
This class method is used retrieve articles
Expand Down Expand Up @@ -492,4 +493,17 @@ def post(self, request, slug, count):
return Response(serializer.data, status=status.HTTP_200_OK)


class ArticlesFilter(filters.FilterSet):
tag = filters.CharFilter(field_name='tags__tag', lookup_expr='exact')
author = filters.CharFilter(field_name='author__user__username', lookup_expr='exact')
keyword = filters.CharFilter(field_name='title', lookup_expr='contains')

class SearchArticlesAPIView(generics.ListAPIView):
permission_classes = (AllowAny,)
serializer_class = UpdateRetrieveArticleViewSerializer
queryset = Article.objects.all()

filter_backends = (DjangoFilterBackend, SearchFilter)
filterset_class = ArticlesFilter
search_fields = ('tags__tag', 'author__user__username', 'title', 'body', 'description')

2 changes: 1 addition & 1 deletion authors/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'django_filters',
'corsheaders',
'django_extensions',
'rest_framework',
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ django-allauth==0.38.0
django-cors-headers==2.4.0
django-cors-middleware==1.3.1
django-extensions==2.1.4
django-filter==2.0.0
django-filters==0.2.1
django-heroku==0.3.1
django-rest-auth==0.9.3
djangorestframework==3.9.0
Expand Down

0 comments on commit e0a6ad5

Please sign in to comment.