From 232473322bbb11d052931bb98b156df1bbc2c46b Mon Sep 17 00:00:00 2001 From: Brennan Bibic Date: Wed, 14 Aug 2024 11:48:26 -0400 Subject: [PATCH] Prettyify wrs page, add wr duration --- .../templates/highscores/world_records.html | 71 ++++++++++--------- highscores/views.py | 14 ++++ 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/highscores/templates/highscores/world_records.html b/highscores/templates/highscores/world_records.html index 1eb2e9f..3c0a0ac 100644 --- a/highscores/templates/highscores/world_records.html +++ b/highscores/templates/highscores/world_records.html @@ -1,34 +1,37 @@ - - - - - World Records - {% load static %} - - - -

World Records

- - - - - - - - - - - - {% for record in world_records %} - - - - - - - - {% endfor %} - -
RankPlayerScoreGameTime Set
{{ forloop.counter }}{{ record.player.username }}{{ record.score }}{{ record.leaderboard.game }}{{ record.time_set }}
- - +{% extends 'home/base.html' %} + +{% block content %} +
+
+

+ World Records +

+
+
+ + + + + + + + + + + + + {% for record in world_records %} + + + + + + + + + {% endfor %} + +
RankPlayer NameScoreGameTime SetActive For
{{ forloop.counter }} + {{ record.player.username }} + {{ record.score }}{{ record.leaderboard.game }}{{ record.time_set }}{{ record.active_for }}
+{% endblock %} diff --git a/highscores/views.py b/highscores/views.py index 4c8a59e..cfdbbb8 100644 --- a/highscores/views.py +++ b/highscores/views.py @@ -5,6 +5,8 @@ from django.shortcuts import render from django.contrib.auth.decorators import login_required from django.db.models import Sum, Max +from django.utils.timezone import make_aware +from datetime import datetime from .lib import extract_form_data, game_slug_to_submit_func from .models import Leaderboard, Score @@ -65,6 +67,18 @@ def world_records(request: HttpRequest) -> HttpResponse: # Sort the world records by the date they were set world_records.sort(key=lambda x: x.time_set) + # Calculate how long each record has been active + for record in world_records: + now = make_aware(datetime.now()) + time_set = record.time_set + active_duration = now - time_set + + years, remainder = divmod(active_duration.total_seconds(), 31536000) # 60*60*24*365 + months, remainder = divmod(remainder, 2592000) # 60*60*24*30 + days, _ = divmod(remainder, 86400) # 60*60*24 + + record.active_for = f"{int(years)} years, {int(months)} months, {int(days)} days" + return render(request, WR_PAGE, {"world_records": world_records})