Skip to content
This repository has been archived by the owner on Jun 29, 2020. It is now read-only.

Commit

Permalink
Fix content_object issue
Browse files Browse the repository at this point in the history
-   Remove `content_object` attribute which can be `None` for deleted
    object
-   Use `snapshot` instead of `content_object` which would not be `None`
-   Stop defering `snapshot` to prevent N+1 problem
  • Loading branch information
lambdalisue committed Oct 14, 2014
1 parent a8bc0a2 commit 6959147
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 21 deletions.
19 changes: 7 additions & 12 deletions src/kawaz/apps/activities/mediator.py
Expand Up @@ -16,8 +16,6 @@ class ActivityMediator(object):
An ActivityMediator class which has responsivilities to controll automatic
activity creation (with signal handling) or rendering.
"""
use_snapshot = False

def _pre_delete_receiver(self, sender, instance, **kwargs):
ct = ContentType.objects.get_for_model(instance)
activity = Activity(content_type=ct,
Expand All @@ -26,9 +24,8 @@ def _pre_delete_receiver(self, sender, instance, **kwargs):
# call user defined alternation code
activity = self.alter(instance, activity, **kwargs)
if activity:
# save current instance snapshot if 'use_snapshot'
if self.use_snapshot:
activity.snapshot = instance
# save current instance as a snapshot
activity.snapshot = instance
activity.save()

def _post_save_receiver(self, sender, instance, created, **kwargs):
Expand All @@ -39,19 +36,17 @@ def _post_save_receiver(self, sender, instance, created, **kwargs):
# call user defined alternation code
activity = self.alter(instance, activity, **kwargs)
if activity:
# save current instance snapshot if 'use_snapshot'
if self.use_snapshot:
activity.snapshot = instance
# save current instance as a snapshot
activity.snapshot = instance
activity.save()

def _m2m_changed_receiver(self, sender, instance, **kwargs):
# call user defined alternation code
# user need to create activity instance
activity = self.alter(instance, None, **kwargs)
if activity:
# save current instance snapshot if 'use_snapshot'
if self.use_snapshot:
activity.snapshot = instance
# save current instance as a snapshot
activity.snapshot = instance
activity.save()

def connect(self, model):
Expand Down Expand Up @@ -109,7 +104,7 @@ def prepare_context(self, activity, context):
"""
context.update({
'activity': activity,
'object': activity.content_object
'object': activity.snapshot
})
return context

Expand Down
5 changes: 0 additions & 5 deletions src/kawaz/apps/activities/models.py
Expand Up @@ -14,10 +14,6 @@


class ActivityManager(models.Manager):
def get_queryset(self):
# defer '_snapshot' column which only required during signal handling
# to accelerate database access
return super().get_queryset().defer('_snapshot')

def latests(self):
"""
Expand All @@ -43,7 +39,6 @@ class Activity(models.Model):

content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField('Object ID')
content_object = GenericForeignKey()
_snapshot = models.BinaryField(default=None, null=True)

created_at = models.DateTimeField(auto_now_add=True)
Expand Down
3 changes: 2 additions & 1 deletion src/kawaz/apps/activities/registry.py
Expand Up @@ -2,6 +2,7 @@
"""
"""
__author__ = 'Alisue <lambdalisue@hashnote.net>'
from django.contrib.contenttypes.models import ContentType
from .mediator import ActivityMediator


Expand Down Expand Up @@ -38,7 +39,7 @@ def get(self, activity):
"""
Get connected activity mediator of a model which the activity has.
"""
model = activity.content_object._meta.model
model = activity.content_type.model_class()
return self._registry[model]


Expand Down
2 changes: 1 addition & 1 deletion src/kawaz/apps/activities/tests/test_mediator.py
Expand Up @@ -137,7 +137,7 @@ def test_prepare_context(self):

context.update.assert_called_with({
'activity': activity,
'object': activity.content_object,
'object': activity.snapshot,
})
self.assertEqual(c, context)

Expand Down
2 changes: 1 addition & 1 deletion src/kawaz/apps/activities/tests/test_models.py
Expand Up @@ -66,10 +66,10 @@ def test_attributes(self):
activity = Activity()
self.assertTrue(hasattr(activity, 'status'))
self.assertTrue(hasattr(activity, 'remarks'))
self.assertTrue(hasattr(activity, 'content_object'))
self.assertTrue(hasattr(activity, 'object_id'))
self.assertTrue(hasattr(activity, 'created_at'))
self.assertTrue(hasattr(activity, '_snapshot'))
self.assertTrue(hasattr(activity, 'snapshot'))

def test_snapshot(self):
model = self.models[0]
Expand Down
2 changes: 1 addition & 1 deletion src/kawaz/apps/activities/tests/test_registry.py
Expand Up @@ -37,6 +37,6 @@ def test_get(self):
registry.register(model, mediator)

activity = MagicMock()
activity.content_object._meta.model = model
activity.content_type.model_class = MagicMock(return_value=model)

self.assertEqual(registry.get(activity), mediator)

0 comments on commit 6959147

Please sign in to comment.