Skip to content

Commit a58cfd3

Browse files
committed
feat: add formatted_score property and update score display in templates
1 parent 2063112 commit a58cfd3

File tree

7 files changed

+34
-32
lines changed

7 files changed

+34
-32
lines changed

src/app/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,17 @@ def process_status(self):
771771

772772
self.item.fetch_releases(delay=True)
773773

774+
@property
775+
def formatted_score(self):
776+
"""Return as int if score is 10.0 or 0.0, otherwise show decimal."""
777+
if self.score is not None:
778+
max_score = 10
779+
min_score = 0
780+
if self.score in (max_score, min_score):
781+
return int(self.score)
782+
return self.score
783+
return None
784+
774785
@property
775786
def formatted_progress(self):
776787
"""Return the progress of the media in a formatted string."""

src/app/statistics.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def get_score_distribution(user_media):
227227

228228
# Use heapq to maintain top items efficiently
229229
top_rated = []
230-
top_rated_count = 12
230+
top_rated_count = 14
231231
counter = itertools.count() # For unique identifiers
232232

233233
# Define score range (0-10)
@@ -242,25 +242,17 @@ def get_score_distribution(user_media):
242242

243243
# Process each media item
244244
for media in scored_media:
245-
# Update top rated using heap
246-
item_data = {
247-
"title": media.item.__str__(),
248-
"image": media.item.image,
249-
"score": media.score,
250-
"url": app_tags.media_url(media.item),
251-
}
252-
253245
# Use negative score for max heap (heapq implements min heap)
254246
# Add counter as tiebreaker
255247
if len(top_rated) < top_rated_count:
256248
heapq.heappush(
257249
top_rated,
258-
(float(media.score), next(counter), item_data),
250+
(float(media.score), next(counter), media),
259251
)
260252
else:
261253
heapq.heappushpop(
262254
top_rated,
263-
(float(media.score), next(counter), item_data),
255+
(float(media.score), next(counter), media),
264256
)
265257

266258
# Bin the score
@@ -280,7 +272,7 @@ def get_score_distribution(user_media):
280272

281273
# Convert heap to sorted list of top rated items
282274
top_rated = [
283-
item_data for _, _, item_data in sorted(top_rated, key=lambda x: (-x[0], x[1]))
275+
media for _, _, media in sorted(top_rated, key=lambda x: (-x[0], x[1]))
284276
]
285277

286278
return {
@@ -295,8 +287,7 @@ def get_score_distribution(user_media):
295287
],
296288
"average_score": average_score,
297289
"total_scored": total_scored,
298-
"top_rated": top_rated,
299-
}
290+
}, top_rated
300291

301292

302293
def get_status_color(status):

src/app/tests/test_statistics.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -715,14 +715,13 @@ def test_get_score_distribution(self):
715715
MediaTypes.ANIME.value: Anime.objects.filter(user=self.user),
716716
}
717717

718-
score_distribution = statistics.get_score_distribution(user_media)
718+
score_distribution, top_rated = statistics.get_score_distribution(user_media)
719719

720720
# Check structure
721721
self.assertIn("labels", score_distribution)
722722
self.assertIn("datasets", score_distribution)
723723
self.assertIn("average_score", score_distribution)
724724
self.assertIn("total_scored", score_distribution)
725-
self.assertIn("top_rated", score_distribution)
726725

727726
# Check content
728727
self.assertEqual(len(score_distribution["labels"]), 11) # Scores 0-10
@@ -737,15 +736,15 @@ def test_get_score_distribution(self):
737736

738737
# Check top rated
739738
self.assertEqual(
740-
len(score_distribution["top_rated"]),
739+
len(top_rated),
741740
2,
742741
) # Only 2 items have scores
743742
self.assertEqual(
744-
score_distribution["top_rated"][0]["score"],
743+
top_rated[0].score,
745744
8.5,
746745
) # TV should be first
747746
self.assertEqual(
748-
score_distribution["top_rated"][1]["score"],
747+
top_rated[1].score,
749748
7.5,
750749
) # Movie should be second
751750

