def notify_handler(verb, **kwargs): """ Handler function to create Notification instance upon action signal call. """ kwargs.pop('signal', None) recipient = kwargs.pop('recipient') actor = kwargs.pop('sender') newnotify = Notification( recipient = recipient, actor_content_type=ContentType.objects.get_for_model(actor), actor_object_id=actor.pk, verb=unicode(verb), public=bool(kwargs.pop('public', True)), description=kwargs.pop('description', None), timestamp=kwargs.pop('timestamp', now()) ) for opt in ('target', 'action_object'): obj = kwargs.pop(opt, None) if not obj is None: setattr(newnotify, '%s_object_id' % opt, obj.pk) setattr(newnotify, '%s_content_type' % opt, ContentType.objects.get_for_model(obj)) if len(kwargs) and EXTRA_DATA: newnotify.data = kwargs newnotify.save()
def notify_user(recipient, actor, verb, **kwargs): """ Handler function to create Notification instance upon action signal call. """ new_notification = Notification( recipient = recipient, actor_content_type=ContentType.objects.get_for_model(actor), actor_object_id=actor.pk, verb=text_type(verb), public=bool(kwargs.pop('public', False)), description=kwargs.pop('description', None), timestamp=kwargs.pop('timestamp', utc_now()), level=kwargs.pop('level', Notification.LEVELS.info), ) for opt in ('target', 'action_object'): obj = kwargs.pop(opt, None) if not obj is None: setattr(new_notification, '%s_object_id' % opt, obj.pk) setattr(new_notification, '%s_content_type' % opt, ContentType.objects.get_for_model(obj)) if len(kwargs) and EXTRA_DATA: new_notification.data = kwargs new_notification.save() return new_notification
def notify_handler(verb, **kwargs): """ Handler function to create Notification instance upon action signal call. """ # Pull the options out of kwargs kwargs.pop('signal', None) recipient = kwargs.pop('recipient') actor = kwargs.pop('sender') optional_objs = [(kwargs.pop(opt, None), opt) for opt in ('target', 'action_object')] public = bool(kwargs.pop('public', True)) description = kwargs.pop('description', None) timestamp = kwargs.pop('timestamp', datetime.now()) level = kwargs.pop('level', Notification.LEVELS.info) # Check if User or Group if isinstance(recipient, Group): recipients = recipient.user_set.all() elif isinstance(recipient, QuerySet) or isinstance(recipient, list): recipients = recipient else: recipients = [recipient] new_notifications = [] for recipient in recipients: newnotify = Notification( recipient=recipient, actor_content_type=ContentType.objects.get_for_model(actor), actor_object_id=actor.pk, verb=text_type(verb), public=public, description=description, timestamp=timestamp, level=level, ) # Set optional objects for obj, opt in optional_objs: if obj is not None: setattr(newnotify, '%s_object_id' % opt, obj.pk) setattr(newnotify, '%s_content_type' % opt, ContentType.objects.get_for_model(obj)) if len(kwargs) and EXTRA_DATA: newnotify.data = kwargs newnotify.save() new_notifications.append(newnotify) return new_notifications