Coverage for src/couchers/notifications/settings.py: 40%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

15 statements  

1import logging 

2 

3from couchers.models import NotificationDelivery, NotificationPreference 

4from couchers.sql import couchers_select as select 

5 

6logger = logging.getLogger(__name__) 

7 

8 

9def reset_preference(session, user_id, topic_action, delivery_type): 

10 current_pref = session.execute( 

11 select(NotificationPreference) 

12 .where(NotificationPreference.id == user_id) 

13 .where(NotificationPreference.topic_action == topic_action) 

14 .where(NotificationDelivery.delivery_type == delivery_type) 

15 ).scalar_one_or_none() 

16 if current_pref: 

17 session.delete(current_pref) 

18 session.flush() 

19 

20 

21def set_preference(session, user_id, topic_action, delivery_type, deliver): 

22 current_pref = session.execute( 

23 select(NotificationPreference) 

24 .where(NotificationPreference.id == user_id) 

25 .where(NotificationPreference.topic_action == topic_action) 

26 .where(NotificationDelivery.delivery_type == delivery_type) 

27 ).scalar_one_or_none() 

28 if current_pref: 

29 current_pref.deliver = deliver 

30 else: 

31 session.add( 

32 NotificationPreference( 

33 user_id=user_id, 

34 topic_action=topic_action, 

35 delivery_type=delivery_type, 

36 deliver=deliver, 

37 ) 

38 ) 

39 session.flush()