Skip to content

Commit

Permalink
fix: fix bib and card_numbers indexes (sportorg#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-karpov authored Aug 18, 2024
1 parent ec8d804 commit b571fe9
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 67 deletions.
4 changes: 2 additions & 2 deletions sportorg/gui/dialogs/entry_mass_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def accept(self, *args, **kwargs):
cur_person.qual = change_qual

if self.bib_checkbox.isChecked():
cur_person.change_bib(change_bib)
cur_person.set_bib(change_bib)

if self.world_code_checkbox.isChecked():
cur_person.world_code = change_world_code
Expand All @@ -210,7 +210,7 @@ def accept(self, *args, **kwargs):
cur_person.national_code = change_national_code

if self.card_checkbox.isChecked():
cur_person.change_card(change_card_number)
cur_person.set_card_number(change_card_number)

if self.start_time_checkbox.isChecked():
cur_person.start_time = change_start_time
Expand Down
4 changes: 2 additions & 2 deletions sportorg/gui/dialogs/merge_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def apply_changes_impl(self):

result_list.sort(key=lambda c: c.finish_time)
final_result = ResultSportident()
final_result.change_bib(cur_bib)
final_result.bib = cur_bib
final_result.person = result_list[0].person
final_result.change_card(result_list[0].card_number)
final_result.card_number = result_list[0].card_number
cur_cp = first_cp

for i in result_list:
Expand Down
21 changes: 17 additions & 4 deletions sportorg/gui/dialogs/person_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
from sportorg.gui.global_access import GlobalAccess
from sportorg.language import translate
from sportorg.models.constant import get_names, get_race_groups, get_race_teams
from sportorg.models.memory import Limit, Organization, Qualification, find, race
from sportorg.models.memory import (
Limit,
Organization,
Person,
Qualification,
find,
race,
)
from sportorg.models.result.result_calculation import ResultCalculation
from sportorg.modules.configs.configs import Config
from sportorg.modules.live.live import live_client
Expand All @@ -25,11 +32,13 @@ class PersonEditDialog(BaseDialog):
GROUP_NAME = ''
ORGANIZATION_NAME = ''

def __init__(self, person, is_new=False):
def __init__(self, person: Person, is_new=False):
super().__init__(GlobalAccess().get_main_window())
self.current_object = person
self.is_new = is_new
self.is_item_valid = {}
self.bib = person.bib
self.card_number = person.card_number

time_format = 'hh:mm:ss'
if race().get_setting('time_accuracy', 0):
Expand Down Expand Up @@ -91,7 +100,7 @@ def __init__(self, person, is_new=False):
),
NumberField(
title=translate('Bib'),
object=person,
object=self,
key='bib',
id='bib',
minimum=0,
Expand Down Expand Up @@ -123,7 +132,7 @@ def __init__(self, person, is_new=False):
),
NumberField(
title=translate('Punch card #'),
object=person,
object=self,
key='card_number',
id='card_number',
minimum=0,
Expand Down Expand Up @@ -264,6 +273,10 @@ def on_card_number_changed(self):

def apply(self):
person = self.current_object
if self.bib != person.bib:
person.set_bib(self.bib)
if self.card_number != person.card_number:
person.set_card_number(self.card_number)
if self.is_new:
race().add_person(person)

Expand Down
8 changes: 4 additions & 4 deletions sportorg/gui/dialogs/result_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from sportorg.gui.utils.custom_controls import AdvComboBox, AdvSpinBox, AdvTimeEdit
from sportorg.language import translate
from sportorg.models.constant import StatusComments
from sportorg.models.memory import Limit, ResultStatus, Split, race
from sportorg.models.memory import Limit, Result, ResultStatus, Split, race
from sportorg.models.result.result_calculation import ResultCalculation
from sportorg.models.result.result_checker import ResultChecker, ResultCheckerException
from sportorg.models.result.split_calculation import GroupSplits
Expand All @@ -36,7 +36,7 @@
class ResultEditDialog(QDialog):
def __init__(self, result, is_new=False):
super().__init__(GlobalAccess().get_main_window())
self.current_object = result
self.current_object: Result = result
self.is_new = is_new

self.time_format = 'hh:mm:ss'
Expand Down Expand Up @@ -260,14 +260,14 @@ def apply_changes_impl(self):
if new_bib == 0:
if result.person and result.is_punch():
if result.person.card_number == result.card_number:
result.person.change_card(0)
result.person.set_card_number(0)
result.person = None
elif cur_bib != new_bib:
new_person = race().find_person_by_bib(new_bib)
if new_person:
if result.person:
if result.is_punch():
result.person.change_card(0)
result.person.set_card_number(0)
result.person = new_person
if result.is_punch():
race().person_card_number(result.person, result.card_number)
Expand Down
1 change: 1 addition & 0 deletions sportorg/gui/dialogs/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
get_current_race_index,
move_down_race,
move_up_race,
race,
races,
set_current_race_index,
)
Expand Down
2 changes: 1 addition & 1 deletion sportorg/gui/dialogs/text_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def set_property(person, key, value, **options):
elif key == translate('Bib'):
if value.isdigit():
new_bib = int(value)
person.change_bib(new_bib)
person.set_bib(new_bib)
elif key == translate('Comment'):
person.comment = value
elif key == translate('IOF id'):
Expand Down
1 change: 1 addition & 0 deletions sportorg/gui/menu/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ class RecheckingAction(Action, metaclass=ActionFactory):
def execute(self):
ResultChecker.check_all()
ResultCalculation(race()).process_results()
race().rebuild_indexes()
self.app.refresh()


