added "on the same theme" news list and "you should read" news list

This commit is contained in:
michail 2019-09-30 13:17:38 +05:00
parent 755490c6cf
commit 055e99dba2
2 changed files with 36 additions and 1 deletions

View File

@ -119,3 +119,36 @@ class News(BaseAttributes, TranslatedFieldsMixin):
news_list = [{"id": like_news[r].id, "slug": like_news[r].slug} for r in random_ids]
return news_list
@property
def on_the_same_theme_news(self):
# 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 you_should_read_news(self):
# 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

View File

@ -64,7 +64,9 @@ class NewsDetailSerializer(NewsBaseSerializer):
'is_publish',
'author',
'country',
'list_also_like_news',
# 'list_also_like_news',
'on_the_same_theme_news',
'you_should_read_news',
)