Skip to content

Commit

Permalink
Merge pull request #133 from boxine/fix-humanize
Browse files Browse the repository at this point in the history
Bugfix `ZeroDivisionError` in TaskModel admin
  • Loading branch information
phihag authored Nov 20, 2023
2 parents 4a2de58 + bc5d62c commit 871b868
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ You must change your Django settings and replace the app name:

## History

* [dev](https://github.com/boxine/django-huey-monitor/compare/v0.8.0...main)
* [dev](https://github.com/boxine/django-huey-monitor/compare/v0.8.1...main)
* _tbc_
* [v0.8.1 - 20.11.2023](https://github.com/boxine/django-huey-monitor/compare/v0.8.0...v0.8.1)
* Bugfix `ZeroDivisionError` in admin
* [v0.8.0 - 20.11.2023](https://github.com/boxine/django-huey-monitor/compare/v0.7.1...v0.8.0)
* Make is possible to override `list_filter` of `SignalInfoModelAdmin` and `TaskModelAdmin` via settings
* Update local docker dev setup
Expand Down
2 changes: 1 addition & 1 deletion huey_monitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
Django based tool for monitoring huey task queue: https://github.com/coleifer/huey
"""

__version__ = '0.8.0'
__version__ = '0.8.1'
__author__ = 'Jens Diemer <[email protected]>'
7 changes: 6 additions & 1 deletion huey_monitor/humanize.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ def format_sizeof(num, suffix='', divisor=1000):
return f'{num:3.1f}Y' + suffix


def percentage(num, total):
def percentage(num, total) -> str | None:
"""
>>> percentage(25, 100)
'25%'
>>> percentage(33.333, 100)
'33%'
>>> percentage(123, 0)
None
"""
if not total:
return None

frac = num / total
percentage = frac * 100
return f'{percentage:.0f}%'
Expand Down
2 changes: 1 addition & 1 deletion huey_monitor/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def elapsed_sec(self):
return dt_diff.total_seconds()

def human_percentage(self):
if self.progress_count is not None and self.total is not None:
if self.progress_count is not None and self.total:
return percentage(num=self.progress_count, total=self.total)
human_percentage.short_description = _('percentage')

Expand Down
12 changes: 12 additions & 0 deletions huey_monitor_project/tests/test_huey_monitor_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.test import TestCase
from model_bakery import baker

from huey_monitor.models import TaskModel


class HueyMonitorModelsTestCase(TestCase):
def test_human_percentage(self):
instance = baker.make(TaskModel, progress_count=10, total=None)
self.assertIs(instance.human_percentage(), None)
instance.total = 100
self.assertEqual(instance.human_percentage(), '10%')

0 comments on commit 871b868

Please sign in to comment.