Skip to content

Commit

Permalink
Merge 672da6c 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 + 672da6c commit 0df5647
Show file tree
Hide file tree
Showing 6 changed files with 78 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_200_OK)



6 changes: 6 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 FilterSearchListAPIView


urlpatterns = [
path('articles/', ArticleAPIView.as_view(), name='articles'),
Expand All @@ -23,4 +26,7 @@
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/', FilterSearchListAPIView.as_view(), name='filter'),
# path('article/view/', TagListAPIView.as_view(), name='tagfilter'),

]
27 changes: 27 additions & 0 deletions authors/apps/articles/views/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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, OrderingFilter

from authors.apps.articles.serializers import ArticleAuthorSerializer
from authors.apps.articles.models import Article
from rest_framework.response import Response


class ArticlesFilter(filters.FilterSet):
author = filters.CharFilter(field_name='author__username', lookup_expr='icontains')
title = filters.CharFilter(field_name='title', lookup_expr='icontains')
tag = filters.CharFilter(field_name='tags__name', lookup_expr='icontains')


class Meta:
model = Article
fields = ['title', 'author', 'tag']

class FilterSearchListAPIView(ListAPIView):
"""class for filtering and searching articles"""
queryset = Article.objects.all()
serializer_class = ArticleAuthorSerializer
filter_backends = (SearchFilter, DjangoFilterBackend)
filterset_class = ArticlesFilter
search_fields = ('title', 'author__username', 'tags__name')
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 0df5647

Please sign in to comment.