Skip to content

Commit 15a5699

Browse files
committed
Adding table_name parameter to both HistoricalRecords() and
`register()` functions instead of using `History` inner class.
1 parent f3de643 commit 15a5699

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

simple_history/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
def register(
77
model, app=None, manager_name='history', records_class=None,
8-
**records_config):
8+
table_name=None, **records_config):
99
"""
1010
Create historical model for `model` and attach history manager to `model`.
1111
@@ -14,6 +14,8 @@ def register(
1414
manager_name -- class attribute name to use for historical manager
1515
records_class -- class to use for history relation (defaults to
1616
HistoricalRecords)
17+
table_name -- Custom name for history table (defaults to
18+
'APPNAME_historicalMODELNAME')
1719
1820
This method should be used as an alternative to attaching an
1921
`HistoricalManager` instance directly to `model`.
@@ -24,6 +26,7 @@ def register(
2426
records_class = models.HistoricalRecords
2527
records = records_class(**records_config)
2628
records.manager_name = manager_name
29+
records.table_name = table_name
2730
records.module = app and ("%s.models" % app) or model.__module__
2831
records.add_extra_methods(model)
2932
records.finalize(model)

simple_history/models.py

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ class HistoricalRecords(object):
4444
thread = threading.local()
4545

4646
def __init__(self, verbose_name=None, bases=(models.Model,),
47-
user_related_name='+'):
47+
user_related_name='+', table_name=None):
4848
self.user_set_verbose_name = verbose_name
4949
self.user_related_name = user_related_name
50+
self.table_name = table_name
5051
try:
5152
if isinstance(bases, six.string_types):
5253
raise TypeError
@@ -117,9 +118,8 @@ def create_history_model(self, model):
117118
attrs.update(self.get_extra_fields(model, fields))
118119
# type in python2 wants str as a first argument
119120
attrs.update(Meta=type(str('Meta'), (), self.get_meta_options(model)))
120-
history_options = self.get_history_options(model)
121-
if 'db_history_table' in history_options:
122-
attrs['Meta'].db_table = history_options['db_history_table']
121+
if self.table_name is not None:
122+
attrs['Meta'].db_table = self.table_name
123123
name = 'Historical%s' % model._meta.object_name
124124
registered_models[model._meta.db_table] = model
125125
return python_2_unicode_compatible(
@@ -228,27 +228,6 @@ def get_meta_options(self, model):
228228
meta_fields['verbose_name'] = name
229229
return meta_fields
230230

231-
def get_history_options(self, model):
232-
"""
233-
Returns a dictionary of options set to the History inner
234-
class of the historical record model.
235-
"""
236-
VALID_OPTIONS = ['db_history_table']
237-
if hasattr(model, 'History'):
238-
history_options = model.History.__dict__.copy()
239-
for key in model.History.__dict__:
240-
# Ignore any private attributes that we doesn't care about, like
241-
# "__module__" or "__dict__".
242-
# NOTE: We can't modify a dictionary's contents while looping
243-
# over it, so we loop over the *original* dictionary instead.
244-
if key.startswith('_'):
245-
del history_options[key]
246-
for option in history_options:
247-
if not option in VALID_OPTIONS:
248-
raise TypeError("'class History' got invalid attribute: %s" % (option,))
249-
return history_options
250-
return {}
251-
252231
def post_save(self, instance, created, **kwargs):
253232
if not created and hasattr(instance, 'skip_history_when_saving'):
254233
return

simple_history/tests/models.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@ class City(models.Model):
268268
class Contact(models.Model):
269269
name = models.CharField(max_length=30)
270270
email = models.EmailField(max_length=255, unique=True)
271-
history = HistoricalRecords()
271+
history = HistoricalRecords(table_name='contacts_history')
272+
273+
class ContactRegister(models.Model):
274+
name = models.CharField(max_length=30)
275+
email = models.EmailField(max_length=255, unique=True)
272276

273-
class History:
274-
db_history_table = 'contacts_history'
277+
register(ContactRegister, table_name='contacts_register_history')

simple_history/tests/tests/test_models.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
ExternalModel1, ExternalModel3, UnicodeVerboseName, HistoricalChoice,
2020
HistoricalState, HistoricalCustomFKError, Series, SeriesWork, PollInfo,
2121
UserAccessorDefault, UserAccessorOverride, Employee, Country, Province,
22-
City, Contact
22+
City, Contact, ContactRegister
2323
)
2424
from ..external.models import ExternalModel2, ExternalModel4
2525

@@ -746,9 +746,16 @@ def test_restore_employee(self):
746746
with self.assertRaises(Employee.DoesNotExist):
747747
original.manager
748748

749-
class CustomTableNameTest(TestCase):
749+
class CustomTableNameTest1(TestCase):
750750
def get_table_name(self, manager):
751751
return manager.model._meta.db_table
752752

753753
def test_custom_table_name(self):
754754
self.assertEqual(self.get_table_name(Contact.history), 'contacts_history')
755+
756+
class CustomTableNameTest2(TestCase):
757+
def get_table_name(self, manager):
758+
return manager.model._meta.db_table
759+
760+
def test_custom_table_name(self):
761+
self.assertEqual(self.get_table_name(ContactRegister.history), 'contacts_register_history')

0 commit comments

Comments
 (0)