コード例 #1
0
ファイル: handlers.py プロジェクト: rkondracki/crits
def get_user_notifications(username, count=False, newer_than=None):
    """
    Get the notifications for a user.

    :param username: The user to get notifications for.
    :type username: str
    :param count: Only return the count.
    :type count:bool
    :returns: int, :class:`crits.core.crits_mongoengine.CritsQuerySet`
    """
    n = None

    if newer_than is None or newer_than == None:
        if count:
            n = Notification.objects(
                users=username).order_by('-created').count()
        else:
            n = Notification.objects(users=username).order_by('-created')
    else:
        if count:
            n = Notification.objects(
                Q(users=username)
                & Q(created__gt=newer_than)).order_by('-created').count()
        else:
            n = Notification.objects(
                Q(users=username)
                & Q(created__gt=newer_than)).order_by('-created')
    return n
コード例 #2
0
def remove_user_notifications(username):
    """
    Remove a user from all notifications.

    :param username: The user to remove.
    :type username: str
    :returns: dict with keys "success" (boolean) and "message" (str) if failed.
    """

    Notification.objects(users=username).update(pull__users=username)
コード例 #3
0
def remove_user_from_notification_id(username, id):
    """
    Remove a user from the list of users for a notification.

    :param username: The user to remove.
    :type username: str
    :param obj_id: The ObjectId of the top-level object for this notification.
    :type obj_id: str
    :param obj_type: The top-level object type.
    :type obj_type: str
    :returns: dict with keys "success" (boolean) and "message" (str) if failed.
    """

    Notification.objects(id=id).update(pull__users=username)
    return {'success': True}
コード例 #4
0
    def handle(self, *args, **options):
        """
        Script Execution.
        """

        # only look for active users who want email notifications
        users = CRITsUser.objects(is_active=True,
                                  prefs__notify__email=True)
        # only get the unprocessed notifications
        notifications = Notification.objects(status='new')

        for user in users:
            # only include notifications where the user is in the users list and
            # it wasn't created by them.
            includes = [x for x in notifications if user.username in x.users and user.username != x.analyst and x.obj_id != None]

            # only send an email if there's something to send
            if len(includes):
                email = EmailNotification(username=user.username,
                                        email=user.email)
                for include in includes:
                    email.add_to_body(email.create_notification(include))
                email.send_email()

        # clean up after ourselves
        usernames = [u.username for u in users]
        self.process_notifications(notifications, usernames)
コード例 #5
0
    def handle(self, *args, **options):
        """
        Script Execution.
        """

        # only look for active users who want email notifications
        users = CRITsUser.objects(is_active=True, prefs__notify__email=True)
        # only get the unprocessed notifications
        notifications = Notification.objects(status='new')

        for user in users:
            # only include notifications where the user is in the users list and
            # it wasn't created by them.
            includes = [
                x for x in notifications if user.username in x.users
                and user.username != x.analyst and x.obj_id != None
            ]

            # only send an email if there's something to send
            if len(includes):
                email = EmailNotification(username=user.username,
                                          email=user.email)
                for include in includes:
                    email.add_to_body(email.create_notification(include))
                email.send_email()

        # clean up after ourselves
        usernames = [u.username for u in users]
        self.process_notifications(notifications, usernames)
コード例 #6
0
ファイル: handlers.py プロジェクト: 0x3a/crits
def get_user_notifications(username, count=False, newer_than=None):
    """
    Get the notifications for a user.

    :param username: The user to get notifications for.
    :type username: str
    :param count: Only return the count.
    :type count:bool
    :returns: int, :class:`crits.core.crits_mongoengine.CritsQuerySet`
    """
    n = None

    if newer_than is None or newer_than == None:
        n = Notification.objects(users=username).order_by('-created')
    else:
        n = Notification.objects(Q(users=username) & Q(created__gt=newer_than)).order_by('-created')

    if count:
        return len(n)
    else:
        return n
コード例 #7
0
ファイル: handlers.py プロジェクト: eltair/crits
def get_notifications_for_id(username, obj_id, obj_type):
    """
    Get notifications for a specific top-level object and user.

    :param username: The user to search for.
    :param obj_id: The ObjectId to search for.
    :type obj_id: str
    :param obj_type: The top-level object type.
    :type obj_type: str
    :returns: :class:`crits.core.crits_mongoengine.CritsQuerySet`
    """

    return Notification.objects(users=username, obj_id=obj_id, obj_type=obj_type)
コード例 #8
0
def get_notifications_for_id(username, obj_id, obj_type):
    """
    Get notifications for a specific top-level object and user.

    :param username: The user to search for.
    :param obj_id: The ObjectId to search for.
    :type obj_id: str
    :param obj_type: The top-level object type.
    :type obj_type: str
    :returns: :class:`crits.core.crits_mongoengine.CritsQuerySet`
    """

    return Notification.objects(users=username,
                                obj_id=obj_id,
                                obj_type=obj_type)
コード例 #9
0
ファイル: handlers.py プロジェクト: plouzek/crits-1
def get_user_notifications(username, count=False):
    """
    Get the notifications for a user.

    :param username: The user to get notifications for.
    :type username: str
    :param count: Only return the count.
    :type count:bool
    :returns: int, :class:`crits.core.crits_mongoengine.CritsQuerySet`
    """

    n = Notification.objects(users=username).order_by('-created')
    if count:
        return len(n)
    else:
        return n
コード例 #10
0
def remove_notification(obj_id):
    """
    Remove an existing notification.

    :param obj_id: The top-level ObjectId to find the notification to remove.
    :type obj_id: str
    :returns: dict with keys "success" (boolean) and "message" (str).
    """

    notification = Notification.objects(id=obj_id).first()
    if not notification:
        message = "Could not find notification to remove!"
        result = {'success': False, 'message': message}
    else:
        notification.delete()
        message = "Notification removed successfully!"
        result = {'success': True, 'message': message}
    return result
コード例 #11
0
    def process_notifications(self, notifications, users):
        """
        Set notifications to processed. Remove users from the list if they
        received an email. If any notification has 0 users left, remove it.
        Also remove any processed notifications with 0 users left.

        :param notifications: The list of notifications to work with.
        :type notifications: list
        :param users: The users to work with.
        :type users: list
        """

        old = Notification.objects(status='processed').only('users')
        for oldn in old:
            if not len(oldn.users):
                oldn.delete()

        for notice in notifications:
            notice.users = [u for u in notice.users if u not in users]
            if not len(notice.users):
                notice.delete()
            else:
                notice.set_status('processed')
                notice.save()
コード例 #12
0
    def process_notifications(self, notifications, users):
        """
        Set notifications to processed. Remove users from the list if they
        received an email. If any notification has 0 users left, remove it.
        Also remove any processed notifications with 0 users left.

        :param notifications: The list of notifications to work with.
        :type notifications: list
        :param users: The users to work with.
        :type users: list
        """

        old = Notification.objects(status='processed').only('users')
        for oldn in old:
            if not len(oldn.users):
                oldn.delete()

        for notice in notifications:
            notice.users = [u for u in notice.users if u not in users]
            if not len(notice.users):
                notice.delete()
            else:
                notice.set_status('processed')
                notice.save()
コード例 #13
0
def get_new_notifications():
    """
    Get any new notifications.
    """

    return Notification.objects(status="new")