コード例 #1
0
ファイル: handlers.py プロジェクト: flowsand88/crits
def create_general_notification(username,
                                target_users,
                                header,
                                link_url,
                                message,
                                notification_type=NotificationType.ALERT):
    """
    Generate a general notification -- not based on mongo obj.

    :param obj: The object.
    :type obj: class which inherits from
               :class:`crits.core.crits_mongoengine.CritsBaseAttributes`
    :param username: The user creating the notification.
    :type username: str
    :param target_users: The list of users who will get the notification.
    :type target_users: list(str)
    :param header: The notification header message.
    :type header: list(str)
    :param link_url: A link URL for the header, specify None if there is no link.
    :type link_url: str
    :param message: The notification message.
    :type message: str
    :param notification_type: The notification type (e.g. alert, error).
    :type notification_type: str
    """

    if notification_type not in NotificationType.ALL:
        notification_type = NotificationType.ALERT

    n = Notification()
    n.analyst = username
    n.notification_type = notification_type
    n.notification = message
    n.header = header
    n.link_url = link_url

    for target_user in target_users:
        # Check to make sure the user actually exists
        user = CRITsUser.objects(username=target_user).first()
        if user is not None:
            n.users.append(target_user)

    # don't notify the user creating this notification
    n.users = [u for u in n.users if u != username]
    if not len(n.users):
        return
    try:
        n.save()
    except ValidationError:
        pass

    # Signal potentially waiting threads that notification information is available
    for user in n.users:
        notification_lock = NotificationLockManager.get_notification_lock(user)
        notification_lock.acquire()

        try:
            notification_lock.notifyAll()
        finally:
            notification_lock.release()
コード例 #2
0
ファイル: handlers.py プロジェクト: eltair/crits
def create_general_notification(
    username, target_users, header, link_url, message, notification_type=NotificationType.ALERT
):
    """
    Generate a general notification -- not based on mongo obj.

    :param obj: The object.
    :type obj: class which inherits from
               :class:`crits.core.crits_mongoengine.CritsBaseAttributes`
    :param username: The user creating the notification.
    :type username: str
    :param target_users: The list of users who will get the notification.
    :type target_users: list(str)
    :param header: The notification header message.
    :type header: list(str)
    :param link_url: A link URL for the header, specify None if there is no link.
    :type link_url: str
    :param message: The notification message.
    :type message: str
    :param notification_type: The notification type (e.g. alert, error).
    :type notification_type: str
    """

    if notification_type not in NotificationType.ALL:
        notification_type = NotificationType.ALERT

    n = Notification()
    n.analyst = username
    n.notification_type = notification_type
    n.notification = message
    n.header = header
    n.link_url = link_url

    for target_user in target_users:
        # Check to make sure the user actually exists
        user = CRITsUser.objects(username=target_user).first()
        if user is not None:
            n.users.append(target_user)

    # don't notify the user creating this notification
    n.users = [u for u in n.users if u != username]
    if not len(n.users):
        return
    try:
        n.save()
    except ValidationError:
        pass

    # Signal potentially waiting threads that notification information is available
    for user in n.users:
        notification_lock = NotificationLockManager.get_notification_lock(user)
        notification_lock.acquire()

        try:
            notification_lock.notifyAll()
        finally:
            notification_lock.release()