def test_clear_cache_keys_wrong_type(client):
    ckey = Notification.__cache_key_notification_count__.format(
        user_id=1, type='subscripple'
    )
    cache.set(ckey, 100)
    with pytest.raises(APIException):
        Notification.clear_cache_keys(1, 'not_real_type')
def test_clear_cache_keys_without_type(client):
    ckey = Notification.__cache_key_notification_count__.format(
        user_id=1, type=1
    )
    cache.set(ckey, 100)
    Notification.clear_cache_keys(1)
    assert not cache.has(ckey)
Exemple #3
0
def modify_notification(id: int, read: bool):
    """
    Change the read status of a notification. Requires the
    ``notifications_modify`` permission. Modifying another user's
    notifications requires the ``notifications_modify_others`` permission.

    .. :quickref: Notification; Flag notification as read/unread.

    **Example response**:

    .. parsed-literal::

       {
         "status": "success",
         "response": "Notification 1243 marked as read."
       }

    :>json str response: Response message

    :statuscode 200: Successfully modified notification.
    :statuscode 403: User does not have permission to modify notifications.
    :statuscode 404: Notification does not exist.
    """
    noti = Notification.from_pk(id,
                                asrt=NotificationPermissions.MODIFY_OTHERS,
                                error=True)
    noti.read = read
    db.session.commit()
    Notification.clear_cache_keys(noti.user_id, noti.type)
    return flask.jsonify(
        f'Notification {id} marked as {"read" if read else "unread"}.')
def test_clear_cache_keys(client):
    ckey = Notification.__cache_key_notification_count__.format(
        user_id=1, type=1
    )
    cache.set(ckey, 100)
    Notification.clear_cache_keys(1, 'subscripple')
    assert not cache.has(ckey)
Exemple #5
0
def clear_notifications(read: bool, user: User, type: str = None):
    """
    Clear a user's notifications; optionally of a specific type. Requires the
    ``notifications_modify`` permission. Clearing another user's notifications
    requires the ``notifications_modify_others`` permission.

    .. :quickref: Notification; View notifications of a type.

    **Example response**:

    .. parsed-literal::

       {
         "status": "success",
         "response": "All notifications cleared."
       }

    :>json str response: Response message

    :statuscode 200: Successfully cleared notifications.
    :statuscode 403: User does not have permission to clear notifications.
    """
    if not read:
        raise APIException('You cannot set all notifications to unread.')
    Notification.update_many(
        pks=Notification.get_pks_from_type(user.id, type, include_read=False),
        update={'read': True},
    )
    Notification.clear_cache_keys(user.id)
    return flask.jsonify(
        f'{"All" if not type else type} notifications cleared.')