refactor this feature
This commit is contained in:
parent
8f66f5372d
commit
ca9475b182
|
|
@ -77,6 +77,23 @@ class UserSerializer(serializers.ModelSerializer):
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
|
class UserBaseSerializer(serializers.ModelSerializer):
|
||||||
|
"""Serializer is used to display brief information about the user."""
|
||||||
|
|
||||||
|
fullname = serializers.CharField(source='get_full_name', read_only=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
"""Meta class."""
|
||||||
|
|
||||||
|
model = models.User
|
||||||
|
fields = (
|
||||||
|
'fullname',
|
||||||
|
'cropped_image_url',
|
||||||
|
'image_url',
|
||||||
|
)
|
||||||
|
read_only_fields = fields
|
||||||
|
|
||||||
|
|
||||||
class ChangePasswordSerializer(serializers.ModelSerializer):
|
class ChangePasswordSerializer(serializers.ModelSerializer):
|
||||||
"""Serializer for model User."""
|
"""Serializer for model User."""
|
||||||
|
|
||||||
|
|
|
||||||
17
apps/news/migrations/0020_remove_news_author.py
Normal file
17
apps/news/migrations/0020_remove_news_author.py
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
# Generated by Django 2.2.4 on 2019-10-02 09:18
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('news', '0019_news_author'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='news',
|
||||||
|
name='author',
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -27,10 +27,21 @@ class NewsType(models.Model):
|
||||||
class NewsQuerySet(models.QuerySet):
|
class NewsQuerySet(models.QuerySet):
|
||||||
"""QuerySet for model News"""
|
"""QuerySet for model News"""
|
||||||
|
|
||||||
|
def with_base_related(self):
|
||||||
|
"""Return qs with related objects."""
|
||||||
|
return self.select_related('news_type', 'country').prefetch_related('tags')
|
||||||
|
|
||||||
|
def with_extended_related(self):
|
||||||
|
"""Return qs with related objects."""
|
||||||
|
return self.select_related('created_by')
|
||||||
|
|
||||||
def by_type(self, news_type):
|
def by_type(self, news_type):
|
||||||
"""Filter News by type"""
|
"""Filter News by type"""
|
||||||
return self.filter(news_type__name=news_type)
|
return self.filter(news_type__name=news_type)
|
||||||
|
|
||||||
|
def by_tags(self, tags):
|
||||||
|
return self.filter(tags__in=tags)
|
||||||
|
|
||||||
def by_country_code(self, code):
|
def by_country_code(self, code):
|
||||||
"""Filter collection by country code."""
|
"""Filter collection by country code."""
|
||||||
return self.filter(country__code=code)
|
return self.filter(country__code=code)
|
||||||
|
|
@ -42,9 +53,16 @@ class NewsQuerySet(models.QuerySet):
|
||||||
models.Q(end__isnull=True)),
|
models.Q(end__isnull=True)),
|
||||||
state__in=self.model.PUBLISHED_STATES, start__lte=now)
|
state__in=self.model.PUBLISHED_STATES, start__lte=now)
|
||||||
|
|
||||||
def with_related(self):
|
# todo: filter by best score
|
||||||
"""Return qs with related objects."""
|
# todo: filter by country?
|
||||||
return self.select_related('news_type', 'country').prefetch_related('tags')
|
def should_read(self, news):
|
||||||
|
return self.model.objects.exclude(pk=news.pk).published().\
|
||||||
|
with_base_related().by_type(news.news_type).distinct().order_by('?')
|
||||||
|
|
||||||
|
def same_theme(self, news):
|
||||||
|
return self.model.objects.exclude(pk=news.pk).published().\
|
||||||
|
with_base_related().by_type(news.news_type).\
|
||||||
|
by_tags(news.tags.all()).distinct().order_by('-start')
|
||||||
|
|
||||||
|
|
||||||
class News(BaseAttributes, TranslatedFieldsMixin):
|
class News(BaseAttributes, TranslatedFieldsMixin):
|
||||||
|
|
@ -95,15 +113,8 @@ class News(BaseAttributes, TranslatedFieldsMixin):
|
||||||
slug = models.SlugField(unique=True, max_length=50,
|
slug = models.SlugField(unique=True, max_length=50,
|
||||||
verbose_name=_('News slug'))
|
verbose_name=_('News slug'))
|
||||||
playlist = models.IntegerField(_('playlist'))
|
playlist = models.IntegerField(_('playlist'))
|
||||||
|
|
||||||
# author = models.CharField(max_length=255, blank=True, null=True,
|
|
||||||
# default=None,verbose_name=_('Author'))
|
|
||||||
|
|
||||||
state = models.PositiveSmallIntegerField(default=WAITING, choices=STATE_CHOICES,
|
state = models.PositiveSmallIntegerField(default=WAITING, choices=STATE_CHOICES,
|
||||||
verbose_name=_('State'))
|
verbose_name=_('State'))
|
||||||
author = models.CharField(max_length=255, blank=True, null=True,
|
|
||||||
default=None,verbose_name=_('Author'))
|
|
||||||
|
|
||||||
is_highlighted = models.BooleanField(default=False,
|
is_highlighted = models.BooleanField(default=False,
|
||||||
verbose_name=_('Is highlighted'))
|
verbose_name=_('Is highlighted'))
|
||||||
# TODO: metadata_keys - описание ключей для динамического построения полей метаданных
|
# TODO: metadata_keys - описание ключей для динамического построения полей метаданных
|
||||||
|
|
@ -141,54 +152,10 @@ class News(BaseAttributes, TranslatedFieldsMixin):
|
||||||
return reverse('web:news:rud', kwargs={'slug': self.slug})
|
return reverse('web:news:rud', kwargs={'slug': self.slug})
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def list_also_like_news(self):
|
def should_read(self):
|
||||||
|
return self.__class__.objects.should_read(self)[:3]
|
||||||
# without "distinct" method the doubles are arising
|
|
||||||
like_news = News.objects.published().filter(news_type=self.news_type, tags__in=models.F("tags"))\
|
|
||||||
.exclude(id=self.id).distinct()
|
|
||||||
|
|
||||||
news_count = like_news.count()
|
|
||||||
|
|
||||||
if news_count >= 6:
|
|
||||||
random_ids = random_sample(range(news_count), 6)
|
|
||||||
else:
|
|
||||||
random_ids = random_sample(range(news_count), news_count)
|
|
||||||
|
|
||||||
news_list = [{"id": like_news[r].id, "slug": like_news[r].slug} for r in random_ids]
|
|
||||||
|
|
||||||
return news_list
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def same_theme(self):
|
def same_theme(self):
|
||||||
# on the same theme news
|
return self.__class__.objects.same_theme(self)[:3]
|
||||||
|
|
||||||
# without "distinct" method the doubles are arising
|
|
||||||
like_news = News.objects.published().filter(news_type=self.news_type, tags__in=models.F("tags"))\
|
|
||||||
.order_by("-start").exclude(id=self.id).distinct()
|
|
||||||
|
|
||||||
news_count = like_news.count()
|
|
||||||
|
|
||||||
if news_count >= 3:
|
|
||||||
like_news = like_news[:3]
|
|
||||||
|
|
||||||
news_list = [{"id": n.id, "slug": n.slug} for n in like_news]
|
|
||||||
|
|
||||||
return news_list
|
|
||||||
|
|
||||||
@property
|
|
||||||
def should_read(self):
|
|
||||||
# you should read news
|
|
||||||
|
|
||||||
# without "distinct" method the doubles are arising
|
|
||||||
like_news = News.objects.published().filter(news_type=self.news_type).exclude(id=self.id).distinct()
|
|
||||||
|
|
||||||
news_count = like_news.count()
|
|
||||||
|
|
||||||
if news_count >= 3:
|
|
||||||
random_ids = random_sample(range(news_count), 3)
|
|
||||||
else:
|
|
||||||
random_ids = random_sample(range(news_count), news_count)
|
|
||||||
|
|
||||||
news_list = [{"id": like_news[r].id, "slug": like_news[r].slug} for r in random_ids]
|
|
||||||
|
|
||||||
return news_list
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
"""News app common serializers."""
|
"""News app common serializers."""
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from account.serializers.common import UserSerializer
|
from account.serializers.common import UserBaseSerializer
|
||||||
from location import models as location_models
|
from location import models as location_models
|
||||||
from location.serializers import CountrySimpleSerializer
|
from location.serializers import CountrySimpleSerializer
|
||||||
from main.serializers import MetaDataContentSerializer
|
from main.serializers import MetaDataContentSerializer
|
||||||
|
|
@ -51,8 +51,7 @@ class NewsDetailSerializer(NewsBaseSerializer):
|
||||||
|
|
||||||
description_translated = TranslatedField()
|
description_translated = TranslatedField()
|
||||||
country = CountrySimpleSerializer(read_only=True)
|
country = CountrySimpleSerializer(read_only=True)
|
||||||
# todo: check the data redundancy
|
author = UserBaseSerializer(source='created_by', read_only=True)
|
||||||
author = UserSerializer(source='created_by', read_only=True)
|
|
||||||
state_display = serializers.CharField(source='get_state_display',
|
state_display = serializers.CharField(source='get_state_display',
|
||||||
read_only=True)
|
read_only=True)
|
||||||
|
|
||||||
|
|
@ -69,6 +68,19 @@ class NewsDetailSerializer(NewsBaseSerializer):
|
||||||
'state_display',
|
'state_display',
|
||||||
'author',
|
'author',
|
||||||
'country',
|
'country',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class NewsDetailWebSerializer(NewsDetailSerializer):
|
||||||
|
"""News detail serializer for web users.."""
|
||||||
|
|
||||||
|
same_theme = NewsBaseSerializer(many=True, read_only=True)
|
||||||
|
should_read = NewsBaseSerializer(many=True, read_only=True)
|
||||||
|
|
||||||
|
class Meta(NewsDetailSerializer.Meta):
|
||||||
|
"""Meta class."""
|
||||||
|
|
||||||
|
fields = NewsDetailSerializer.Meta.fields + (
|
||||||
'same_theme',
|
'same_theme',
|
||||||
'should_read',
|
'should_read',
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ class NewsMixinView:
|
||||||
|
|
||||||
def get_queryset(self, *args, **kwargs):
|
def get_queryset(self, *args, **kwargs):
|
||||||
"""Override get_queryset method."""
|
"""Override get_queryset method."""
|
||||||
qs = models.News.objects.with_related().published()\
|
qs = models.News.objects.published().with_base_related()\
|
||||||
.order_by('-is_highlighted', '-created')
|
.order_by('-is_highlighted', '-created')
|
||||||
if self.request.country_code:
|
if self.request.country_code:
|
||||||
qs = qs.by_country_code(self.request.country_code)
|
qs = qs.by_country_code(self.request.country_code)
|
||||||
|
|
@ -28,7 +28,11 @@ class NewsDetailView(NewsMixinView, generics.RetrieveAPIView):
|
||||||
"""News detail view."""
|
"""News detail view."""
|
||||||
|
|
||||||
lookup_field = 'slug'
|
lookup_field = 'slug'
|
||||||
serializer_class = serializers.NewsDetailSerializer
|
serializer_class = serializers.NewsDetailWebSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
"""Override get_queryset method."""
|
||||||
|
return super().get_queryset().with_extended_related()
|
||||||
|
|
||||||
|
|
||||||
class NewsTypeListView(generics.ListAPIView):
|
class NewsTypeListView(generics.ListAPIView):
|
||||||
|
|
@ -44,7 +48,7 @@ class NewsBackOfficeMixinView:
|
||||||
"""News back office mixin view."""
|
"""News back office mixin view."""
|
||||||
|
|
||||||
permission_classes = (permissions.IsAuthenticated,)
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
queryset = models.News.objects.with_related() \
|
queryset = models.News.objects.with_base_related() \
|
||||||
.order_by('-is_highlighted', '-created')
|
.order_by('-is_highlighted', '-created')
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -61,6 +65,10 @@ class NewsBackOfficeLCView(NewsBackOfficeMixinView,
|
||||||
return self.create_serializers_class
|
return self.create_serializers_class
|
||||||
return super().get_serializer_class()
|
return super().get_serializer_class()
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
"""Override get_queryset method."""
|
||||||
|
return super().get_queryset().with_extended_related()
|
||||||
|
|
||||||
|
|
||||||
class NewsBackOfficeRUDView(NewsBackOfficeMixinView,
|
class NewsBackOfficeRUDView(NewsBackOfficeMixinView,
|
||||||
generics.RetrieveUpdateDestroyAPIView):
|
generics.RetrieveUpdateDestroyAPIView):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user