From 1117948788b86a9bc25ebeb5a490d326f9466a9f Mon Sep 17 00:00:00 2001 From: Wes Kendall Date: Sat, 24 May 2014 16:28:47 -0400 Subject: [PATCH] django dynamic initial data handles proxy models properly --- dynamic_initial_data/base.py | 3 ++- dynamic_initial_data/models.py | 2 +- dynamic_initial_data/tests/base_tests.py | 27 +++++++++++++++++++++++- dynamic_initial_data/tests/models.py | 8 +++++++ dynamic_initial_data/version.py | 2 +- 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/dynamic_initial_data/base.py b/dynamic_initial_data/base.py index 79fb34e..90beff0 100644 --- a/dynamic_initial_data/base.py +++ b/dynamic_initial_data/base.py @@ -151,7 +151,8 @@ def handle_deletions(self): now = datetime.utcnow() registered_for_deletion_receipts = [ RegisteredForDeletionReceipt( - model_obj_type=ContentType.objects.get_for_model(model_obj), model_obj_id=model_obj.id, + model_obj_type=ContentType.objects.get_for_model(model_obj, for_concrete_model=False), + model_obj_id=model_obj.id, register_time=now) for model_obj in set(self.model_objs_registered_for_deletion) ] diff --git a/dynamic_initial_data/models.py b/dynamic_initial_data/models.py index a3339e5..f60de1e 100644 --- a/dynamic_initial_data/models.py +++ b/dynamic_initial_data/models.py @@ -12,7 +12,7 @@ class RegisteredForDeletionReceipt(models.Model): # The model object that was registered model_obj_type = models.ForeignKey(ContentType) model_obj_id = models.PositiveIntegerField() - model_obj = generic.GenericForeignKey('model_obj_type', 'model_obj_id') + model_obj = generic.GenericForeignKey('model_obj_type', 'model_obj_id', for_concrete_model=False) # The time at which it was registered for deletion register_time = models.DateTimeField() diff --git a/dynamic_initial_data/tests/base_tests.py b/dynamic_initial_data/tests/base_tests.py index 77d8418..38d8912 100644 --- a/dynamic_initial_data/tests/base_tests.py +++ b/dynamic_initial_data/tests/base_tests.py @@ -11,7 +11,7 @@ from dynamic_initial_data.exceptions import InitialDataMissingApp, InitialDataCircularDependency from dynamic_initial_data.models import RegisteredForDeletionReceipt from dynamic_initial_data.tests.mocks import MockInitialData, MockClass, MockOne, MockTwo, MockThree -from dynamic_initial_data.tests.models import Account +from dynamic_initial_data.tests.models import Account, ProxyAccount class BaseInitialDataTest(TestCase): @@ -87,6 +87,31 @@ def test_create_dup_objs(self): self.assertEquals(receipt.model_obj_id, account.id) self.assertEquals(receipt.register_time, datetime(2013, 4, 12)) + def test_create_dup_proxy_objs(self): + """ + Tests creating duplicate objects for deletion when one is a proxy of another. + """ + account = G(Account) + proxy_account = ProxyAccount.objects.get(id=account.id) + self.initial_data_updater.model_objs_registered_for_deletion = [account, account, proxy_account] + + self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0) + with freeze_time('2013-04-12'): + self.initial_data_updater.handle_deletions() + self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 2) + + receipt = RegisteredForDeletionReceipt.objects.get(model_obj_type=ContentType.objects.get_for_model(account)) + self.assertEquals(receipt.model_obj_type, ContentType.objects.get_for_model(Account)) + self.assertEquals(receipt.model_obj_id, account.id) + self.assertEquals(receipt.register_time, datetime(2013, 4, 12)) + + receipt = RegisteredForDeletionReceipt.objects.get( + model_obj_type=ContentType.objects.get_for_model(proxy_account, for_concrete_model=False)) + self.assertEquals( + receipt.model_obj_type, ContentType.objects.get_for_model(ProxyAccount, for_concrete_model=False)) + self.assertEquals(receipt.model_obj_id, proxy_account.id) + self.assertEquals(receipt.register_time, datetime(2013, 4, 12)) + def test_create_delete_one_obj(self): """ Tests creating one object to handle for deletion and then deleting it. diff --git a/dynamic_initial_data/tests/models.py b/dynamic_initial_data/tests/models.py index 19ef6c4..7c38bde 100644 --- a/dynamic_initial_data/tests/models.py +++ b/dynamic_initial_data/tests/models.py @@ -6,3 +6,11 @@ class Account(models.Model): A test account model. """ name = models.CharField(max_length=64) + + +class ProxyAccount(Account): + """ + A model for testing proxy models. + """ + class Meta: + proxy = True diff --git a/dynamic_initial_data/version.py b/dynamic_initial_data/version.py index 0404d81..e1424ed 100644 --- a/dynamic_initial_data/version.py +++ b/dynamic_initial_data/version.py @@ -1 +1 @@ -__version__ = '0.3.0' +__version__ = '0.3.1'