From 75f0100bcbb3a16cdbef0ff3ce1990978b73e42d Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 10 Jul 2023 13:44:23 +0800 Subject: [PATCH 1/6] Update utils.py keep the original returns of bulk_update --- simple_history/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/simple_history/utils.py b/simple_history/utils.py index a3b405bc0..9efe460bb 100644 --- a/simple_history/utils.py +++ b/simple_history/utils.py @@ -173,6 +173,7 @@ def bulk_update_with_history( record :param manager: Optional model manager to use for the model instead of the default manager + :return: rows_updated """ history_manager = get_history_manager_for_model(model) model_manager = manager or model._default_manager @@ -180,7 +181,7 @@ def bulk_update_with_history( raise AlternativeManagerError("The given manager does not belong to the model.") with transaction.atomic(savepoint=False): - model_manager.bulk_update(objs, fields, batch_size=batch_size) + rows_updated = model_manager.bulk_update(objs, fields, batch_size=batch_size) history_manager.bulk_history_create( objs, batch_size=batch_size, @@ -189,6 +190,7 @@ def bulk_update_with_history( default_change_reason=default_change_reason, default_date=default_date, ) + return rows_updated def get_change_reason_from_object(obj): From f2be1da887942047d128c4de03982a696500e3b3 Mon Sep 17 00:00:00 2001 From: qcloud Date: Mon, 10 Jul 2023 15:19:39 +0800 Subject: [PATCH 2/6] feat: add bulk_update row_updated testcase --- simple_history/tests/tests/test_utils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/simple_history/tests/tests/test_utils.py b/simple_history/tests/tests/test_utils.py index 0b629a439..119b6329d 100644 --- a/simple_history/tests/tests/test_utils.py +++ b/simple_history/tests/tests/test_utils.py @@ -1,10 +1,12 @@ from datetime import datetime from unittest.mock import Mock, patch +from unittest import skipUnless from django.contrib.auth import get_user_model from django.db import IntegrityError, transaction from django.test import TestCase, TransactionTestCase, override_settings from django.utils import timezone +from django import VERSION from simple_history.exceptions import AlternativeManagerError, NotHistoricalModelError from simple_history.tests.models import ( @@ -423,6 +425,15 @@ def test_bulk_update_history_with_batch_size(self): self.assertEqual(Poll.objects.count(), 5) self.assertEqual(Poll.history.filter(history_type="~").count(), 5) + @skipUnless(VERSION >= (4, 0), "Requires Django 4.0 or above") + def test_bulk_update_history_row_updated(self): + row_updated = bulk_update_with_history( + self.data, + Poll, + fields=["question"], + ) + self.assertEqual(row_updated, 5) + class BulkUpdateWithHistoryAlternativeManagersTestCase(TestCase): def setUp(self): From db40024ceb926fb1c6fd360e21050945b3835d1d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Jul 2023 07:31:47 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- simple_history/tests/tests/test_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simple_history/tests/tests/test_utils.py b/simple_history/tests/tests/test_utils.py index 119b6329d..b3fde022f 100644 --- a/simple_history/tests/tests/test_utils.py +++ b/simple_history/tests/tests/test_utils.py @@ -1,12 +1,12 @@ from datetime import datetime -from unittest.mock import Mock, patch from unittest import skipUnless +from unittest.mock import Mock, patch +from django import VERSION from django.contrib.auth import get_user_model from django.db import IntegrityError, transaction from django.test import TestCase, TransactionTestCase, override_settings from django.utils import timezone -from django import VERSION from simple_history.exceptions import AlternativeManagerError, NotHistoricalModelError from simple_history.tests.models import ( From db41bcc9b4507d320092c2e8cb6d062a6cf515e0 Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 11 Jul 2023 09:17:16 +0800 Subject: [PATCH 4/6] Apply suggestions from code review Co-authored-by: Anders <6058745+ddabble@users.noreply.github.com> --- simple_history/tests/tests/test_utils.py | 6 +++--- simple_history/utils.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/simple_history/tests/tests/test_utils.py b/simple_history/tests/tests/test_utils.py index b3fde022f..ba43c06b8 100644 --- a/simple_history/tests/tests/test_utils.py +++ b/simple_history/tests/tests/test_utils.py @@ -425,9 +425,9 @@ def test_bulk_update_history_with_batch_size(self): self.assertEqual(Poll.objects.count(), 5) self.assertEqual(Poll.history.filter(history_type="~").count(), 5) - @skipUnless(VERSION >= (4, 0), "Requires Django 4.0 or above") - def test_bulk_update_history_row_updated(self): - row_updated = bulk_update_with_history( + @skipUnless(django.VERSION >= (4, 0), "Requires Django 4.0 or above") + def test_bulk_update_with_history_returns_rows_updated(self): + rows_updated = bulk_update_with_history( self.data, Poll, fields=["question"], diff --git a/simple_history/utils.py b/simple_history/utils.py index 9efe460bb..d74a91d83 100644 --- a/simple_history/utils.py +++ b/simple_history/utils.py @@ -173,7 +173,7 @@ def bulk_update_with_history( record :param manager: Optional model manager to use for the model instead of the default manager - :return: rows_updated + :return: The number of model rows updated, not including any history objects """ history_manager = get_history_manager_for_model(model) model_manager = manager or model._default_manager From d1b8ca4239cc2844e09777913c815de1d477a842 Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 11 Jul 2023 09:28:31 +0800 Subject: [PATCH 5/6] Update test_utils.py fix suggestion --- simple_history/tests/tests/test_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simple_history/tests/tests/test_utils.py b/simple_history/tests/tests/test_utils.py index ba43c06b8..7fc497fd8 100644 --- a/simple_history/tests/tests/test_utils.py +++ b/simple_history/tests/tests/test_utils.py @@ -2,7 +2,7 @@ from unittest import skipUnless from unittest.mock import Mock, patch -from django import VERSION +import django from django.contrib.auth import get_user_model from django.db import IntegrityError, transaction from django.test import TestCase, TransactionTestCase, override_settings @@ -432,7 +432,7 @@ def test_bulk_update_with_history_returns_rows_updated(self): Poll, fields=["question"], ) - self.assertEqual(row_updated, 5) + self.assertEqual(rows_updated, 5) class BulkUpdateWithHistoryAlternativeManagersTestCase(TestCase): From 1ac8b61935b23222fa327c7b009c0f63ddffa989 Mon Sep 17 00:00:00 2001 From: Anders <6058745+ddabble@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:58:32 +0200 Subject: [PATCH 6/6] Added changes in PR #1206 to changelog --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index f92426f3e..c19a7ff8b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -14,6 +14,7 @@ Unreleased - Dropped support for Python 3.7, which reached end-of-life on 2023-06-27 (gh-1202) - Dropped support for Django 4.0, which reached end-of-life on 2023-04-01 (gh-1202) - Added support for Django 4.2 (gh-1202) +- Made ``bulk_update_with_history()`` return the number of model rows updated (gh-1206) 3.3.0 (2023-03-08) ------------------