Coverage for src/couchers/helpers/badges.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-12-02 20:27 +0000

1from sqlalchemy.sql import delete 

2 

3from couchers.models import UserBadge 

4from couchers.notifications.notify import notify 

5from couchers.resources import get_badge_dict 

6from proto import notification_data_pb2 

7 

8 

9def user_add_badge(session, user_id, badge_id): 

10 badge = get_badge_dict()[badge_id] 

11 session.add(UserBadge(user_id=user_id, badge_id=badge_id)) 

12 session.flush() 

13 notify( 

14 session, 

15 user_id=user_id, 

16 topic_action="badge:add", 

17 data=notification_data_pb2.BadgeAdd( 

18 badge_id=badge["id"], 

19 badge_name=badge["name"], 

20 badge_description=badge["description"], 

21 ), 

22 ) 

23 session.commit() 

24 

25 

26def user_remove_badge(session, user_id, badge_id): 

27 badge = get_badge_dict()[badge_id] 

28 session.execute(delete(UserBadge).where(UserBadge.user_id == user_id, UserBadge.badge_id == badge_id)) 

29 session.flush() 

30 notify( 

31 session, 

32 user_id=user_id, 

33 topic_action="badge:remove", 

34 data=notification_data_pb2.BadgeRemove( 

35 badge_id=badge["id"], 

36 badge_name=badge["name"], 

37 badge_description=badge["description"], 

38 ), 

39 ) 

40 session.commit()