Sort persons according to the best ordering

This commit is contained in:
Christian Wolf 2022-11-15 19:36:04 +01:00
parent fb7b1f72eb
commit 04d8cdf52b
2 changed files with 46 additions and 0 deletions

View File

@ -358,3 +358,28 @@ def test_collectPersonsInGroups(mocker):
assert groups['Jun.'] == [persons[2], persons[8]] assert groups['Jun.'] == [persons[2], persons[8]]
assert groups['Jug.'] == [persons[5]] assert groups['Jug.'] == [persons[5]]
assert groups['Sonst'] == [persons[4], persons[6], persons[7]] assert groups['Sonst'] == [persons[4], persons[6], persons[7]]
def test_sortPersons_withId(mocker):
def mockPerson(id):
mock = mocker.patch('solo_turnier.worker.ResultPerson')
mock.id = id
return mock
persons = [mockPerson(2), mockPerson(1), mockPerson(5), mockPerson(3)]
dataWorker = worker.DataWorker()
sorted, showIds = dataWorker.sortPersonsInGroup(persons)
assert sorted == [persons[1], persons[0], persons[3], persons[2]]
assert showIds == True
def test_sortPersons_withoutId(mocker):
def mockPerson(name):
mock = mocker.patch('solo_turnier.worker.ResultPerson')
mock.id = 3
mock.name = name
mock.club = 'TSC Entenhausen'
return mock
persons = [mockPerson('Max'), mockPerson('Isabel'), mockPerson('Reimund'), mockPerson('Anna')]
persons[2].id = None
dataWorker = worker.DataWorker()
sorted, showIds = dataWorker.sortPersonsInGroup(persons)
assert sorted == [persons[3], persons[1], persons[0], persons[2]]
assert showIds == False

View File

@ -243,3 +243,24 @@ class DataWorker:
found = groups['Kin.'] + groups['Jun.'] + groups['Jug.'] found = groups['Kin.'] + groups['Jun.'] + groups['Jug.']
groups['Sonst'] = [p for p in data.keys() if p not in found] groups['Sonst'] = [p for p in data.keys() if p not in found]
return groups return groups
def sortPersonsInGroup(self, persons: list[ResultPerson]) -> list[ResultPerson]:
ids = [p.id for p in persons]
def decorateByName(p: ResultPerson):
return (f'{p.name} ({p.club})', p)
def decorateById(p: ResultPerson):
return (p.id, p)
if any([id == None for id in ids]):
# We need to sort by name
decorated = [decorateByName(p) for p in persons]
showIds = False
else:
decorated = [decorateById(p) for p in persons]
showIds = True
decorated.sort()
return ([d[1] for d in decorated], showIds)