Beispiel #1
0
    def test_notification_auto_deletion(self, student_user: User):
        extra = 3
        start = Notification.MAX_PER_USER
        for _ in range(Notification.MAX_PER_USER + extra - 1):
            Notification.create_notification(
                user=student_user,
                notification_type='',
                message_parameters={},
                related_object_id=1,
            )

        assert Notification.select().count() == start
        expected = start + extra - 1
        actual = Notification.select().order_by(
            Notification.created.desc()).get().id
        assert expected == actual
Beispiel #2
0
    def test_auto_deletion(self, student_user: User):
        extra = 3
        start = Notification.MAX_PER_USER
        for _ in range(Notification.MAX_PER_USER + extra - 1):
            notifications.send(
                user=student_user,
                kind=notifications.NotificationKind.CHECKED,
                message='',
                related_id=1,
                action_url='/view/1',
            )

        assert Notification.select().count() == start
        expected = start + extra - 1
        actual = Notification.select().order_by(
            Notification.created.desc()).get().id
        assert expected == actual
Beispiel #3
0
def create_notification(
    student_user: User,
    solution: Solution,
    index: int = 0,
) -> Notification:
    return Notification.create(
        user=student_user,
        kind=notifications.NotificationKind.CHECKED.value,
        message=f'Test message {index}',
        related_id=solution.id,
        action_url=f'{routes.SOLUTIONS}/{solution.id}',
    )
Beispiel #4
0
def send(
    user: User,
    kind: NotificationKind,
    message: str,
    related_id: Optional[int] = None,
    action_url: Optional[str] = None,
) -> Notification:
    return Notification.send(
        user,
        kind.value,
        message,
        related_id,
        action_url,
    )
Beispiel #5
0
def read(user: Optional[User] = None, id_: Optional[int] = None) -> bool:
    if id_:
        try:
            notification = Notification.get_by_id(id_)
        except Notification.DoesNotExist:
            return False

        if user and (user.id != notification.user.id):
            return False

        notification.read()
        return True

    assert user, 'Must provide user or id_'  # noqa: B101, S101
    is_success = [msg.read() for msg in get(user)]
    return all(is_success)  # Not gen to prevent lazy evaluation
Beispiel #6
0
 def test_read_related(
     self,
     student_user: User,
     solution: Solution,
     exercise: Exercise,
 ):
     solution2 = conftest.create_solution(student_user, exercise)
     student_user2 = conftest.create_user(index=1)
     messages = [
         conftest.create_notification(student_user, solution),
         conftest.create_notification(student_user, solution),
         conftest.create_notification(student_user, solution),
         conftest.create_notification(student_user, solution2),
         conftest.create_notification(student_user2, solution),
     ]
     assert all(not n.viewed for n in messages)
     notifications.read_related(solution.id, student_user)
     # peewee...
     messages = [Notification.get_by_id(n.id) for n in messages]
     assert all(n.viewed for n in messages[:3])
     assert all(not n.viewed for n in messages[3:])
Beispiel #7
0
def read_related(related_id: int, user: int):
    for n in Notification.of(related_id, user):
        n.read()
Beispiel #8
0
def get(user: User) -> Iterable[Notification]:
    return Notification.fetch(user)
Beispiel #9
0
 def get_unread(user: User):
     return (Notification.select().where(
         Notification.user == user,
         Notification.viewed == False,  # NOQA: E712
     ))
Beispiel #10
0
 def check_viewed_inbox(user: User):
     with pytest.raises(Notification.DoesNotExist):
         Notification.get(user=user, viewed=False)