Skip to content

Commit d6ce5c9

Browse files
committed
Make history registration inheritance an 'opt-in', test history fields
1 parent 611350a commit d6ce5c9

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

simple_history/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ class HistoricalRecords(object):
3636
thread = threading.local()
3737

3838
def __init__(self, verbose_name=None, bases=(models.Model,),
39-
user_related_name='+', table_name=None):
39+
user_related_name='+', table_name=None, inherit=False):
4040
self.user_set_verbose_name = verbose_name
4141
self.user_related_name = user_related_name
4242
self.table_name = table_name
43+
self.inherit = inherit
4344
try:
4445
if isinstance(bases, six.string_types):
4546
raise TypeError
@@ -77,8 +78,7 @@ def finalize(self, sender, **kwargs):
7778
pass
7879
else:
7980
if hint_class is not sender: # set in concrete
80-
if not (hint_class._meta.abstract
81-
and issubclass(sender, hint_class)): # set in abstract
81+
if not (self.inherit and issubclass(sender, hint_class)): # set in abstract
8282
return
8383
if hasattr(sender._meta, 'simple_history_manager_attribute'):
8484
raise exceptions.MultipleRegistrationsError('{}.{} registered multiple times for history tracking.'.format(

simple_history/tests/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,14 @@ class ContactRegister(models.Model):
286286
###############################################################################
287287

288288
class TrackedAbstractBaseA(models.Model):
289-
history = HistoricalRecords()
289+
history = HistoricalRecords(inherit=True)
290290

291291
class Meta:
292292
abstract = True
293293

294294

295295
class TrackedAbstractBaseB(models.Model):
296-
history_b = HistoricalRecords()
296+
history_b = HistoricalRecords(inherit=True)
297297

298298
class Meta:
299299
abstract = True
@@ -306,7 +306,7 @@ class Meta:
306306

307307

308308
class TrackedConcreteBase(models.Model):
309-
history = HistoricalRecords()
309+
history = HistoricalRecords(inherit=True)
310310

311311

312312
class UntrackedConcreteBase(models.Model):

simple_history/tests/tests/test_models.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -789,13 +789,19 @@ def test_tracked_abstract_base(self):
789789
class TrackedWithAbstractBase(TrackedAbstractBaseA):
790790
pass
791791

792-
TrackedWithAbstractBase.history.all()
792+
self.assertEqual(
793+
[f.attname for f in TrackedWithAbstractBase.history.model._meta.fields],
794+
['id', 'history_id', 'history_date', 'history_user_id', 'history_type'],
795+
)
793796

794797
def test_tracked_concrete_base(self):
795798
class TrackedWithConcreteBase(TrackedConcreteBase):
796799
pass
797800

798-
TrackedWithConcreteBase.history.all()
801+
self.assertEqual(
802+
[f.attname for f in TrackedWithConcreteBase.history.model._meta.fields],
803+
['id', 'trackedconcretebase_ptr_id', 'history_id', 'history_date', 'history_user_id', 'history_type'],
804+
)
799805

800806
def test_multiple_tracked_bases(self):
801807
with self.assertRaises(exceptions.MultipleRegistrationsError):
@@ -806,7 +812,10 @@ def test_tracked_abstract_and_untracked_concrete_base(self):
806812
class TrackedWithTrackedAbstractAndUntrackedConcreteBase(TrackedAbstractBaseA, UntrackedConcreteBase):
807813
pass
808814

809-
TrackedWithTrackedAbstractAndUntrackedConcreteBase.history.all()
815+
self.assertEqual(
816+
[f.attname for f in TrackedWithTrackedAbstractAndUntrackedConcreteBase.history.model._meta.fields],
817+
['id', 'untrackedconcretebase_ptr_id', 'history_id', 'history_date', 'history_user_id', 'history_type'],
818+
)
810819

811820
def test_indirect_tracked_abstract_base(self):
812821
class BaseTrackedWithIndirectTrackedAbstractBase(TrackedAbstractBaseA):
@@ -815,7 +824,12 @@ class BaseTrackedWithIndirectTrackedAbstractBase(TrackedAbstractBaseA):
815824
class TrackedWithIndirectTrackedAbstractBase(BaseTrackedWithIndirectTrackedAbstractBase):
816825
pass
817826

818-
TrackedWithIndirectTrackedAbstractBase.history.all()
827+
self.assertEqual(
828+
[f.attname for f in TrackedWithIndirectTrackedAbstractBase.history.model._meta.fields],
829+
[
830+
'id', 'basetrackedwithindirecttrackedabstractbase_ptr_id',
831+
'history_id', 'history_date', 'history_user_id', 'history_type'],
832+
)
819833

820834
def test_indirect_tracked_concrete_base(self):
821835
class BaseTrackedWithIndirectTrackedConcreteBase(TrackedAbstractBaseA):
@@ -824,7 +838,12 @@ class BaseTrackedWithIndirectTrackedConcreteBase(TrackedAbstractBaseA):
824838
class TrackedWithIndirectTrackedConcreteBase(BaseTrackedWithIndirectTrackedConcreteBase):
825839
pass
826840

827-
TrackedWithIndirectTrackedConcreteBase.history.all()
841+
self.assertEqual(
842+
[f.attname for f in TrackedWithIndirectTrackedConcreteBase.history.model._meta.fields],
843+
[
844+
'id', 'basetrackedwithindirecttrackedconcretebase_ptr_id',
845+
'history_id', 'history_date', 'history_user_id', 'history_type'],
846+
)
828847

829848
def test_registering_with_tracked_abstract_base(self):
830849
class TrackedWithAbstractBaseToRegister(TrackedAbstractBaseA):

0 commit comments

Comments
 (0)