diff --git a/src/app/templatetags/app_tags.py b/src/app/templatetags/app_tags.py index 0a1b669f..d2be3117 100644 --- a/src/app/templatetags/app_tags.py +++ b/src/app/templatetags/app_tags.py @@ -433,3 +433,28 @@ def get_pagination_range(current_page, total_pages, window): result.append(total_pages) return result + +@register.filter +def show_media_score(rating): + """ + Return if we should show the rating of a media. + + Args: + rating: the rating value of the media + + Returns: + True if we should show the media score + """ + return show_media_score(rating, settings.HIDE_ZERO_RATING) + +def show_media_score(rating, hide_zero_rating): # noqa: F811 + """ + Return if we should show the rating of a media. + + Args: + rating: the rating value of the media + + Returns: + True if we should show the media score + """ + return rating is not None and (not hide_zero_rating or (hide_zero_rating and rating > 0)) # noqa: E501 diff --git a/src/app/tests/test_templatetags.py b/src/app/tests/test_templatetags.py index fe9c76bc..dbc567aa 100644 --- a/src/app/tests/test_templatetags.py +++ b/src/app/tests/test_templatetags.py @@ -356,3 +356,13 @@ class AppTagsTests(TestCase): self.assertTrue(len(inactive_result) > 0) except KeyError: self.fail(f"icon raised KeyError for {media_type}") + + def test_show_media_score(self): + """Test if we should show media rating or not.""" + self.assertTrue(app_tags.show_media_score(1, False)) # noqa: FBT003 + self.assertTrue(app_tags.show_media_score(0, False)) # noqa: FBT003 + self.assertFalse(app_tags.show_media_score(None, False)) # noqa: FBT003 + + self.assertTrue(app_tags.show_media_score(1, True)) # noqa: FBT003 + self.assertFalse(app_tags.show_media_score(0, True)) # noqa: FBT003 + self.assertFalse(app_tags.show_media_score(None, True)) # noqa: FBT003 diff --git a/src/config/settings.py b/src/config/settings.py index b70eed1c..9284ff3e 100644 --- a/src/config/settings.py +++ b/src/config/settings.py @@ -467,6 +467,8 @@ SIMKL_SECRET = config( ), ) +HIDE_ZERO_RATING = config("HIDE_ZERO_RATING", default=False, cast=bool) + TESTING = False HEALTHCHECK_CELERY_PING_TIMEOUT = config( diff --git a/src/templates/app/components/media_card.html b/src/templates/app/components/media_card.html index b36f17f0..cd0af963 100644 --- a/src/templates/app/components/media_card.html +++ b/src/templates/app/components/media_card.html @@ -46,7 +46,7 @@ {% endif %} - {% if media.score is not None %} + {% if media.score|show_media_score %}