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

17 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-07-20 21:46 +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 user_id=user_id, 

15 topic_action="badge:add", 

16 data=notification_data_pb2.BadgeAdd( 

17 badge_id=badge["id"], 

18 badge_name=badge["name"], 

19 badge_description=badge["description"], 

20 ), 

21 ) 

22 session.commit() 

23 

24 

25def user_remove_badge(session, user_id, badge_id): 

26 badge = get_badge_dict()[badge_id] 

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

28 session.flush() 

29 notify( 

30 user_id=user_id, 

31 topic_action="badge:remove", 

32 data=notification_data_pb2.BadgeRemove( 

33 badge_id=badge["id"], 

34 badge_name=badge["name"], 

35 badge_description=badge["description"], 

36 ), 

37 ) 

38 session.commit()