Example #1
0
def generate_notifications(request, annotation, action):
    # Only send notifications when new annotations are created
    if action != 'create':
        return

    # If the annotation doesn't have a parent, we can't find its parent, or we
    # have no idea who the author of the parent is, then we can't send a
    # notification email.
    parent_id = annotation.parent_id
    if parent_id is None:
        return
    parent = storage.fetch_annotation(request, parent_id)
    if parent is None or 'user' not in parent:
        return

    # We don't send replies to the author of the parent unless they're going to
    # be able to read it. That means there must be some overlap between the set
    # of effective principals of the parent's author, and the read permissions
    # of the reply.
    child_read_permissions = annotation.get('permissions', {}).get('read', [])
    parent_principals = auth.effective_principals(parent['user'], request)
    read_principals = translate_annotation_principals(child_read_permissions)
    if not set(parent_principals).intersection(read_principals):
        return

    # Store the parent values as additional data
    data = {
        'parent': parent
    }

    subscriptions = Subscriptions.get_active_subscriptions_for_a_type(
        types.REPLY_TYPE)
    for subscription in subscriptions:
        data['subscription'] = subscription.__json__(request)

        # Validate annotation
        if check_conditions(annotation, data):
            try:
                subject, text, html, recipients = render_reply_notification(
                    request,
                    annotation,
                    parent)
                yield subject, text, html, recipients
            # ToDo: proper exception handling here
            except TemplateRenderException:
                log.exception('Failed to render subscription'
                              ' template %s', subscription)
            except:
                log.exception('Unknown error when trying to render'
                              ' subscription template %s', subscription)
Example #2
0
def generate_notifications(request, annotation, action):
    # Only send notifications when new annotations are created
    if action != 'create':
        return

    # If the annotation doesn't have a parent, we can't find its parent, or we
    # have no idea who the author of the parent is, then we can't send a
    # notification email.
    parent_id = annotation.parent_id
    if parent_id is None:
        return
    parent = storage.fetch_annotation(request, parent_id)
    if parent is None or 'user' not in parent:
        return

    # We don't send replies to the author of the parent unless they're going to
    # be able to read it. That means there must be some overlap between the set
    # of effective principals of the parent's author, and the read permissions
    # of the reply.
    child_read_permissions = annotation.get('permissions', {}).get('read', [])
    parent_principals = auth.effective_principals(parent['user'], request)
    read_principals = translate_annotation_principals(child_read_permissions)
    if not set(parent_principals).intersection(read_principals):
        return

    # Store the parent values as additional data
    data = {
        'parent': parent
    }

    subscriptions = Subscriptions.get_active_subscriptions_for_a_type(
        types.REPLY_TYPE)
    for subscription in subscriptions:
        data['subscription'] = subscription.__json__(request)

        # Validate annotation
        if check_conditions(annotation, data):
            try:
                subject, text, html, recipients = render_reply_notification(
                    request,
                    annotation,
                    parent)
                yield subject, text, html, recipients
            # ToDo: proper exception handling here
            except TemplateRenderException:
                log.exception('Failed to render subscription'
                              ' template %s', subscription)
            except:
                log.exception('Unknown error when trying to render'
                              ' subscription template %s', subscription)
Example #3
0
def send_notifications(event):
    # Extract data
    action = event.action
    request = event.request
    annotation = event.annotation

    # And for them we need only the creation action
    if action != 'create':
        return

    # Check for authorization. Send notification only for public annotation
    # XXX: This will be changed and fine grained when
    # user groups will be introduced
    if Everyone not in principals_allowed_by_permission(annotation, 'read'):
        return

    # Store the parent values as additional data
    data = {
        'parent': parent_values(annotation)
    }

    subscriptions = Subscriptions.get_active_subscriptions_for_a_type(
        request,
        types.REPLY_TYPE
    )
    for subscription in subscriptions:
        data['subscription'] = subscription.__json__(request)

        # Validate annotation
        if check_conditions(annotation, data):
            try:
                # Render e-mail parts
                tmap = create_template_map(request, annotation, data)
                text = render(TXT_TEMPLATE, tmap, request).strip()
                html = render(HTML_TEMPLATE, tmap, request).strip()
                subject = render(SUBJECT_TEMPLATE, tmap, request).strip()
                recipients = get_recipients(request, data)
                send_email(request, subject, text, html, recipients)
            # ToDo: proper exception handling here
            except TemplateRenderException:
                log.exception('Failed to render subscription'
                              ' template %s', subscription)
            except:
                log.exception('Unknown error when trying to render'
                              ' subscription template %s', subscription)
