def on_save(self, instance): """ Update the search index when we know how to index the instance. """ identifier = instance_to_string(instance) if identifier and get_model_index(instance): instance_update.delay(identifier)
def on_delete(self, instance): """ Remove the instance from the index if the instance has a search_index """ identifier = instance_to_string(instance) if identifier and get_model_index(instance): instance_delete.delay(identifier)
def _delay_push(self, template, context, instance=None): """ Helper for push messages. Does the work in a celery task. """ target = None if instance: target = instance_to_string(instance) return send_push.delay(user=self.user.id, target=target, template=template, context=context)
def test_create_with_personal_feed(self): follow = FollowCompany.objects.filter(pk=1).first() follower_feed = PersonalFeed(follow.follower.id) event = Event.objects.filter(company=follow.target).first() self.assertIsNotNone(event) self.assertEqual(self.activity_count(follower_feed), 0) self.handler.handle_create(event) self.assertEqual(self.activity_count(follower_feed), 1) activity = self.all_activities(follower_feed)[0] self.assertEqual(activity.object, instance_to_string(event)) self.handler.handle_delete(event) self.assertEqual(self.activity_count(follower_feed), 0)
def call_handler(instance, action, **kwargs): """ The call_handler executes the delete action sync because the instance is removed from the DB before a worker is able to process the event. All other actions is executed by celery. """ if registry.handler_exists(instance): if action == "delete": handler = registry.get_handler_by_instance(instance) return handler.run(instance, action, **kwargs) return execute_action_handler.delay( instance=instance_to_string(instance), action=action, kwargs=kwargs )
def send(self): """ Send push messages to devices owned by the user. Apple supports a badge argument, this is set to the unread notifications count. """ message = render_to_string(self.template, self.context).strip() extra = dict() if self.instance: extra['target'] = instance_to_string(self.instance) gcm_devices = GCMDevice.objects.filter(user=self.user, active=True) apns_devices = APNSDevice.objects.filter(user=self.user, active=True) log.info('send_push', message=message, gcm_devices=gcm_devices.count(), apns_devices=apns_devices.count()) gcm_devices.send_message(message, extra=extra) apns_devices.send_message(message, badge=self._get_unread_count(), extra=extra)
def test_comment_on_followed_event(self): follow = FollowEvent.objects.filter(pk=2).first() comment = Comment.objects.filter( content_type=ContentType.objects.get_for_model(Event), object_id=follow.target.id).first() follower_feed = PersonalFeed(follow.follower.id) creator_feed = PersonalFeed(comment.created_by.id) self.assertIsNotNone(comment) self.assertEqual(self.activity_count(creator_feed), 0) self.assertEqual(self.activity_count(follower_feed), 0) self.handler.handle_create(comment) self.assertEqual(self.activity_count(creator_feed), 0) self.assertEqual(self.activity_count(follower_feed), 1) activity = self.all_activities(follower_feed)[0] self.assertEqual(activity.object, instance_to_string(comment)) self.handler.handle_delete(comment) self.assertEqual(self.activity_count(follower_feed), 0) self.assertEqual(self.activity_count(creator_feed), 0)
def test_instance_to_string(self): self.assertEqual(content_types.instance_to_string(self.instance), 'utils.mock-10')