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

18 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-12-20 18:03 +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, do_notify=True): 

10 badge = get_badge_dict()[badge_id] 

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

12 session.flush() 

13 if do_notify: 

14 notify( 

15 session, 

16 user_id=user_id, 

17 topic_action="badge:add", 

18 data=notification_data_pb2.BadgeAdd( 

19 badge_id=badge["id"], 

20 badge_name=badge["name"], 

21 badge_description=badge["description"], 

22 ), 

23 ) 

24 session.commit() 

25 

26 

27def user_remove_badge(session, user_id, badge_id): 

28 badge = get_badge_dict()[badge_id] 

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

30 session.flush() 

31 notify( 

32 session, 

33 user_id=user_id, 

34 topic_action="badge:remove", 

35 data=notification_data_pb2.BadgeRemove( 

36 badge_id=badge["id"], 

37 badge_name=badge["name"], 

38 badge_description=badge["description"], 

39 ), 

40 ) 

41 session.commit()