Skip to content

Commit

Permalink
Make history registration inheritance an 'opt-in', test history fields
Browse files Browse the repository at this point in the history
  • Loading branch information
macro1 committed Dec 31, 2015
1 parent 611350a commit d6ce5c9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
6 changes: 3 additions & 3 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ class HistoricalRecords(object):
thread = threading.local()

def __init__(self, verbose_name=None, bases=(models.Model,),
user_related_name='+', table_name=None):
user_related_name='+', table_name=None, inherit=False):
self.user_set_verbose_name = verbose_name
self.user_related_name = user_related_name
self.table_name = table_name
self.inherit = inherit
try:
if isinstance(bases, six.string_types):
raise TypeError
Expand Down Expand Up @@ -77,8 +78,7 @@ def finalize(self, sender, **kwargs):
pass
else:
if hint_class is not sender: # set in concrete
if not (hint_class._meta.abstract
and issubclass(sender, hint_class)): # set in abstract
if not (self.inherit and issubclass(sender, hint_class)): # set in abstract
return
if hasattr(sender._meta, 'simple_history_manager_attribute'):
raise exceptions.MultipleRegistrationsError('{}.{} registered multiple times for history tracking.'.format(
Expand Down
6 changes: 3 additions & 3 deletions simple_history/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,14 @@ class ContactRegister(models.Model):
###############################################################################

class TrackedAbstractBaseA(models.Model):
history = HistoricalRecords()
history = HistoricalRecords(inherit=True)

class Meta:
abstract = True


class TrackedAbstractBaseB(models.Model):
history_b = HistoricalRecords()
history_b = HistoricalRecords(inherit=True)

class Meta:
abstract = True
Expand All @@ -306,7 +306,7 @@ class Meta:


class TrackedConcreteBase(models.Model):
history = HistoricalRecords()
history = HistoricalRecords(inherit=True)


class UntrackedConcreteBase(models.Model):
Expand Down
29 changes: 24 additions & 5 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,13 +789,19 @@ def test_tracked_abstract_base(self):
class TrackedWithAbstractBase(TrackedAbstractBaseA):
pass

TrackedWithAbstractBase.history.all()
self.assertEqual(
[f.attname for f in TrackedWithAbstractBase.history.model._meta.fields],
['id', 'history_id', 'history_date', 'history_user_id', 'history_type'],
)

def test_tracked_concrete_base(self):
class TrackedWithConcreteBase(TrackedConcreteBase):
pass

TrackedWithConcreteBase.history.all()
self.assertEqual(
[f.attname for f in TrackedWithConcreteBase.history.model._meta.fields],
['id', 'trackedconcretebase_ptr_id', 'history_id', 'history_date', 'history_user_id', 'history_type'],
)

def test_multiple_tracked_bases(self):
with self.assertRaises(exceptions.MultipleRegistrationsError):
Expand All @@ -806,7 +812,10 @@ def test_tracked_abstract_and_untracked_concrete_base(self):
class TrackedWithTrackedAbstractAndUntrackedConcreteBase(TrackedAbstractBaseA, UntrackedConcreteBase):
pass

TrackedWithTrackedAbstractAndUntrackedConcreteBase.history.all()
self.assertEqual(
[f.attname for f in TrackedWithTrackedAbstractAndUntrackedConcreteBase.history.model._meta.fields],
['id', 'untrackedconcretebase_ptr_id', 'history_id', 'history_date', 'history_user_id', 'history_type'],
)

def test_indirect_tracked_abstract_base(self):
class BaseTrackedWithIndirectTrackedAbstractBase(TrackedAbstractBaseA):
Expand All @@ -815,7 +824,12 @@ class BaseTrackedWithIndirectTrackedAbstractBase(TrackedAbstractBaseA):
class TrackedWithIndirectTrackedAbstractBase(BaseTrackedWithIndirectTrackedAbstractBase):
pass

TrackedWithIndirectTrackedAbstractBase.history.all()
self.assertEqual(
[f.attname for f in TrackedWithIndirectTrackedAbstractBase.history.model._meta.fields],
[
'id', 'basetrackedwithindirecttrackedabstractbase_ptr_id',
'history_id', 'history_date', 'history_user_id', 'history_type'],
)

def test_indirect_tracked_concrete_base(self):
class BaseTrackedWithIndirectTrackedConcreteBase(TrackedAbstractBaseA):
Expand All @@ -824,7 +838,12 @@ class BaseTrackedWithIndirectTrackedConcreteBase(TrackedAbstractBaseA):
class TrackedWithIndirectTrackedConcreteBase(BaseTrackedWithIndirectTrackedConcreteBase):
pass

TrackedWithIndirectTrackedConcreteBase.history.all()
self.assertEqual(
[f.attname for f in TrackedWithIndirectTrackedConcreteBase.history.model._meta.fields],
[
'id', 'basetrackedwithindirecttrackedconcretebase_ptr_id',
'history_id', 'history_date', 'history_user_id', 'history_type'],
)

def test_registering_with_tracked_abstract_base(self):
class TrackedWithAbstractBaseToRegister(TrackedAbstractBaseA):
Expand Down

0 comments on commit d6ce5c9

Please sign in to comment.