예제 #1
0
def send_notification_as_email(
    notification: BaseNotification,
    users: Set[User],
    shared_context: Mapping[str, Any],
    extra_context_by_user_id: Optional[Mapping[int, Mapping[str, Any]]],
) -> None:
    headers = get_headers(notification)

    for user in users:
        extra_context = (extra_context_by_user_id or {}).get(user.id, {})
        log_message(notification, user)
        context = get_context(notification, user, shared_context,
                              extra_context)
        subject = get_subject_with_prefix(notification, context=context)
        msg = MessageBuilder(
            subject=subject,
            context=context,
            template=notification.get_template(),
            html_template=notification.get_html_template(),
            headers=headers,
            reference=notification.get_reference(),
            reply_reference=notification.get_reply_reference(),
            type=notification.get_type(),
        )
        msg.add_users([user.id], project=notification.project)
        msg.send_async()
예제 #2
0
def send_notification_as_email(
    notification: BaseNotification,
    recipients: Iterable[Union["Team", "User"]],
    shared_context: Mapping[str, Any],
    extra_context_by_user_id: Optional[Mapping[int, Mapping[str, Any]]],
) -> None:
    headers = get_headers(notification)

    for recipient in recipients:
        if isinstance(recipient, Team):
            # TODO(mgaeta): MessageBuilder only works with Users so filter out Teams for now.
            continue
        extra_context = (extra_context_by_user_id or {}).get(recipient.id, {})
        log_message(notification, recipient)
        context = get_context(notification, recipient, shared_context,
                              extra_context)
        subject = get_subject_with_prefix(notification, context=context)
        msg = MessageBuilder(
            subject=subject,
            context=context,
            template=notification.get_template(),
            html_template=notification.get_html_template(),
            headers=headers,
            reference=notification.get_reference(),
            reply_reference=notification.get_reply_reference(),
            type=notification.get_type(),
        )
        msg.add_users([recipient.id], project=notification.project)
        msg.send_async()
예제 #3
0
def get_builder_args_from_context(
        notification: BaseNotification,
        context: Mapping[str, Any]) -> MutableMapping[str, Any]:
    output = {
        "subject": get_subject_with_prefix(notification, context),
        "context": context,
        "template": notification.get_template(),
        "html_template": notification.get_html_template(),
        "headers": get_headers(notification),
        "reference": notification.get_reference(),
        "reply_reference": notification.get_reply_reference(),
        "type": notification.get_type(),
    }
    # add in optinal fields
    from_email = notification.from_email
    if from_email:
        output["from_email"] = from_email
    return output
예제 #4
0
def get_builder_args(
    notification: BaseNotification,
    recipient: User,
    shared_context: Mapping[str, Any] | None = None,
    extra_context_by_actor_id: Mapping[int, Mapping[str, Any]] | None = None,
) -> Mapping[str, Any]:
    # TODO: move context logic to single notification class method
    extra_context = (extra_context_by_actor_id
                     or {}).get(recipient.actor_id, {})
    context = get_context(notification, recipient, shared_context or {},
                          extra_context)
    return {
        "subject": get_subject_with_prefix(notification, context),
        "context": context,
        "template": notification.get_template(),
        "html_template": notification.get_html_template(),
        "headers": get_headers(notification),
        "reference": notification.get_reference(),
        "reply_reference": notification.get_reply_reference(),
        "type": notification.get_type(),
    }