From 6161561da38eed3aa038c849eca568a84166b805 Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Thu, 26 Mar 2026 10:46:48 +0330 Subject: [PATCH] feat: add Rate model to allow users to rate content across various services. and fix the round value of avg ratings. --- apps/bookmark/models/rate.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/apps/bookmark/models/rate.py b/apps/bookmark/models/rate.py index c4326d1..92481f0 100644 --- a/apps/bookmark/models/rate.py +++ b/apps/bookmark/models/rate.py @@ -17,8 +17,10 @@ class Rate(models.Model): PODCAST = 'podcast', 'Podcast' PODCAST_PLAYLIST = 'podcast_playlist', 'Podcast Playlist' HADITH = 'hadith', 'Hadith' + HADITH_CORRECTION = 'hadith_correction', 'Hadith Correction' VIDEO = 'video', 'Video' VIDEO_PLAYLIST = 'video_playlist', 'Video Playlist' + ARTICLE = 'article', 'Article' user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='rates', verbose_name='User') service = models.CharField(max_length=20, choices=ServiceChoices.choices, verbose_name='Service') @@ -101,6 +103,12 @@ class Rate(models.Model): elif service == cls.ServiceChoices.VIDEO_PLAYLIST: from apps.video.models import VideoPlaylist return VideoPlaylist.objects.filter(id=content_id).exists() + elif service == cls.ServiceChoices.ARTICLE: + from apps.article.models import Article + return Article.objects.filter(id=content_id).exists() + elif service == cls.ServiceChoices.HADITH_CORRECTION: + from apps.hadis.models import HadisCorrection + return HadisCorrection.objects.filter(id=content_id).exists() return False @classmethod @@ -121,5 +129,6 @@ class Rate(models.Model): status=True ).aggregate(avg_rate=Avg('rate')) - return result['avg_rate'] + avg_rate = result['avg_rate'] + return round(avg_rate, 2) if avg_rate is not None else None