-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
351 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,3 +99,5 @@ db.sqlite3 | |
authors/apps/*/migrations/* | ||
|
||
logfile | ||
# search engine index directory | ||
whoosh_index/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
from django.urls import path | ||
from django.urls import path, include | ||
from . import views | ||
|
||
urlpatterns = [ | ||
path('', views.ArticleView.as_view(), name='articles'), | ||
path('<slug>/', views.ArticleSpecificFunctions.as_view(), | ||
name='articleSpecific')] | ||
name='articleSpecific'), | ||
path('q/', include('authors.apps.search.urls'), | ||
name="filter-articles") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,5 +39,4 @@ | |
'users/activate/<uidb64>/<token>', | ||
ActivateAPIView.as_view(), | ||
name='activate'), | ||
|
||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class SearchConfig(AppConfig): | ||
name = 'authors.apps.search' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from django_filters import rest_framework as filters | ||
|
||
from authors.apps.articles.models import Articles | ||
|
||
|
||
class ArticlesFilter(filters.FilterSet): | ||
# Filter for articles by date published,modified, using ISO 8601 formatted | ||
# dates | ||
publish_date = filters.IsoDateTimeFilter(field_name='created') | ||
modified_date = filters.IsoDateTimeFilter(field_name='modified') | ||
|
||
class Meta: | ||
model = Articles | ||
fields = { | ||
'title': ['exact', 'contains'], | ||
'slug': ['exact', 'contains'], | ||
'description': ['exact', 'contains'], | ||
'body': ['exact', 'contains'], | ||
'tagList': ['exact', 'contains'], | ||
'createdAt': ['exact', 'year__gt', 'year__lt'], | ||
'updatedAt': ['exact', 'year__gt', 'year__lt'], | ||
'author__username': ['exact', 'contains'], | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Create your search Indexes here. | ||
""" | ||
This file contains search indexes for Haystack. | ||
SearchIndex objects are the way Haystack determines what data should be placed | ||
in the search index and handles the flow of data in. | ||
""" | ||
import datetime | ||
|
||
from haystack import indexes | ||
from authors.apps.articles.models import Articles | ||
|
||
|
||
class ArticlesIndex(indexes.SearchIndex, indexes.Indexable): | ||
""" | ||
This class contains the Search Index for Articles | ||
""" | ||
text = indexes.CharField(document=True, use_template=False) | ||
author = indexes.CharField(model_attr="author") | ||
title = indexes.CharField(model_attr="title") | ||
tags = indexes.CharField(model_attr="tagList") | ||
keywords = indexes.CharField(model_attr="body") | ||
slug = indexes.CharField(model_attr="slug") | ||
pub_date = indexes.CharField(model_attr='createdAt') | ||
edit_date = indexes.CharField(model_attr='updatedAt') | ||
|
||
@staticmethod | ||
def prepare_author(obj): | ||
"""return the username or blank""" | ||
return '' if not obj.author else obj.author.username | ||
|
||
@staticmethod | ||
def prepare_autocomplete(obj): | ||
return " ".join(( | ||
obj.author.username, obj.title, obj.description | ||
)) | ||
|
||
def get_model(self): | ||
"""defines the model to be indexed""" | ||
return Articles | ||
|
||
def index_queryset(self, using=None): | ||
"""Used when the entire index for model is updated.""" | ||
return self.get_model().objects.filter( | ||
createdAt__lte=datetime.datetime.now()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from drf_haystack.serializers import HaystackSerializer | ||
|
||
from authors.apps.search.search_indexes import ArticlesIndex | ||
|
||
|
||
class ArticleSearchSerializer(HaystackSerializer): | ||
class Meta: | ||
"""This is the list of indices to be included in this search""" | ||
index_classes = [ArticlesIndex] | ||
# this are the fields in the Index that are included in the search | ||
fields = ['author', 'title', 'tags', 'keywords', 'autocomplete', | ||
'pub_date', 'edit_date', 'slug'] | ||
ignore_fields = ["autocomplete"] | ||
|
||
field_aliases = { | ||
"q": "autocomplete" | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import logging | ||
|
||
import pytest | ||
from django.utils.http import urlencode | ||
from rest_framework.reverse import reverse | ||
|
||
from authors.apps.authentication.tests.factories.authentication import \ | ||
ArticlesFactory | ||
|
||
logger = logging.getLogger(__file__) | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestSearchFilter: | ||
@staticmethod | ||
def reverse_querystring(view, urlconf=None, args=None, kwargs=None, | ||
current_app=None, query_kwargs=None): | ||
'''Custom reverse to handle query strings.''' | ||
base_url = reverse(view, urlconf=urlconf, args=args, kwargs=kwargs, | ||
current_app=current_app) | ||
if query_kwargs: | ||
return '{}?{}'.format(base_url, urlencode(query_kwargs)) | ||
|
||
return base_url | ||
|
||
def test_filter_endpoint_anonymous_user(self, test_client): | ||
# add an article to the db | ||
article = ArticlesFactory() | ||
# view the article | ||
logger.error(article.title) | ||
# get the url with query arguments | ||
url = self.reverse_querystring( | ||
view='filter-articles', | ||
query_kwargs={"title": article.title} | ||
) | ||
# search the DB | ||
response = test_client.get(url) | ||
# display the results | ||
logger.error(response.data) | ||
|
||
# test assertions | ||
assert response.status_code == 200 | ||
assert len(response.data) == 1 | ||
|
||
def test_filter_endpoint_authenticated_user(self, test_auth_client): | ||
# add an article to the db | ||
article = ArticlesFactory() | ||
# view the article | ||
logger.error(article.title) | ||
# get the url with query arguments | ||
url = self.reverse_querystring( | ||
view='filter-articles', | ||
query_kwargs={"title": article.title} | ||
) | ||
# search the DB | ||
response = test_auth_client.get(url) | ||
# display the results | ||
logger.error(response.data) | ||
|
||
# test assertions | ||
assert response.status_code == 200 | ||
assert len(response.data) == 1 | ||
|
||
def test_search_the_database(self, test_client): | ||
# add 3 articles to the db | ||
article = ArticlesFactory() | ||
ArticlesFactory() | ||
ArticlesFactory() | ||
|
||
url = self.reverse_querystring( | ||
view='search-articles', | ||
query_kwargs={"title": article.title} | ||
) | ||
url = url.replace('+', '%20') | ||
response = test_client.get(url) | ||
|
||
assert response.status_code == 200 | ||
assert isinstance(response.data, list) |
Oops, something went wrong.