Initial search indexes app
This commit is contained in:
parent
cfe1c982be
commit
99011ba15a
|
|
@ -1,6 +1,7 @@
|
|||
"""News app models."""
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from rest_framework.reverse import reverse
|
||||
|
||||
from utils.models import BaseAttributes, TJSONField, TranslatedFieldsMixin
|
||||
|
||||
|
|
@ -77,3 +78,6 @@ class News(BaseAttributes, TranslatedFieldsMixin):
|
|||
def __str__(self):
|
||||
return f'news: {self.id}'
|
||||
|
||||
@property
|
||||
def web_url(self):
|
||||
return reverse('web:news:rud', kwargs={'pk': self.pk})
|
||||
|
|
|
|||
0
apps/search_indexes/__init__.py
Normal file
0
apps/search_indexes/__init__.py
Normal file
7
apps/search_indexes/apps.py
Normal file
7
apps/search_indexes/apps.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
from django.apps import AppConfig
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
||||
class SearchIndexesConfig(AppConfig):
|
||||
name = 'search_indexes'
|
||||
verbose_name = _('Search indexes')
|
||||
6
apps/search_indexes/documents/__init__.py
Normal file
6
apps/search_indexes/documents/__init__.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from search_indexes.documents.news import NewsDocument
|
||||
|
||||
|
||||
__all__ = [
|
||||
'NewsDocument',
|
||||
]
|
||||
40
apps/search_indexes/documents/news.py
Normal file
40
apps/search_indexes/documents/news.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
"""News app documents."""
|
||||
from django.conf import settings
|
||||
from django_elasticsearch_dsl import Document, Index, fields
|
||||
from news.models import News
|
||||
|
||||
|
||||
INDEX = Index(settings.ELASTICSEARCH_INDEX_NAMES[__name__])
|
||||
INDEX.settings(number_of_shards=1, number_of_replicas=1)
|
||||
|
||||
|
||||
@INDEX.doc_type
|
||||
class NewsDocument(Document):
|
||||
"""News document."""
|
||||
|
||||
title = fields.ObjectField()
|
||||
subtitle = fields.ObjectField()
|
||||
description = fields.ObjectField()
|
||||
web_url = fields.StringField(attr='web_url')
|
||||
|
||||
class Django:
|
||||
|
||||
model = News
|
||||
fields = (
|
||||
'id',
|
||||
'playlist',
|
||||
)
|
||||
|
||||
def get_queryset(self):
|
||||
return super().get_queryset().published()
|
||||
|
||||
def prepare_title(self, instance):
|
||||
return instance.title
|
||||
|
||||
def prepare_subtitle(self, instance):
|
||||
return instance.subtitle
|
||||
|
||||
def prepare_description(self, instance):
|
||||
return instance.description
|
||||
|
||||
|
||||
28
apps/search_indexes/serializers.py
Normal file
28
apps/search_indexes/serializers.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
"""Search indexes serializers."""
|
||||
from rest_framework import serializers
|
||||
from django_elasticsearch_dsl_drf.serializers import DocumentSerializer
|
||||
from search_indexes.documents.news import NewsDocument
|
||||
from utils.models import get_current_language
|
||||
|
||||
|
||||
class NewsDocumentSerializer(DocumentSerializer):
|
||||
"""News document serialzier."""
|
||||
|
||||
title_translated = serializers.SerializerMethodField(method_name='get_title_translated',
|
||||
allow_null=True)
|
||||
|
||||
class Meta:
|
||||
"""Meta class."""
|
||||
|
||||
document = NewsDocument
|
||||
fields = (
|
||||
'id',
|
||||
'title',
|
||||
'subtitle',
|
||||
'description',
|
||||
'web_url',
|
||||
'title_translated',
|
||||
)
|
||||
|
||||
def get_title_translated(self, instance):
|
||||
return instance.title.get(get_current_language())
|
||||
9
apps/search_indexes/urls.py
Normal file
9
apps/search_indexes/urls.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
"""Search indexes app urlconf."""
|
||||
from rest_framework import routers
|
||||
from search_indexes import views
|
||||
|
||||
|
||||
router = routers.SimpleRouter()
|
||||
router.register(r'news', views.NewsDocumentViewSet, basename='news')
|
||||
|
||||
urlpatterns = router.urls
|
||||
29
apps/search_indexes/views.py
Normal file
29
apps/search_indexes/views.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
"""Search indexes app views."""
|
||||
from rest_framework import permissions
|
||||
from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet
|
||||
from django_elasticsearch_dsl_drf.pagination import PageNumberPagination
|
||||
from search_indexes import serializers
|
||||
from search_indexes.documents import NewsDocument
|
||||
|
||||
# from django_elasticsearch_dsl_drf.constants import (
|
||||
# LOOKUP_QUERY_IN,
|
||||
# LOOKUP_QUERY_GT,
|
||||
# )
|
||||
#
|
||||
# from django_elasticsearch_dsl_drf.filter_backends import (
|
||||
# FilteringFilterBackend,
|
||||
# IdsFilterBackend,
|
||||
# OrderingFilterBackend,
|
||||
# SearchFilterBackend,
|
||||
# DefaultOrderingFilterBackend,
|
||||
# )
|
||||
|
||||
|
||||
class NewsDocumentViewSet(BaseDocumentViewSet):
|
||||
"""News document ViewSet."""
|
||||
|
||||
document = NewsDocument
|
||||
lookup_field = 'id'
|
||||
pagination_class = PageNumberPagination
|
||||
permission_classes = (permissions.AllowAny,)
|
||||
serializer_class = serializers.NewsDocumentSerializer
|
||||
|
|
@ -54,3 +54,15 @@ LOGGING = {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
# ELASTICSEARCH SETTINGS
|
||||
ELASTICSEARCH_DSL = {
|
||||
'default': {
|
||||
'hosts': 'elasticsearch:9200'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ELASTICSEARCH_INDEX_NAMES = {
|
||||
'search_indexes.documents.news': 'local_news',
|
||||
}
|
||||
|
|
@ -55,6 +55,7 @@ api_urlpatterns = [
|
|||
path('web/', include(('project.urls.web', 'web'), namespace='web')),
|
||||
path('back/', include(('project.urls.back', 'back'), namespace='back')),
|
||||
path('mobile/', include(('project.urls.mobile', 'mobile'), namespace='mobile')),
|
||||
path('search/', include('search_indexes.urls')),
|
||||
]
|
||||
|
||||
urlpatterns = [
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user