Skip to content

Commit

Permalink
Fixed history entries not being created for proxy models.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Osgood committed Dec 10, 2014
1 parent 86705a9 commit 9bb5400
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
11 changes: 6 additions & 5 deletions simple_history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
__version__ = '1.5.3-ebd.0'


def register(
model, app=None, manager_name='history', records_class=None,
**records_config):
def register(model, app=None, manager_name='history', records_class=None, **records_config):
"""
Create historical model for `model` and attach history manager to `model`.
Expand All @@ -19,7 +17,7 @@ def register(
`HistoricalManager` instance directly to `model`.
"""
from . import models
if model._meta.db_table not in models.registered_models:
if models.not_registered(model):
if records_class is None:
records_class = models.HistoricalRecords
records = records_class(**records_config)
Expand All @@ -29,4 +27,7 @@ def register(
records.add_extra_methods(model)
records.setup_m2m_history(model)
records.finalize(model)
models.registered_models[model._meta.db_table] = model
if model._meta.proxy:
models.registered_models['%s%s' % (model._meta.db_table, model.__name__)] = model
else:
models.registered_models[model._meta.db_table] = model
20 changes: 15 additions & 5 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
registered_models = {}


def not_registered(model):
if model._meta.proxy:
return '%s%s' % (model._meta.db_table, model.__name__) not in registered_models
return model._meta.db_table not in registered_models


class HistoricalRecords(object):
thread = threading.local()

Expand Down Expand Up @@ -89,7 +95,7 @@ def setup_m2m_history(self, cls):
('%s must be a ManyToManyField' % field.name)
if not sum([isinstance(item, HistoricalRecords) for item in field.rel.through.__dict__.values()]):
through_model = field.rel.through
if through_model._meta.auto_created and not through_model._meta.db_table in registered_models:
if through_model._meta.auto_created and not_registered(through_model):
through_model.history = HistoricalRecords()
register(through_model)
elif m2m_history_fields:
Expand All @@ -101,7 +107,7 @@ def setup_m2m_history(self, cls):
('%s must be a ManyToManyField' % field_name)
if not sum([isinstance(item, HistoricalRecords) for item in field.rel.through.__dict__.values()]):
through_model = field.rel.through
if through_model._meta.auto_created and not through_model._meta.db_table in registered_models:
if through_model._meta.auto_created and not_registered(through_model):
through_model.history = HistoricalRecords()
register(through_model)

Expand All @@ -115,9 +121,13 @@ def finalize(self, sender, **kwargs):
if not (hint_class._meta.abstract
and issubclass(sender, hint_class)): # set in abstract
return
history_model = self.create_history_model(sender)
module = importlib.import_module(self.module)
setattr(module, history_model.__name__, history_model)
if sender._meta.proxy:
# Proxy models use their parent's history model
history_model = getattr(sender, self.manager_name).model
else:
history_model = self.create_history_model(sender)
module = importlib.import_module(self.module)
setattr(module, history_model.__name__, history_model)
# The HistoricalRecords object will be discarded,
# so the signal handlers can't use weak references.
models.signals.post_save.connect(self.post_save, sender=sender,
Expand Down

0 comments on commit 9bb5400

Please sign in to comment.