Example #4
0
def generate_notifications(request, annotation, action):
    # Only send notifications when new annotations are created
    if action != 'create':
        return

    # If the annotation doesn't have a parent, we can't find its parent, or we
    # have no idea who the author of the parent is, then we can't send a
    # notification email.
    parent_id = annotation.parent_id
    if parent_id is None:
        return
    parent = storage.fetch_annotation(request, parent_id)
    if parent is None or not annotation.userid:
        return

    # Don't send reply notifications to the author of the parent annotation if
    # the author doesn't have permission to read the reply.
    if not auth.has_permission(request, annotation, parent.userid, 'read'):
        return

    # Store the parent values as additional data
    data = {
        'parent': parent
    }

    subscriptions = Subscriptions.get_active_subscriptions_for_a_type(
        types.REPLY_TYPE)
    for subscription in subscriptions:
        data['subscription'] = subscription.__json__(request)

        # Validate annotation
        if check_conditions(annotation, data):
            try:
                subject, text, html, recipients = render_reply_notification(
                    request,
                    annotation,
                    parent)
                yield subject, text, html, recipients
            # ToDo: proper exception handling here
            except TemplateRenderException:
                log.exception('Failed to render subscription'
                              ' template %s', subscription)
            except:
                log.exception('Unknown error when trying to render'
                              ' subscription template %s', subscription)
Example #5
0
def generate_notifications(request, annotation, action):
    # Only send notifications when new annotations are created
    if action != 'create':
        return

    # If the annotation doesn't have a parent, we can't find its parent, or we
    # have no idea who the author of the parent is, then we can't send a
    # notification email.
    parent_id = annotation.parent_id
    if parent_id is None:
        return
    parent = storage.fetch_annotation(request, parent_id)
    if parent is None or not annotation.userid:
        return

    # Don't send reply notifications to the author of the parent annotation if
    # the author doesn't have permission to read the reply.
    if not auth.has_permission(request, annotation, parent.userid, 'read'):
        return

    # Store the parent values as additional data
    data = {'parent': parent}

    subscriptions = Subscriptions.get_active_subscriptions_for_a_type(
        types.REPLY_TYPE)
    for subscription in subscriptions:
        data['subscription'] = subscription.__json__(request)

        # Validate annotation
        if check_conditions(annotation, data):
            try:
                subject, text, html, recipients = render_reply_notification(
                    request, annotation, parent)
                yield subject, text, html, recipients
            # ToDo: proper exception handling here
            except TemplateRenderException:
                log.exception('Failed to render subscription'
                              ' template %s', subscription)
            except:
                log.exception(
                    'Unknown error when trying to render'
                    ' subscription template %s', subscription)
Example #6
0
def send_notifications(event):
    # Extract data
    action = event.action
    request = event.request
    annotation = event.annotation

    # And for them we need only the creation action
    if action != 'create':
        return

    # Check for authorization. Send notification only for public annotation
    # XXX: This will be changed and fine grained when
    # user groups will be introduced
    if Everyone not in principals_allowed_by_permission(annotation, 'read'):
        return

    # Store the parent values as additional data
    data = {'parent': parent_values(annotation)}

    subscriptions = Subscriptions.get_active_subscriptions_for_a_type(
        request, types.REPLY_TYPE)
    for subscription in subscriptions:
        data['subscription'] = subscription.__json__(request)

        # Validate annotation
        if check_conditions(annotation, data):
            try:
                # Render e-mail parts
                tmap = create_template_map(request, annotation, data)
                text = render(TXT_TEMPLATE, tmap, request).strip()
                html = render(HTML_TEMPLATE, tmap, request).strip()
                subject = render(SUBJECT_TEMPLATE, tmap, request).strip()
                recipients = get_recipients(request, data)
                send_email(request, subject, text, html, recipients)
            # ToDo: proper exception handling here
            except TemplateRenderException:
                log.exception('Failed to render subscription'
                              ' template %s', subscription)
            except:
                log.exception(
                    'Unknown error when trying to render'
                    ' subscription template %s', subscription)