Expand Down
4 changes: 2 additions & 2 deletions sportorg/gui/tabs/memory_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ def duplicate(self, position):
person = self.race.persons[position]
new_person = copy(person)
new_person.id = uuid.uuid4()
new_person.bib = 0
new_person.card_number = 0
new_person.set_bib(0)
new_person.set_card_number(0)
self.race.persons.insert(position, new_person)

def get_values_from_object(self, obj):
Expand Down
117 changes: 92 additions & 25 deletions sportorg/models/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1282,8 +1282,8 @@ def __init__(self):
self.name = ''
self.surname = ''

self.card_number = 0
self.bib = 0
self._card_number = 0
self._bib = 0

self.birth_date: Optional[date] = None
self.organization: Optional[Organization] = None
Expand Down Expand Up @@ -1372,8 +1372,8 @@ def to_dict(self):
def update_data(self, data):
self.name = str(data['name'])
self.surname = str(data['surname'])
self.change_card(data['card_number'])
self.change_bib(int(data['bib']))
self.set_card_number(int(data['card_number']))
self.set_bib(int(data['bib']))
self.contact = []
if data['world_code']:
self.world_code = str(data['world_code'])
Expand All @@ -1393,25 +1393,70 @@ def update_data(self, data):
elif 'year' in data and data['year']: # back compatibility with v 1.0.0
self.set_year(int(data['year']))

def change_bib(self, new_bib: int):
if self.bib == new_bib:
@property
def bib(self):
return self._bib

@bib.setter
def bib(self, new_bib: int):
raise NotImplementedError('Please, use set_bib()')

def set_bib(self, new_bib: int) -> None:
if self._bib == new_bib:
return
self._index_bib(new_bib)
self._bib = new_bib

def _index_bib(self, new_bib: int) -> None:
r = race()
if self.bib in r.person_index_bib:
if self._bib != new_bib and self._bib in r.person_index_bib:
r.person_index_bib.pop(self.bib)
self.bib = new_bib
r.index_person(self)
if new_bib > 0 and new_bib in r.person_index_bib:
other_person = r.person_index_bib[new_bib]
if not other_person is self:
logging.info(
'Duplicate bib %s (do nothing): %s | %s',
new_bib,
self,
other_person,
)
r.person_index_bib[new_bib] = self

def index_bib(self) -> None:
self._index_bib(self.bib)

@property
def card_number(self):
return self._card_number

@card_number.setter
def card_number(self, new_card: int):
raise NotImplementedError('Please, use set_card_number()')

def change_card(self, new_card: int):
if self.card_number == new_card:
def set_card_number(self, new_card: int):
if self._card_number == new_card:
return

self._index_card(new_card)
self._card_number = new_card

def _index_card(self, new_card):
r = race()
if self.card_number in r.person_index_card:
r.person_index_card.pop(self.card_number)
self.card_number = new_card
r.index_person(self)
if self._card_number != new_card and self._card_number in r.person_index_card:
r.person_index_card.pop(self._card_number)
if new_card > 0 and new_card in r.person_index_card:
other_person = r.person_index_card[new_card]
if not other_person is self:
logging.info(
'Duplicate card %s (do nothing): %s | %s',
new_card,
self,
other_person,
)
r.person_index_card[new_card] = self

def index_card(self):
self._index_card(self._card_number)


class RaceData(Model):
Expand Down Expand Up @@ -1714,27 +1759,49 @@ def get_setting(self, setting, nvl_value=None):
def get_days(self, date_=None):
return self.data.get_days(date_)

def person_card_number(self, person, number=0):
person.change_card(number)
def person_card_number(self, person: Person, number=0):
person.set_card_number(number)
for p in self.persons:
if p.card_number == number and p != person:
p.card_number = 0
p.set_card_number(0)
p.is_rented_card = False
return p

def rebuild_indexes(self):
self.person_index_bib = {}
self.person_index_card = {}
for person in self.persons:
person.index_bib()
person.index_card()

def delete_persons(self, indexes):
indexes = sorted(indexes, reverse=True)
persons = []
for i in indexes:
person = self.persons[i]
persons.append(person)
for result in self.results:
if result.person is person:
result.person = None
result.bib = person.bib
self.remove_person_from_indexes(person)
del self.persons[i]
return persons

def remove_person_from_indexes(self, person: Person):
for result in self.results:
if result.person is person:
result.person = None
result.bib = person.bib
if (
person.bib
and person.bib in self.person_index_bib
and self.person_index_bib[person.bib] is person
):
del self.person_index_bib[person.bib]
if (
person.card_number
and person.card_number in self.person_index_card
and self.person_index_card[person.card_number] is person
):
del self.person_index_card[person.card_number]

def delete_results(self, indexes):
indexes = sorted(indexes, reverse=True)
results = []
Expand Down Expand Up @@ -1789,12 +1856,12 @@ def find_person_result(self, person: Person) -> Optional[Result]:
return i
return None

def find_person_by_bib(self, bib: int):
def find_person_by_bib(self, bib: int) -> Person:
if bib in self.person_index_bib:
return self.person_index_bib[bib]
return None

def find_person_by_card(self, card: int):
def find_person_by_card(self, card: int) -> Person:
if card in self.person_index_card:
return self.person_index_card[card]
return None
Expand Down Expand Up @@ -2295,7 +2362,7 @@ def is_out_of_competition(self):

def set_bib(self):
if self.person:
self.person.change_bib(self.get_bib())
self.person.set_bib(self.get_bib())

def set_person(self, person):
self.person = person
Expand Down
2 changes: 1 addition & 1 deletion sportorg/models/start/relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_leg_count():


def set_next_relay_number_to_person(person):
person.change_bib(get_next_relay_number_setting())
person.set_bib(get_next_relay_number_setting())
set_next_relay_number(get_next_relay_number(person.bib))


Expand Down
Loading

0 comments on commit b571fe9

Please sign in to comment.