From cdc6ca132410a205f8120c2bb2b370b11d75c533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Thu, 9 Nov 2023 09:18:49 +0100 Subject: [PATCH] Fix potential division by zero --- games/views.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/games/views.py b/games/views.py index 5196801..6f31f65 100644 --- a/games/views.py +++ b/games/views.py @@ -304,9 +304,17 @@ def stats(request, year: int = 0): purchased_unfinished = all_purchased_without_refunded_this_year.filter( date_finished__isnull=True ) - unfinished_purchases_percent = int( - purchased_unfinished.count() / all_purchased_refunded_this_year.count() * 100 - ) + 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 + ) all_finished_this_year = Purchase.objects.filter(date_finished__year=year).order_by( "date_finished"