Skip to content

Commit

Permalink
Merge 8658064 into 844e5f8
Browse files Browse the repository at this point in the history
  • Loading branch information
macro1 committed Oct 11, 2014
2 parents 844e5f8 + 8658064 commit f54fa6a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
8 changes: 7 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from setuptools import setup
import simple_history

tests_require = ["Django>=1.4", "webtest==2.0.6", "django-webtest==1.7"]
try:
from unittest import skipUnless
except ImportError: # Python 2.6 compatibility
tests_require.append("unittest2")

setup(
name='django-simple-history',
version=simple_history.__version__,
Expand All @@ -27,7 +33,7 @@
'Programming Language :: Python :: 3.3',
"License :: OSI Approved :: BSD License",
],
tests_require=["Django>=1.4", "webtest==2.0.6", "django-webtest==1.7"],
tests_require=tests_require,
include_package_data=True,
test_suite='runtests.main',
)
17 changes: 10 additions & 7 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def copy_fields(self, model):
# Don't allow reverse relations.
# ForeignKey knows best what datatype to use for the column
# we'll used that as soon as it's finalized by copying rel.to
field.__class__ = get_custom_fk_class(type(field))
field.__class__ = CustomForeignKeyField
field.rel.related_name = '+'
field.null = True
field.blank = True
Expand Down Expand Up @@ -243,7 +243,8 @@ def get_history_user(self, instance):
return None


class ForeignKeyMixin(object):
class CustomForeignKeyField(models.ForeignKey):

def get_attname(self):
return self.name

Expand Down Expand Up @@ -303,7 +304,13 @@ def get_field(self, other, cls):
def do_related_class(self, other, cls):
field = self.get_field(other, cls)
if not hasattr(self, 'related'):
self.related = RelatedObject(other, cls.instance_type, self)
try:
instance_type = cls.instance_type
except AttributeError: # when model is reconstituted for migration
if cls.__module__ != "__fake__": # not from migrations, error
raise
else:
self.related = RelatedObject(other, instance_type, self)
transform_field(field)
field.rel = None

Expand All @@ -312,10 +319,6 @@ def contribute_to_class(self, cls, name):
RelatedField.contribute_to_class(self, cls, name)


def get_custom_fk_class(parent_type):
return type(str('CustomForeignKey'), (ForeignKeyMixin, parent_type), {})


def transform_field(field):
"""Customize field appropriately for use in historical model"""
field.name = field.attname
Expand Down
5 changes: 5 additions & 0 deletions simple_history/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,8 @@ class UnicodeVerboseName(models.Model):

class Meta:
verbose_name = '\u570b'


class CustomFKError(models.Model):
fk = models.ForeignKey(SecondLevelInheritedModel)
history = HistoricalRecords()
12 changes: 12 additions & 0 deletions simple_history/tests/tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from contextlib import contextmanager
from six.moves import cStringIO as StringIO
from datetime import datetime
try:
from unittest import skipUnless
except ImportError:
from unittest2 import skipUnless
import django
from django.test import TestCase
from django.core import management
from simple_history import models as sh_models
Expand Down Expand Up @@ -101,3 +106,10 @@ def test_no_historical(self):
stdout=out)
self.assertIn(populate_history.Command.NO_REGISTERED_MODELS,
out.getvalue())


class TestMigrate(TestCase):

@skipUnless(django.get_version() >= "1.7", "Requires 1.7 migrations")
def test_migrate_command(self):
management.call_command('migrate', fake=True, stdout=StringIO())
12 changes: 11 additions & 1 deletion simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from __future__ import unicode_literals

from datetime import datetime, timedelta
try:
from unittest import skipUnless
except ImportError:
from unittest2 import skipUnless

import django
try:
from django.contrib.auth import get_user_model
User = get_user_model()
Expand All @@ -18,7 +23,7 @@
FileModel, Document, Book, HistoricalPoll, Library, State, AbstractBase,
ConcreteAttr, ConcreteUtil, SelfFK, Temperature, WaterLevel,
ExternalModel1, ExternalModel3, UnicodeVerboseName, HistoricalChoice,
HistoricalState
HistoricalState, HistoricalCustomFKError
)
from ..external.models import ExternalModel2, ExternalModel4

Expand Down Expand Up @@ -469,3 +474,8 @@ 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)

@skipUnless(django.get_version() >= "1.7", "Requires 1.7 migrations")
def test_state_serialization_of_customfk(self):
from django.db.migrations import state
state.ModelState.from_model(HistoricalCustomFKError)

0 comments on commit f54fa6a

Please sign in to comment.