Skip to content

Commit c2576e3

Browse files
committed
Changed the order of some util functions
1 parent e2894b5 commit c2576e3

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

simple_history/tests/tests/test_utils.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@
1010
from django.utils import timezone
1111

1212
from simple_history.exceptions import AlternativeManagerError, NotHistoricalModelError
13-
from simple_history.tests.models import (
13+
from simple_history.utils import (
14+
bulk_create_with_history,
15+
bulk_update_with_history,
16+
get_history_manager_for_model,
17+
get_history_model_for_model,
18+
get_m2m_field_name,
19+
get_m2m_reverse_field_name,
20+
update_change_reason,
21+
)
22+
23+
from ..models import (
1424
BulkCreateManyToManyModel,
1525
Document,
1626
Place,
@@ -28,19 +38,21 @@
2838
PollWithUniqueQuestion,
2939
Street,
3040
)
31-
from simple_history.utils import (
32-
bulk_create_with_history,
33-
bulk_update_with_history,
34-
get_history_manager_for_model,
35-
get_history_model_for_model,
36-
get_m2m_field_name,
37-
get_m2m_reverse_field_name,
38-
update_change_reason,
39-
)
4041

4142
User = get_user_model()
4243

4344

45+
class UpdateChangeReasonTestCase(TestCase):
46+
def test_update_change_reason_with_excluded_fields(self):
47+
poll = PollWithExcludeFields(
48+
question="what's up?", pub_date=timezone.now(), place="The Pub"
49+
)
50+
poll.save()
51+
update_change_reason(poll, "Test change reason.")
52+
most_recent = poll.history.order_by("-history_date").first()
53+
self.assertEqual(most_recent.history_change_reason, "Test change reason.")
54+
55+
4456
class GetM2MFieldNamesTestCase(unittest.TestCase):
4557
def test__get_m2m_field_name__returns_expected_value(self):
4658
def field_names(model):
@@ -629,14 +641,3 @@ def test_bulk_manager_with_custom_model_attributes(self):
629641
PollWithHistoricalSessionAttr.history.filter(session="co-op").count(),
630642
5,
631643
)
632-
633-
634-
class UpdateChangeReasonTestCase(TestCase):
635-
def test_update_change_reason_with_excluded_fields(self):
636-
poll = PollWithExcludeFields(
637-
question="what's up?", pub_date=timezone.now(), place="The Pub"
638-
)
639-
poll.save()
640-
update_change_reason(poll, "Test change reason.")
641-
most_recent = poll.history.order_by("-history_date").first()
642-
self.assertEqual(most_recent.history_change_reason, "Test change reason.")

simple_history/utils.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
from simple_history.exceptions import AlternativeManagerError, NotHistoricalModelError
66

77

8+
def get_change_reason_from_object(obj):
9+
if hasattr(obj, "_change_reason"):
10+
return getattr(obj, "_change_reason")
11+
12+
return None
13+
14+
815
def update_change_reason(instance, reason):
916
attrs = {}
1017
model = type(instance)
@@ -35,6 +42,11 @@ def get_history_manager_for_model(model):
3542
return getattr(model, manager_name)
3643

3744

45+
def get_history_model_for_model(model):
46+
"""Return the history model for a given app model."""
47+
return get_history_manager_for_model(model).model
48+
49+
3850
def get_history_manager_from_history(history_instance):
3951
"""
4052
Return the history manager, based on an existing history instance.
@@ -45,11 +57,6 @@ def get_history_manager_from_history(history_instance):
4557
)
4658

4759

48-
def get_history_model_for_model(model):
49-
"""Return the history model for a given app model."""
50-
return get_history_manager_for_model(model).model
51-
52-
5360
def get_app_model_primary_key_name(model):
5461
"""Return the primary key name for a given app model."""
5562
if isinstance(model._meta.pk, ForeignKey):
@@ -233,10 +240,3 @@ def bulk_update_with_history(
233240
custom_historical_attrs=custom_historical_attrs,
234241
)
235242
return rows_updated
236-
237-
238-
def get_change_reason_from_object(obj):
239-
if hasattr(obj, "_change_reason"):
240-
return getattr(obj, "_change_reason")
241-
242-
return None

0 commit comments

Comments
 (0)