src/app/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ def statistics(request):
842842
media_type_distribution = stats.get_media_type_distribution(
843843
media_count,
844844
)
845-
score_distribution = stats.get_score_distribution(user_media)
845+
score_distribution, top_rated = stats.get_score_distribution(user_media)
846846
status_distribution = stats.get_status_distribution(user_media)
847847
status_pie_chart_data = stats.get_status_pie_chart_data(
848848
status_distribution,
@@ -858,6 +858,7 @@ def statistics(request):
858858
"activity_data": activity_data,
859859
"media_type_distribution": media_type_distribution,
860860
"score_distribution": score_distribution,
861+
"top_rated": top_rated,
861862
"status_distribution": status_distribution,
862863
"status_pie_chart_data": status_pie_chart_data,
863864
"timeline": timeline,

src/templates/app/components/media_card.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class="w-4 h-4 text-yellow-400 mr-1 fill-current">
2323
<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"></polygon>
2424
</svg>
25-
<span class="text-sm text-white">{{ media.score }}</span>
25+
<span class="text-sm text-white">{{ media.formatted_score }}</span>
2626
</div>
2727
</div>
2828
{% endif %}

src/templates/app/components/media_table_items.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
class="w-4 h-4 text-yellow-400 mr-1 fill-current">
5959
<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"></polygon>
6060
</svg>
61-
<span class="text-white -mt-[3px]">{{ media.score }}</span>
61+
<span class="text-white -mt-[3px]">{{ media.formatted_score }}</span>
6262
</div>
6363
</td>
6464
{% else %}

src/templates/app/statistics.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -462,15 +462,15 @@ <h3 class="text-lg font-medium mb-2">No ratings yet</h3>
462462
<div class="bg-[#2a2f35] rounded-lg p-6">
463463
<h2 class="text-xl font-semibold mb-4">Top Rated Media</h2>
464464

465-
{% if score_distribution.top_rated %}
465+
{% if top_rated %}
466466
<div class="media-grid">
467-
{% for media in score_distribution.top_rated %}
467+
{% for media in top_rated %}
468468
<div class="bg-[#39404b] rounded-lg overflow-hidden shadow-lg">
469469
<div class="relative">
470-
<a href="{{ media.url }}" class="block">
471-
<img alt="{{ media.title }}"
472-
class="w-full aspect-[2/3] bg-[#3e454d] {% if media.image != IMG_NONE %}object-cover{% endif %}"
473-
src="{{ media.image }}">
470+
<a href="{{ media.item|media_url }}" class="block">
471+
<img alt="{{ media.item.title }}"
472+
class="w-full aspect-[2/3] bg-[#3e454d] {% if media.item.image != IMG_NONE %}object-cover{% endif %}"
473+
src="{{ media.item.image }}">
474474
<div class="absolute top-2 right-2 bg-black/75 px-2 py-1 rounded-md flex items-center">
475475
<svg xmlns="http://www.w3.org/2000/svg"
476476
width="24"
@@ -484,14 +484,14 @@ <h2 class="text-xl font-semibold mb-4">Top Rated Media</h2>
484484
class="w-4 h-4 text-yellow-400 fill-current mr-1">
485485
<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"></polygon>
486486
</svg>
487-
<span class="text-white text-sm font-medium">{{ media.score }}</span>
487+
<span class="text-white text-sm font-medium">{{ media.formatted_score }}</span>
488488
</div>
489489
</a>
490490
</div>
491491
<div class="p-3">
492-
<a href="{{ media.url }}"
492+
<a href="{{ media.item|media_url }}"
493493
class="font-medium text-sm text-white hover:text-indigo-400 transition duration-300 line-clamp-1"
494-
title="{{ media.title }}">{{ media.title }}</a>
494+
title="{{ media.item.title }}">{{ media.item.title }}</a>
495495
</div>
496496
</div>
497497
{% endfor %}
@@ -591,7 +591,7 @@ <h4 class="font-medium">
591591
class="w-4 h-4 mr-1 fill-current">
592592
<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"></polygon>
593593
</svg>
594-
<span>{{ media.score }}</span>
594+
<span>{{ media.formatted_score }}</span>
595595
</div>
596596
{% endif %}
597597
</div>

0 commit comments

Comments
 (0)