diff --git a/apps/news/models.py b/apps/news/models.py index cfb10d39..fc07d4cd 100644 --- a/apps/news/models.py +++ b/apps/news/models.py @@ -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 \ No newline at end of file diff --git a/apps/news/serializers.py b/apps/news/serializers.py index 981ddae0..a36279b9 100644 --- a/apps/news/serializers.py +++ b/apps/news/serializers.py @@ -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', )