Skip to content

Commit

Permalink
Merge pull request #11 from ambitioninc/develop
Browse files Browse the repository at this point in the history
proxy models are handled
  • Loading branch information
wesleykendall committed May 24, 2014
2 parents f7e5255 + baa777a commit 4dc2cfe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
3 changes: 2 additions & 1 deletion dynamic_initial_data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
Expand Down
2 changes: 1 addition & 1 deletion dynamic_initial_data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
27 changes: 26 additions & 1 deletion dynamic_initial_data/tests/base_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions dynamic_initial_data/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion dynamic_initial_data/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.3.0'
__version__ = '0.3.1'

0 comments on commit 4dc2cfe

Please sign in to comment.