def _compute_app_rating(self, days: int = -1, threshold: int = 5) -> Tuple[float, int]: """ Computes an app rating based on :param days: passing 30 will only consider ratings from the last 30 days, pass a negative number to include all ratings :param threshold: if the amount of ratings is lower than this number return 0.5 :return: the app rating """ app_ratings = AppRating.objects.filter(app=self.app) if days >= 0: range = timezone.now() - datetime.timedelta(days=days) app_ratings = app_ratings.filter(rated_at__gte=range) ratings = map(lambda r: r.rating, app_ratings) return compute_rating(list(ratings), threshold)
def _compute_app_rating(self, days: int = -1, threshold: int = 5) -> Tuple[float, int]: """ Computes an app rating based on :param days: passing 30 will only consider ratings from the last 30 days, pass a negative number to include all ratings :param threshold: if the amount of ratings is lower than this number return 0.5 :return: the app rating """ app_ratings = AppRating.objects.filter(app=self.app) if days >= 0: range = timezone.now() - datetime.timedelta(days=days) app_ratings = app_ratings.filter(rated_at__gte=range) ratings = map(lambda r: r.rating, app_ratings) return compute_rating(list(ratings), threshold)
def test_full_rating(self): result, num = compute_rating([1.0, 1.0, 0.5, 0.5], 1) self.assertEqual(0.75, result) self.assertEqual(4, num)
def test_no_ratings(self): result, num = compute_rating([], -1) self.assertEqual(0.5, result) self.assertEqual(0, num)
def test_simple_rating(self): result, num = compute_rating([1.0], 0) self.assertEqual(1.0, result) self.assertEqual(1, num)
def test_below_threshold_rating(self): result, num = compute_rating([1.0], 2) self.assertEqual(0.5, result) self.assertEqual(0, num)