Always return timedelta in update_game_playtime
This commit is contained in:
parent
f7e426e030
commit
6d53fca910
@ -1,3 +1,6 @@
|
|||||||
|
import logging
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
from django.db.models import F, Sum
|
from django.db.models import F, Sum
|
||||||
from django.db.models.signals import m2m_changed, post_delete, post_save, pre_save
|
from django.db.models.signals import m2m_changed, post_delete, post_save, pre_save
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
@ -5,6 +8,8 @@ from django.utils.timezone import now
|
|||||||
|
|
||||||
from games.models import Game, GameStatusChange, Purchase, Session
|
from games.models import Game, GameStatusChange, Purchase, Session
|
||||||
|
|
||||||
|
logger = logging.getLogger("games")
|
||||||
|
|
||||||
|
|
||||||
@receiver(m2m_changed, sender=Purchase.games.through)
|
@receiver(m2m_changed, sender=Purchase.games.through)
|
||||||
def update_num_purchases(sender, instance, **kwargs):
|
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 = game.sessions.aggregate(
|
||||||
total_playtime=Sum(F("duration_calculated") + F("duration_manual"))
|
total_playtime=Sum(F("duration_calculated") + F("duration_manual"))
|
||||||
)["total_playtime"]
|
)["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"])
|
game.save(update_fields=["playtime"])
|
||||||
|
|
||||||
|
|
||||||
@ -31,14 +36,18 @@ def game_status_changed(sender, instance, **kwargs):
|
|||||||
try:
|
try:
|
||||||
old_instance = sender.objects.get(pk=instance.pk)
|
old_instance = sender.objects.get(pk=instance.pk)
|
||||||
old_status = old_instance.status
|
old_status = old_instance.status
|
||||||
print("Got old instance")
|
logger.info("[game_status_changed]: Previous status exists.")
|
||||||
except sender.DoesNotExist:
|
except sender.DoesNotExist:
|
||||||
# Handle the case where the instance was deleted before the signal was sent
|
# 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
|
return
|
||||||
|
|
||||||
if old_status != instance.status:
|
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(
|
GameStatusChange.objects.create(
|
||||||
game=instance,
|
game=instance,
|
||||||
old_status=old_status,
|
old_status=old_status,
|
||||||
@ -46,6 +55,4 @@ def game_status_changed(sender, instance, **kwargs):
|
|||||||
timestamp=instance.updated_at,
|
timestamp=instance.updated_at,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print("Status not changed")
|
logger.info("[game_status_changed]: Status has not changed")
|
||||||
print(f"{old_instance.status}")
|
|
||||||
print(f"{instance.status}")
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user