Skip to content

Commit

Permalink
GH-341: Warning if HistoricalRecords(inherit=False) in an abstract cl…
Browse files Browse the repository at this point in the history
…ass (#472)

* First commit for issue #341 - Warning if inerhit is set to False in an abstract class

* First commit for issue #341 - Warning if inerhit is set to False in an abstract class

* Fixed flake8 errors and tested that warning is raised

* Added changes.rst

* Updated warning with tests to improve coverage
  • Loading branch information
Ross Mechanic authored Oct 31, 2018
1 parent 7dbc000 commit a6fddd6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Unreleased
----------
- Add `custom_model_name` parameter to the constructor of `HistoricalRecords` (gh-451)
- Fix header on history pages when custom site_header is used (gh-448)
- Raise warning if HistoricalRecords(inherit=False) is in an abstract model (gh-341)

2.5.1 (2018-10-19)
------------------
Expand Down
8 changes: 7 additions & 1 deletion simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import importlib
import threading
import uuid
import warnings

from django.apps import apps
from django.conf import settings
Expand All @@ -21,8 +22,8 @@
from . import exceptions
from .manager import HistoryDescriptor
from .signals import (
pre_create_historical_record,
post_create_historical_record,
pre_create_historical_record
)

registered_models = {}
Expand Down Expand Up @@ -71,6 +72,11 @@ def contribute_to_class(self, cls, name):
models.signals.class_prepared.connect(self.finalize, weak=False)
self.add_extra_methods(cls)

if cls._meta.abstract and not self.inherit:
msg = "HistoricalRecords added to abstract model ({}) without " \
"inherit=True".format(self.cls.__name__)
warnings.warn(msg, UserWarning)

def add_extra_methods(self, cls):
def save_without_historical_record(self, *args, **kwargs):
"""
Expand Down
19 changes: 19 additions & 0 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,3 +1048,22 @@ def test_changed_value_lost(self):
historical = self.get_first_historical()
instance = historical.instance
self.assertEqual(instance.place, new_place)


class WarningOnAbstractModelWithInheritFalseTest(TestCase):
def test_warning_on_abstract_model_with_inherit_false(self):

with warnings.catch_warnings(record=True) as w:
class AbstractModelWithInheritFalse(models.Model):
string = models.CharField()
history = HistoricalRecords()

class Meta:
abstract = True

self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[0].category, UserWarning))
self.assertEqual(str(w[0].message),
'HistoricalRecords added to abstract model '
'(AbstractModelWithInheritFalse) without '
'inherit=True')

0 comments on commit a6fddd6

Please sign in to comment.