Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update utils.py with adding a return of function bulk_update_with_history #1206

Merged
merged 6 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

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
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(VERSION >= (4, 0), "Requires Django 4.0 or above")
zhaojiejoe marked this conversation as resolved.
Show resolved Hide resolved
def test_bulk_update_history_row_updated(self):
zhaojiejoe marked this conversation as resolved.
Show resolved Hide resolved
row_updated = bulk_update_with_history(
zhaojiejoe marked this conversation as resolved.
Show resolved Hide resolved
self.data,
Poll,
fields=["question"],
)
self.assertEqual(row_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: rows_updated
zhaojiejoe marked this conversation as resolved.
Show resolved Hide resolved
"""
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