From 6d53fca910d49897c1d5637cca978a12db76643a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Wed, 26 Mar 2025 12:05:10 +0100 Subject: [PATCH] Always return timedelta in update_game_playtime --- games/signals.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/games/signals.py b/games/signals.py index 5775ccf..ea1a3ee 100644 --- a/games/signals.py +++ b/games/signals.py @@ -1,3 +1,6 @@ +import logging +from datetime import timedelta + from django.db.models import F, Sum from django.db.models.signals import m2m_changed, post_delete, post_save, pre_save from django.dispatch import receiver @@ -5,6 +8,8 @@ from django.utils.timezone import now from games.models import Game, GameStatusChange, Purchase, Session +logger = logging.getLogger("games") + @receiver(m2m_changed, sender=Purchase.games.through) def update_num_purchases(sender, instance, **kwargs): @@ -19,7 +24,7 @@ def update_game_playtime(sender, instance, **kwargs): total_playtime = game.sessions.aggregate( total_playtime=Sum(F("duration_calculated") + F("duration_manual")) )["total_playtime"] - game.playtime = total_playtime if total_playtime else 0 + game.playtime = total_playtime if total_playtime else timedelta(0) game.save(update_fields=["playtime"]) @@ -31,14 +36,18 @@ def game_status_changed(sender, instance, **kwargs): try: old_instance = sender.objects.get(pk=instance.pk) old_status = old_instance.status - print("Got old instance") + logger.info("[game_status_changed]: Previous status exists.") except sender.DoesNotExist: # Handle the case where the instance was deleted before the signal was sent - print("Instance does not exist") + logger.info("[game_status_changed]: Previous status does not exist.") return if old_status != instance.status: - print("Status changed") + logger.info( + "[game_status_changed]: Status changed from {} to {}".format( + old_status, instance.status + ) + ) GameStatusChange.objects.create( game=instance, old_status=old_status, @@ -46,6 +55,4 @@ def game_status_changed(sender, instance, **kwargs): timestamp=instance.updated_at, ) else: - print("Status not changed") - print(f"{old_instance.status}") - print(f"{instance.status}") + logger.info("[game_status_changed]: Status has not changed")