Example #1
0
def delete_matching_notifications(target, kind_name, **kwargs):
    kind_ref = NotificationKind.get(name=kind_name)

    # Load all notifications for the user with the given kind.
    notifications = Notification.select().where(
        Notification.target == target, Notification.kind == kind_ref
    )

    # For each, match the metadata to the specified values.
    for notification in notifications:
        matches = True
        try:
            metadata = json.loads(notification.metadata_json)
        except:
            continue

        for (key, value) in kwargs.items():
            if not key in metadata or metadata[key] != value:
                matches = False
                break

        if not matches:
            continue

        notification.delete_instance()
Example #2
0
def list_notifications(user,
                       kind_name=None,
                       id_filter=None,
                       include_dismissed=False,
                       page=None,
                       limit=None):

    base_query = Notification.select(
        Notification.id,
        Notification.uuid,
        Notification.kind,
        Notification.metadata_json,
        Notification.dismissed,
        Notification.lookup_path,
        Notification.created,
        Notification.created.alias("cd"),
        Notification.target,
    ).join(NotificationKind)

    if kind_name is not None:
        base_query = base_query.where(NotificationKind.name == kind_name)

    if id_filter is not None:
        base_query = base_query.where(Notification.uuid == id_filter)

    if not include_dismissed:
        base_query = base_query.where(Notification.dismissed == False)

    # Lookup directly for the user.
    user_direct = base_query.clone().where(Notification.target == user)

    # Lookup via organizations admined by the user.
    Org = User.alias()
    AdminTeam = Team.alias()
    AdminTeamMember = TeamMember.alias()
    AdminUser = User.alias()

    via_orgs = (base_query.clone().join(
        Org, on=(Org.id == Notification.target)).join(
            AdminTeam, on=(Org.id == AdminTeam.organization)).join(
                TeamRole,
                on=(AdminTeam.role == TeamRole.id)).switch(AdminTeam).join(
                    AdminTeamMember,
                    on=(AdminTeam.id == AdminTeamMember.team)).join(
                        AdminUser, on=(AdminTeamMember.user == AdminUser.id
                                       )).where((AdminUser.id == user)
                                                & (TeamRole.name == "admin")))

    query = user_direct | via_orgs

    if page:
        query = query.paginate(page, limit)
    elif limit:
        query = query.limit(limit)

    return query.order_by(SQL("cd desc"))
Example #3
0
def lookup_notifications_by_path_prefix(prefix):
    return list(
        (Notification.select().where(Notification.lookup_path % prefix)))