def __init__(self, *args, **kwargs): medium = kwargs.get('medium', None) notice_type = kwargs.get('notice_type', None) if medium and notice_type and 'send' not in kwargs: kwargs['send'] = (get_backend(medium).sensitivity <= notice_type.default) super(NoticeSetting, self).__init__(*args, **kwargs)
def create_notification_setting(user, notice_type, medium): default = (get_backend(medium).sensitivity <= notice_type.default) setting = NoticeSetting(user=user, notice_type=notice_type, medium=medium, send=default) setting.save() return setting
def smart_send(users, label, extra_context=None, sender=None, related_object=None, now=False): """ Creates a new notice. This is intended to be how other apps create new notices. notification.send(user, 'friends_invite_sent', { 'spam': 'eggs', 'foo': 'bar', }) You can pass in on_site=False to prevent the notice emitted from being displayed on the site. """ try: notice_type = NoticeType.objects.get(label=label) except NoticeType.DoesNotExist: if settings.DEBUG and not settings.NOTIFICATION_FAIL_SILENTLY: raise return try: iter_users = iter(users) except TypeError: iter_users = iter([users]) for user in iter_users: notice_settings = NoticeSetting.objects.filter_or_create( user, notice_type).filter(send=True) if notice_settings: active_settings = [] on_site = False for setting in notice_settings: if not get_backend(setting.medium).passive: active_settings.append(setting) elif setting.medium == 'on_site' and setting.send: on_site = True notice = Notice.objects.create(recipient=user, notice_type=notice_type, on_site=on_site, sender=sender, related_object=related_object) if active_settings and user.is_active: if now or not DELAY_ALL: send_notice(notice, active_settings, extra_context) else: send_notice.apply_async(args=(notice, active_settings, extra_context), countdown=10) elif ENFORCE_CREATE: Notice.objects.create(recipient=user, notice_type=notice_type, on_site=False, sender=sender, related_object=related_object)
def send_notice(notice, notice_settings, context): context = context or {} context.update({'notice': notice, 'recipient': notice.recipient, 'sender': notice.sender, 'related_object': notice.related_object, 'notice_type': notice.notice_type}) for setting in notice_settings: backend = get_backend(setting.medium) # get prerendered format messages context.update({'notice_setting': setting}) message = get_formatted_message( backend.formats, notice.notice_type, context, backend.slug) backend.send(message, notice.recipient)
def smart_send(users, label, extra_context=None, sender=None, related_object=None, now=False): """ Creates a new notice. This is intended to be how other apps create new notices. notification.send(user, 'friends_invite_sent', { 'spam': 'eggs', 'foo': 'bar', }) You can pass in on_site=False to prevent the notice emitted from being displayed on the site. """ try: notice_type = NoticeType.objects.get(label=label) except NoticeType.DoesNotExist: if settings.DEBUG and not settings.NOTIFICATION_FAIL_SILENTLY: raise return try: iter_users = iter(users) except TypeError: iter_users = iter([users]) for user in iter_users: notice_settings = NoticeSetting.objects.filter_or_create( user, notice_type).filter(send=True) if notice_settings: active_settings = [] on_site = False for setting in notice_settings: if not get_backend(setting.medium).passive: active_settings.append(setting) elif setting.medium == 'on_site' and setting.send: on_site = True notice = Notice.objects.create( recipient=user, notice_type=notice_type, on_site=on_site, sender=sender, related_object=related_object) if active_settings and user.is_active: if now or not DELAY_ALL: send_notice(notice, active_settings, extra_context) else: send_notice.apply_async( args=(notice, active_settings, extra_context), countdown=10) elif ENFORCE_CREATE: Notice.objects.create( recipient=user, notice_type=notice_type, on_site=False, sender=sender, related_object=related_object)
import celery from notification.backends import get_backend @celery.task(default_retry_delay=30 * 60) def send_notice(notice, notice_settings, context): context = context or {} related_object = None if notice.content_type: try: related_object = notice.content_type.get_object_for_this_type( id=notice.object_id) except notice.content_type.DoesNotExist, exc: raise send_notice.retry(exc=exc, countdown=60) context.update({'notice': notice, 'recipient': notice.recipient, 'sender': notice.sender, 'related_object': related_object, 'notice_type': notice.notice_type}) for setting in notice_settings: backend = get_backend(setting.medium) context.update({'notice_setting': setting}) backend.send(notice, context)
def can_modify(self): return bool(get_backend(self.medium).id & self.notice_type.allowed)
def create_notification_setting(user, notice_type, medium): default = (get_backend(medium).sensitivity <= notice_type.default) setting = NoticeSetting( user=user, notice_type=notice_type, medium=medium, send=default) setting.save() return setting
import celery from notification.backends import get_backend @celery.task(default_retry_delay=30 * 60) def send_notice(notice, notice_settings, context): context = context or {} related_object = None if notice.content_type: try: related_object = notice.content_type.get_object_for_this_type( id=notice.object_id) except notice.content_type.DoesNotExist, exc: raise send_notice.retry(exc=exc, countdown=60) context.update({ 'notice': notice, 'recipient': notice.recipient, 'sender': notice.sender, 'related_object': related_object, 'notice_type': notice.notice_type }) for setting in notice_settings: backend = get_backend(setting.medium) context.update({'notice_setting': setting}) backend.send(notice, context)