Python backend for a Slack's kudos plugin.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #11 from Dekalabs/feat/dynamic-groups

feat: dynamic groups

authored by

Marcos Gabarda and committed by
GitHub
40e60967 8221f2c0

+50 -5
+7
.fleet/run.json
··· 50 50 "testFramework": "pytest", 51 51 "target": "kefi/tests/test_interactivity", 52 52 "arguments": [] 53 + }, 54 + { 55 + "type": "python-tests", 56 + "name": "Run plazas tests", 57 + "testFramework": "pytest", 58 + "target": "kefi/tests/plazas/helpers", 59 + "arguments": [] 53 60 } 54 61 ] 55 62 }
+2 -1
kefi/config/__init__.py
··· 16 16 PLAZA_PRICE: int = 10 17 17 PLAZA_DEFAULT_HOUR: int = 10 18 18 PLAZA_DEFAULT_MINUTE: int = 0 19 - PLAZA_SIZE: int = 4 19 + PLAZA_SIZE: str = "3-5" 20 + PLAZA_SIZE_OPTIONS: list[int] = [3, 4, 5] 20 21 LOCALE: str = "es_ES" 21 22 KUDOS_PRICE: int = 100 22 23 CONGRATS_PRICE: int = 25
+19 -4
kefi/models/plazas/helpers.py
··· 102 102 results = session.exec(query).all() 103 103 users = [attendance.user for attendance in results] 104 104 random.shuffle(users) 105 - return [ 106 - users[index : index + settings.PLAZA_SIZE] 107 - for index in range(0, len(users), settings.PLAZA_SIZE) 108 - ] 105 + return split_users_in_groups(users) 106 + 107 + 108 + def split_users_in_groups(users: list[User]) -> list[list]: 109 + users_len = len(users) 110 + if min(settings.PLAZA_SIZE_OPTIONS) > users_len: 111 + return [] 112 + group_size = next( 113 + filter(lambda size: users_len % size == 0, settings.PLAZA_SIZE_OPTIONS), 114 + settings.PLAZA_SIZE_OPTIONS[0], 115 + ) 116 + grouped_users = list( 117 + [users[i : i + group_size] for i in range(0, users_len, group_size)] 118 + ) 119 + if len(grouped_users[-1]) < group_size: 120 + orphan_users = grouped_users.pop() 121 + for i, user in enumerate(orphan_users): 122 + grouped_users[i % len(grouped_users)].append(user) 123 + return grouped_users 109 124 110 125 111 126 def notify_plaza(session: Session, plaza: Plaza | None = None):
kefi/tests/plazas/__init__.py

This is a binary file and will not be displayed.

+22
kefi/tests/plazas/helpers.py
··· 1 + from kefi.models.database import User 2 + from kefi.models.plazas.helpers import split_users_in_groups 3 + 4 + 5 + def test_split_users_in_groups(): 6 + mock_user_list = lambda size_list: ( 7 + User(slack_user_id="user_1") for i in range(size_list) 8 + ) 9 + # Creates some list of users 10 + # Here i would like to mock settings value to [3,4,5] for test to work always 11 + # Creates some list of users 12 + users_lists = [ 13 + {"list": mock_user_list(9), "expected_groups": 3}, 14 + {"list": mock_user_list(10), "expected_groups": 2}, 15 + {"list": mock_user_list(8), "expected_groups": 2}, 16 + {"list": mock_user_list(14), "expected_groups": 4}, 17 + {"list": mock_user_list(3), "expected_groups": 1}, 18 + {"list": mock_user_list(2), "expected_groups": 0}, 19 + ] 20 + 21 + for users in users_lists: 22 + assert len(split_users_in_groups(users.list)) == users.expected_groups