From c52cd822ae1648dcacec3a7186d3429a66e94f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Thu, 9 Nov 2023 10:06:14 +0100 Subject: [PATCH] Use safe_division in more places --- common/utils.py | 9 +++++++++ games/views.py | 25 ++++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 common/utils.py diff --git a/common/utils.py b/common/utils.py new file mode 100644 index 0000000..422a018 --- /dev/null +++ b/common/utils.py @@ -0,0 +1,9 @@ +def safe_division(numerator: int | float, denominator: int | float) -> int | float: + """ + Divides without triggering division by zero exception. + Returns 0 if denominator is 0. + """ + try: + return numerator / denominator + except ZeroDivisionError: + return 0 diff --git a/games/views.py b/games/views.py index 6f31f65..417f832 100644 --- a/games/views.py +++ b/games/views.py @@ -1,4 +1,5 @@ from common.time import format_duration, now as now_with_tz +from common.utils import safe_division from datetime import datetime, timedelta from django.conf import settings from django.db.models import Sum, F, Count @@ -304,17 +305,13 @@ def stats(request, year: int = 0): purchased_unfinished = all_purchased_without_refunded_this_year.filter( date_finished__isnull=True ) - if ( - purchased_unfinished.count() == 0 - or all_purchased_refunded_this_year.count() == 0 - ): - unfinished_purchases_percent = 0 - else: - unfinished_purchases_percent = int( - purchased_unfinished.count() - / all_purchased_refunded_this_year.count() - * 100 + + unfinished_purchases_percent = int( + safe_division( + purchased_unfinished.count(), all_purchased_refunded_this_year.count() ) + * 100 + ) all_finished_this_year = Purchase.objects.filter(date_finished__year=year).order_by( "date_finished" @@ -374,7 +371,7 @@ def stats(request, year: int = 0): "total_spent_currency": selected_currency, "all_purchased_this_year": all_purchased_without_refunded_this_year, "spent_per_game": int( - total_spent / all_purchased_without_refunded_this_year.count() + safe_division(total_spent, all_purchased_without_refunded_this_year.count()) ), "all_finished_this_year": all_finished_this_year, "this_year_finished_this_year": this_year_finished_this_year, @@ -385,8 +382,10 @@ def stats(request, year: int = 0): "purchased_unfinished": purchased_unfinished, "unfinished_purchases_percent": unfinished_purchases_percent, "refunded_percent": int( - all_purchased_refunded_this_year.count() - / all_purchased_this_year.count() + safe_division( + all_purchased_refunded_this_year.count(), + all_purchased_this_year.count(), + ) * 100 ), "all_purchased_refunded_this_year": all_purchased_refunded_this_year,