Skip to content

Commit

Permalink
Allow historical tracking to be set on abstract bases
Browse files Browse the repository at this point in the history
  • Loading branch information
macro1 committed Aug 22, 2014
1 parent 9bc5340 commit 40b48ed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
8 changes: 7 additions & 1 deletion simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def __init__(self, verbose_name=None, bases=(models.Model,)):
def contribute_to_class(self, cls, name):
self.manager_name = name
self.module = cls.__module__
models.signals.class_prepared.connect(self.finalize, sender=cls)
self.cls = cls
models.signals.class_prepared.connect(self.finalize, weak=False)
self.add_extra_methods(cls)

def add_extra_methods(self, cls):
Expand All @@ -89,6 +90,11 @@ def save_without_historical_record(self, *args, **kwargs):
save_without_historical_record)

def finalize(self, sender, **kwargs):
try:
if not issubclass(sender, self.cls):
return
except AttributeError: # called via `register`
pass
history_model = self.create_history_model(sender)
module = importlib.import_module(self.module)
setattr(module, history_model.__name__, history_model)
Expand Down
11 changes: 11 additions & 0 deletions simple_history/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,14 @@ class UnicodeVerboseName(models.Model):

class Meta:
verbose_name = '\u570b'


class TrackedAbstractBase(models.Model):
history = HistoricalRecords()

class Meta:
abstract = True


class Tracked(TrackedAbstractBase):
pass
5 changes: 4 additions & 1 deletion simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
FileModel, Document, Book, HistoricalPoll, Library, State, AbstractBase,
ConcreteAttr, ConcreteUtil, SelfFK, Temperature, WaterLevel,
ExternalModel1, ExternalModel3, UnicodeVerboseName, HistoricalChoice,
HistoricalState
HistoricalState, Tracked
)
from ..external.models import ExternalModel2, ExternalModel4

Expand Down Expand Up @@ -469,3 +469,6 @@ def test_import_related(self):
def test_string_related(self):
field_object = HistoricalState._meta.get_field_by_name('library_id')[0]
self.assertEqual(field_object.related.model, State)

def test_historical_abstract(self):
Tracked.history.all()

0 comments on commit 40b48ed

Please sign in to comment.