Skip to content

Commit

Permalink
Merge branch 'master' of github.com:barseghyanartur/django-elasticsea…
Browse files Browse the repository at this point in the history
…rch-dsl-drf
  • Loading branch information
barseghyanartur committed Aug 30, 2019
2 parents 2b55206 + bb49df2 commit 44a5662
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/simple/factories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
from .books_order import *
from .books_orderline import *
from .books_publisher import *
from .books_tag import *
2 changes: 2 additions & 0 deletions examples/simple/search_indexes/documents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from .city import CityDocument
from .publisher import PublisherDocument
from .location import LocationDocument
from .tag import TagDocument

__all___ = (
'AddressDocument',
'AuthorDocument',
'BookDocument',
'CityDocument',
'PublisherDocument',
'TagDocument',
)
35 changes: 35 additions & 0 deletions examples/simple/search_indexes/documents/tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django.conf import settings
from django_elasticsearch_dsl import Document, Index, fields

from books.models import Tag
from .analyzers import html_strip


__all__ = ('TagDocument',)

INDEX = Index(settings.ELASTICSEARCH_INDEX_NAMES[__name__])

INDEX.settings(
number_of_shards=1,
number_of_replicas=1,
blocks={'read_only_allow_delete': None}
)


@INDEX.doc_type
class TagDocument(Document):
"""Elasticsearch document for a Tag."""

# Set unique title as the document id.
id = fields.KeywordField(attr='title')
title = fields.KeywordField()

class Django(object):
"""Django Elasticsearch DSL ORM Meta."""

model = Tag

class Meta(object):
"""Meta options."""

parellel_indexing = True
2 changes: 2 additions & 0 deletions examples/simple/search_indexes/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
PublisherDocumentSerializer,
PublisherDocumentSimpleSerializer,
)
from .tag import TagDocumentSerializer

__all__ = (
'AddressDocumentSerializer',
Expand All @@ -21,4 +22,5 @@
'LocationDocumentSerializer',
'PublisherDocumentSerializer',
'PublisherDocumentSimpleSerializer',
'TagDocumentSerializer',
)
19 changes: 19 additions & 0 deletions examples/simple/search_indexes/serializers/tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django_elasticsearch_dsl_drf.serializers import DocumentSerializer

from ..documents import TagDocument


__all__ = ('TagDocumentSerializer',)


class TagDocumentSerializer(DocumentSerializer):
"""Serializer for a Tag document."""

class Meta(object):
"""Meta options."""

document = TagDocument
fields = (
'id',
'title',
)
11 changes: 11 additions & 0 deletions examples/simple/search_indexes/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
LocationDocumentViewSet,
PublisherDocumentViewSet,
FrontAddressDocumentViewSet,
TagDocumentViewSet,
)

__all__ = ('urlpatterns',)
Expand Down Expand Up @@ -203,6 +204,16 @@
basename='publisherdocument'
)

# *********************************************************
# ************************* Tags **************************
# *********************************************************

router.register(
r'tags',
TagDocumentViewSet,
basename='tagdocument'
)

# **********************************************************
# ********************** URL patterns **********************
# **********************************************************
Expand Down
2 changes: 2 additions & 0 deletions examples/simple/search_indexes/viewsets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .city import CityDocumentViewSet, CityCompoundSearchBackendDocumentViewSet
from .location import LocationDocumentViewSet
from .publisher import PublisherDocumentViewSet
from .tag import TagDocumentViewSet

__all__ = (
'AddressDocumentViewSet',
Expand Down Expand Up @@ -50,4 +51,5 @@
'LocationDocumentViewSet',
'PublisherDocumentViewSet',
'FrontAddressDocumentViewSet',
'TagDocumentViewSet',
)
19 changes: 19 additions & 0 deletions examples/simple/search_indexes/viewsets/tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django_elasticsearch_dsl_drf.pagination import LimitOffsetPagination
from django_elasticsearch_dsl_drf.viewsets import DocumentViewSet

from ..documents import TagDocument
from ..serializers import TagDocumentSerializer

__all__ = (
'TagDocumentViewSet',
)


class TagDocumentViewSet(DocumentViewSet):
"""Document viewset for tags."""

document = TagDocument
serializer_class = TagDocumentSerializer
lookup_field = 'title'
filter_backends = []
pagination_class = LimitOffsetPagination
1 change: 1 addition & 0 deletions examples/simple/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@
'search_indexes.documents.address': 'address',
'search_indexes.documents.author': 'author',
'search_indexes.documents.book': 'book',
'search_indexes.documents.tag': 'tag',
'search_indexes.documents.city': 'city',
'search_indexes.documents.publisher': 'publisher',
'search_indexes.documents.location': 'location',
Expand Down
1 change: 1 addition & 0 deletions examples/simple/settings/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'search_indexes.documents.address': 'test_address',
'search_indexes.documents.author': 'test_author',
'search_indexes.documents.book': 'test_book',
'search_indexes.documents.tag': 'test_tag',
'search_indexes.documents.city': 'test_city',
'search_indexes.documents.publisher': 'test_publisher',
'search_indexes.documents.location': 'test_location',
Expand Down
11 changes: 11 additions & 0 deletions src/django_elasticsearch_dsl_drf/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TestViews(BaseRestFrameworkTestCase):
def setUpClass(cls):
"""Set up class."""
cls.books = factories.BookWithoutTagsAndOrdersFactory.create_batch(20)
cls.tags = factories.TagGenreFactory.create_batch(20)

call_command('search_index', '--rebuild', '-f')

Expand Down Expand Up @@ -70,6 +71,16 @@ def test_detail_view(self):
getattr(__obj, __field)
)

def test_detail_view_with_custom_lookup(self):
"""Test detail view with a custom lookup field."""
__obj = self.tags[0]
url = reverse('tagdocument-detail', kwargs={'title': __obj.title})
data = {}

response = self.client.get(url, data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['id'], __obj.title)


if __name__ == '__main__':
unittest.main()

0 comments on commit 44a5662

Please sign in to comment.