Skip to content

Commit a6fddd6

Browse files
author
Ross Mechanic
authored
GH-341: Warning if HistoricalRecords(inherit=False) in an abstract class (#472)
* First commit for issue #341 - Warning if inerhit is set to False in an abstract class * First commit for issue #341 - Warning if inerhit is set to False in an abstract class * Fixed flake8 errors and tested that warning is raised * Added changes.rst * Updated warning with tests to improve coverage
1 parent 7dbc000 commit a6fddd6

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Unreleased
55
----------
66
- Add `custom_model_name` parameter to the constructor of `HistoricalRecords` (gh-451)
77
- Fix header on history pages when custom site_header is used (gh-448)
8+
- Raise warning if HistoricalRecords(inherit=False) is in an abstract model (gh-341)
89

910
2.5.1 (2018-10-19)
1011
------------------

simple_history/models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import importlib
55
import threading
66
import uuid
7+
import warnings
78

89
from django.apps import apps
910
from django.conf import settings
@@ -21,8 +22,8 @@
2122
from . import exceptions
2223
from .manager import HistoryDescriptor
2324
from .signals import (
24-
pre_create_historical_record,
2525
post_create_historical_record,
26+
pre_create_historical_record
2627
)
2728

2829
registered_models = {}
@@ -71,6 +72,11 @@ def contribute_to_class(self, cls, name):
7172
models.signals.class_prepared.connect(self.finalize, weak=False)
7273
self.add_extra_methods(cls)
7374

75+
if cls._meta.abstract and not self.inherit:
76+
msg = "HistoricalRecords added to abstract model ({}) without " \
77+
"inherit=True".format(self.cls.__name__)
78+
warnings.warn(msg, UserWarning)
79+
7480
def add_extra_methods(self, cls):
7581
def save_without_historical_record(self, *args, **kwargs):
7682
"""

simple_history/tests/tests/test_models.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,3 +1048,22 @@ def test_changed_value_lost(self):
10481048
historical = self.get_first_historical()
10491049
instance = historical.instance
10501050
self.assertEqual(instance.place, new_place)
1051+
1052+
1053+
class WarningOnAbstractModelWithInheritFalseTest(TestCase):
1054+
def test_warning_on_abstract_model_with_inherit_false(self):
1055+
1056+
with warnings.catch_warnings(record=True) as w:
1057+
class AbstractModelWithInheritFalse(models.Model):
1058+
string = models.CharField()
1059+
history = HistoricalRecords()
1060+
1061+
class Meta:
1062+
abstract = True
1063+
1064+
self.assertEqual(len(w), 1)
1065+
self.assertTrue(issubclass(w[0].category, UserWarning))
1066+
self.assertEqual(str(w[0].message),
1067+
'HistoricalRecords added to abstract model '
1068+
'(AbstractModelWithInheritFalse) without '
1069+
'inherit=True')

0 commit comments

Comments
 (0)