some fixes
This commit is contained in:
parent
b6a3aa4264
commit
43804ae5ab
|
|
@ -1,29 +1,38 @@
|
||||||
"""News app documents."""
|
"""News app documents."""
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django_elasticsearch_dsl import Document, Index, fields
|
from django_elasticsearch_dsl import Document, Index, fields
|
||||||
from news.models import News
|
from news import models
|
||||||
|
|
||||||
|
|
||||||
INDEX = Index(settings.ELASTICSEARCH_INDEX_NAMES[__name__])
|
NEWS_INDEX = Index(settings.ELASTICSEARCH_INDEX_NAMES[__name__])
|
||||||
INDEX.settings(number_of_shards=1, number_of_replicas=1)
|
NEWS_INDEX.settings(number_of_shards=1, number_of_replicas=1)
|
||||||
|
|
||||||
|
|
||||||
@INDEX.doc_type
|
@NEWS_INDEX.doc_type
|
||||||
class NewsDocument(Document):
|
class NewsDocument(Document):
|
||||||
"""News document."""
|
"""News document."""
|
||||||
|
|
||||||
|
news_type = fields.NestedField(properties={
|
||||||
|
'id': fields.IntegerField(),
|
||||||
|
'name': fields.KeywordField()
|
||||||
|
})
|
||||||
title = fields.ObjectField()
|
title = fields.ObjectField()
|
||||||
subtitle = fields.ObjectField()
|
subtitle = fields.ObjectField()
|
||||||
description = fields.ObjectField()
|
description = fields.ObjectField()
|
||||||
web_url = fields.StringField(attr='web_url')
|
country = fields.NestedField(properties={
|
||||||
|
'id': fields.IntegerField(),
|
||||||
|
'code': fields.KeywordField()
|
||||||
|
})
|
||||||
|
web_url = fields.KeywordField(attr='web_url')
|
||||||
|
|
||||||
class Django:
|
class Django:
|
||||||
|
|
||||||
model = News
|
model = models.News
|
||||||
fields = (
|
fields = (
|
||||||
'id',
|
'id',
|
||||||
'playlist',
|
'playlist',
|
||||||
)
|
)
|
||||||
|
related_models = [models.NewsType]
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return super().get_queryset().published()
|
return super().get_queryset().published()
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,9 @@ from utils.models import get_current_language
|
||||||
|
|
||||||
|
|
||||||
class NewsDocumentSerializer(DocumentSerializer):
|
class NewsDocumentSerializer(DocumentSerializer):
|
||||||
"""News document serialzier."""
|
"""News document serializer."""
|
||||||
|
|
||||||
title_translated = serializers.SerializerMethodField(method_name='get_title_translated',
|
title_translated = serializers.SerializerMethodField(allow_null=True)
|
||||||
allow_null=True)
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Meta class."""
|
"""Meta class."""
|
||||||
|
|
@ -24,5 +23,8 @@ class NewsDocumentSerializer(DocumentSerializer):
|
||||||
'title_translated',
|
'title_translated',
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_title_translated(self, instance):
|
def get_title_translated(self, obj):
|
||||||
return instance.title.get(get_current_language())
|
title_dict = obj.title
|
||||||
|
if not isinstance(title_dict, dict):
|
||||||
|
title_dict = obj.title.to_dict()
|
||||||
|
return title_dict.get(get_current_language())
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,13 @@ from search_indexes.documents import NewsDocument
|
||||||
# LOOKUP_QUERY_GT,
|
# LOOKUP_QUERY_GT,
|
||||||
# )
|
# )
|
||||||
#
|
#
|
||||||
# from django_elasticsearch_dsl_drf.filter_backends import (
|
from django_elasticsearch_dsl_drf.filter_backends import (
|
||||||
# FilteringFilterBackend,
|
FilteringFilterBackend,
|
||||||
# IdsFilterBackend,
|
IdsFilterBackend,
|
||||||
# OrderingFilterBackend,
|
OrderingFilterBackend,
|
||||||
# SearchFilterBackend,
|
SearchFilterBackend,
|
||||||
# DefaultOrderingFilterBackend,
|
DefaultOrderingFilterBackend,
|
||||||
# )
|
)
|
||||||
|
|
||||||
|
|
||||||
class NewsDocumentViewSet(BaseDocumentViewSet):
|
class NewsDocumentViewSet(BaseDocumentViewSet):
|
||||||
|
|
@ -27,3 +27,14 @@ class NewsDocumentViewSet(BaseDocumentViewSet):
|
||||||
pagination_class = PageNumberPagination
|
pagination_class = PageNumberPagination
|
||||||
permission_classes = (permissions.AllowAny,)
|
permission_classes = (permissions.AllowAny,)
|
||||||
serializer_class = serializers.NewsDocumentSerializer
|
serializer_class = serializers.NewsDocumentSerializer
|
||||||
|
ordering = ('id',)
|
||||||
|
#
|
||||||
|
filter_backends = [
|
||||||
|
# DefaultOrderingFilterBackend,
|
||||||
|
# FilteringFilterBackend,
|
||||||
|
SearchFilterBackend,
|
||||||
|
]
|
||||||
|
#
|
||||||
|
# search_fields = (
|
||||||
|
# 'playlist', 'title', 'subtitle', 'description'
|
||||||
|
# )
|
||||||
Loading…
Reference in New Issue
Block a user