Skip to content

Commit

Permalink
Merge 768bf39 into 8413ff5
Browse files Browse the repository at this point in the history
  • Loading branch information
salma-nyagaka committed Dec 20, 2018
2 parents 8413ff5 + 768bf39 commit 75053b1
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions authors/apps/articles/tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def setUp(self):
self.bookmarks_url = reverse('articles:get-bookmarks')
self.comment_url = ''#reverse("articles:comment")
self.favoritearticle_url = reverse("articles:favorite", kwargs={'slug':"salma123445"})
self.filter_url = reverse("articles:filter")

self.register_data = {
"user":{
Expand Down
38 changes: 38 additions & 0 deletions authors/apps/articles/tests/test_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.urls import reverse
from rest_framework import status
from .base_test import BaseTestCase


class TestSearchArticles(BaseTestCase):
"class to test searching and filtering of articles"

def test_search_by_title(self):
"""class to test searching by title"""

self.user_signup()
token = self.user_login()
response = self.create_article()
response = self.client.get(reverse("articles:filter"), data={'title': 'Hello'}, format='json', HTTP_AUTHORIZATION='Token ' + token)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_search_by_author(self):
"""class to test searching by author"""

self.user_signup()
token = self.user_login()
response = self.create_article()
response = self.client.get(reverse("articles:filter"), data={'author': 'JohnDoe'}, format='json', HTTP_AUTHORIZATION='Token ' + token)
self.assertEqual(response.status_code, status.HTTP_200_OK)


def test_search_by_tag(self):
"""class to test searching by author"""

self.user_signup()
token = self.user_login()
response = self.create_article()
response = self.client.get(reverse("articles:filter"), data={'tag': 'dummydata'}, format='json', HTTP_AUTHORIZATION='Token ' + token)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)



5 changes: 5 additions & 0 deletions authors/apps/articles/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from django.urls import path


from authors.apps.articles.views.articles import ArticleAPIView, SpecificArticleAPIView, LikeArticle, DislikeArticle
from authors.apps.articles.views.bookmarks import BookMarkCreateAPIView, BookMarkListAPIView
from authors.apps.articles.views.comments import CommentsListAPIView, UpdateDestroyCommentsAPIView
from authors.apps.articles.views.favorites import FavoriteArticles
from authors.apps.articles.views.ratings import RatingsAPIView
from authors.apps.articles.views.reply import ReplyListAPIView, UpdateDestroyReplyAPIView
from authors.apps.articles.views.report_articles import ReportArticleAPIView, ReportListAPIView
from authors.apps.articles.views.filters import FilterSearchViewset


urlpatterns = [
path('articles/', ArticleAPIView.as_view(), name='articles'),
Expand All @@ -23,4 +26,6 @@
path('articles/reports/', ReportListAPIView.as_view(), name='report-list'),
path('articles/<str:article_slug>/bookmark/', BookMarkCreateAPIView.as_view(), name='bookmark-article'),
path('articles/bookmarks/', BookMarkListAPIView.as_view(), name='get-bookmarks'),
path('article/', FilterSearchViewset.as_view({'get': 'list'}), name='filter'),

]
45 changes: 45 additions & 0 deletions authors/apps/articles/views/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django_filters import rest_framework as filters
from rest_framework.generics import ListAPIView
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import SearchFilter
from rest_framework.viewsets import ViewSet

from authors.apps.articles.serializers import ArticleAuthorSerializer
from authors.apps.articles.models import Article
from rest_framework.response import Response
from django.db.models import Q
from rest_framework.views import APIView
from rest_framework import status



class FilterSearchViewset(ViewSet):
"""Viewset to filter through articles."""

def list(self, request):
"""Get a list of articles with searched query."""
queryset = Article.objects.all()
queries = {
'tag': request.GET.get('tag'),
'title': request.GET.get('title'),
'author': request.GET.get('author')}
if queries['tag']:
result = queryset.filter(
Q(tags__name__icontains=queries['tag']))
elif queries['title']:
result = queryset.filter(
Q(title__icontains=queries['title']))
elif queries['author']:
result = queryset.filter(
Q(author__username__icontains=queries['author']))
else:
data = {"message": "Missing search query."}
return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
if result:
serializers = [ArticleAuthorSerializer(i).data for i in result]
return Response(data=serializers, status=status.HTTP_200_OK)
else:
data = {"message": "Your search has not been found."}
return Response(data=data, status=status.HTTP_404_NOT_FOUND)


4 changes: 4 additions & 0 deletions authors/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'social_django',
'taggit',
'authors.apps.notifications',
'django_filters',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -160,6 +161,9 @@
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_FILTER_BACKENDS': (
'django_filters.rest_framework.DjangoFilterBackend',
),
}

AUTHENTICATION_BACKENDS = (
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ urllib3==1.24.1
whitenoise==4.1.2
wrapt==1.10.11
psycopg2==2.7.6.1
django-filter==2.0.0

0 comments on commit 75053b1

Please sign in to comment.