def render_tag(self, context, obj, signal, notice_type, kwa, varname):
        observer = context['request'].user
        content_type = ContentType.objects.get_for_model(obj)
        observed = False
        if is_observing(observed=obj, observer=observer, signal=signal):
            observed = True

        result = ''
        perm = permission_by_label(obj, 'view')
        allowed = observer.is_authenticated() and (
            observer.has_perm(perm, obj) or observed  # Can stop oserving for closed observed object
        )

        if allowed:
            local_context = copy.copy(context)
            local_context.update(kwa)
            local_context.update({
                "object": obj,
                "object_id": obj.pk,
                "content_type_id": content_type.pk,
                'observer': observer,
                "signal": signal,
                "notice_type": notice_type,
                "observed": observed,
            })
            result = render_to_string('notification/observe_link.html',
                                      local_context)

        if varname:
            context[varname] = result
            return ''
        return result
Exemplo n.º 2
0
def observe(observed, observer, notice_type_label, signal="post_save"):
    """
    Create a new ObservedItem.

    To be used by applications to register a user as an observer for some object.
    """
    perm = permission_by_label(observed, 'view')
    if not (observer.is_authenticated() and observer.has_perm(perm, observed)):
        raise PermissionDenied()
    notice_type = NoticeType.objects.get(label=notice_type_label)
    observed_item = ObservedItem(
        user=observer, observed_object=observed,
        notice_type=notice_type, signal=signal
    )
    observed_item.save()
    return observed_item
Exemplo n.º 3
0
def send_now(users, label, extra_context=None, on_site=True, sender=None):
    """
    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.
    """
    sent = {}
    if extra_context is None:
        extra_context = {}

    notice_type = NoticeType.objects.get(label=label)
    notice_uid = extra_context.get('notice_uid', None)

    current_language = translation.get_language()
    current_timezone = timezone.get_current_timezone()

    for user in users:
        obj = extra_context.get('context_object',
                                extra_context.get('observed', None))
        if obj and not user.has_perm(permission_by_label(obj, 'view'), obj):
            continue

        if notice_uid:
            try:
                NoticeUid.objects.get(notice_uid=notice_uid, recipient=user)
                continue
            except NoticeUid.DoesNotExist:
                NoticeUid.objects.create(notice_uid=notice_uid, recipient=user)

        # Deprecated
        # get user language for user from language store defined in
        # NOTIFICATION_LANGUAGE_MODULE setting
        try:
            language = get_notification_language(user)
        except LanguageStoreNotAvailable:
            language = None

        # Deprecated
        result = {'pass': True}
        results = should_deliver.send(
            sender=Notice,
            result=result,
            recipient=user,
            label=label,
            notice_type=notice_type,
            extra_context=extra_context,
            sender_user=sender
        )
        if not result['pass']:
            continue
        if False in [i[1] for i in results]:
            continue

        results = configure.send(
            sender=Notice,
            recipient=user,
            label=label,
            notice_type=notice_type,
            extra_context=extra_context,
            sender_user=sender
        )
        configs = [i[1] for i in results if i[1]]
        configs.sort(key=lambda x: x.get('order', 0))
        # TODO: Let pass config as argument of function, or as item of extra_context???
        config = {
            'language': language or current_language,
            'timezone': current_timezone,
            'send': True,
        }
        for i in configs:
            config.update(i)

        if not config['send']:
            continue

        with translation.override(config['language']), timezone.override(config['timezone']):
            for (medium_id, backend_label), backend in list(NOTIFICATION_BACKENDS.items()):
                if backend.can_send(user, notice_type):
                    backend.deliver(user, sender, notice_type, extra_context)
                    delivered.send(
                        sender=Notice,
                        recipient=user,
                        notice_type=notice_type,
                        extra_context=extra_context,
                        sender_user=sender,
                        medium_id=medium_id,
                        backend_label=backend_label,
                        backend=backend
                    )
                    sent.setdefault(backend_label, 0)
                    sent[backend_label] += 1

    return sent