Skip to content

Commit

Permalink
Update utils.py with adding a return of function bulk_update_with_his…
Browse files Browse the repository at this point in the history
…tory (#1206)

* Update utils.py

keep the original returns of bulk_update

* feat: add bulk_update row_updated testcase

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Apply suggestions from code review

Co-authored-by: Anders <[email protected]>

* Update test_utils.py

fix suggestion

* Added changes in PR #1206 to changelog

---------

Co-authored-by: qcloud <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Anders <[email protected]>
  • Loading branch information
4 people authored Jul 11, 2023
1 parent 56dc4f9 commit afd02c3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down
11 changes: 11 additions & 0 deletions simple_history/tests/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from datetime import datetime
from unittest import skipUnless
from unittest.mock import Mock, patch

import django
from django.contrib.auth import get_user_model
from django.db import IntegrityError, transaction
from django.test import TestCase, TransactionTestCase, override_settings
Expand Down Expand Up @@ -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(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"],
)
self.assertEqual(rows_updated, 5)


class BulkUpdateWithHistoryAlternativeManagersTestCase(TestCase):
def setUp(self):
Expand Down
4 changes: 3 additions & 1 deletion simple_history/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,15 @@ def bulk_update_with_history(
record
:param manager: Optional model manager to use for the model instead of the default
manager
: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
if model_manager.model is not model:
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,
Expand All @@ -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):
Expand Down

0 comments on commit afd02c3

Please sign in to comment.