From 6ecf4ea1030a7b363138a633fa408b89e67d4a34 Mon Sep 17 00:00:00 2001 From: Trygve Date: Tue, 31 May 2022 23:12:38 +0200 Subject: [PATCH] La til Fee import og eksport MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Event har nå fees attributen. Fee kan importeres fra xml påmeldingsfiler og importeres og eksporteres fra .otime filer. --- otime.py | 133 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 98 insertions(+), 35 deletions(-) diff --git a/otime.py b/otime.py index 0e666da..644acdd 100644 --- a/otime.py +++ b/otime.py @@ -34,6 +34,11 @@ class Event: except KeyError: self.card_dumps = [] + try: + self.fees = kwargs['fees'] + except KeyError: + self.fees = [] + def add_course(self, *args): for n in args: self.courses.append(n) @@ -46,8 +51,13 @@ class Event: for n in args: self.runners.append(n) + def add_fees(self, *args): + for n in args: + self.fees.append(n) + def import_xml_entries(self, xml_file): - self.add_runners(*runners_from_xml_entries(xml_file, self.o_classes)) + self.add_runners(*runners_from_xml_entries(xml_file)) + self.add_fees(*fees_from_xml_entries(xml_file)) def import_ttime_cnf(self, ttime_file): self.add_course(*courses_from_ttime_conf(ttime_file)) @@ -194,13 +204,18 @@ class Event: for dump in self.card_dumps: ddicts.append(dump.asdict()) + fdicts = [] + for fee in self.fees: + fdicts.append(fee.asdict()) + json_data = { 'id': self.id, 'name': self.name, 'runners': rdicts, 'courses': cdicts, 'o_classes': ocdicts, - 'card_dumps': ddicts + 'card_dumps': ddicts, + 'fees': fdicts } return json.dumps(json_data, sort_keys=True, indent=4) @@ -223,6 +238,10 @@ class Event: for d in data['card_dumps']: card_dumps.append(CardDump(d['card'], d['controls'], d['splits'], datetime.datetime.fromisoformat(d['read_time']), datetime.datetime.fromisoformat(d['s_time']), datetime.datetime.fromisoformat(d['f_time']))) + fees = [] + for f in data['fees']: + fees.append(Fee(f['id'], f['name'], f['currency'], f['amount'], from_birth_date=f['from_birth_date'], to_birth_date=f['to_birth_date'])) + return Event(data['id'], data['name'], runners=runners, courses=courses, o_classes=o_classes, card_dumps=card_dumps) def create_start_list_pdf(self, file_name): @@ -575,11 +594,32 @@ class OClass: } class Fee: - def __init__(self, fee_id, name, currency, amount): + def __init__(self, fee_id, name, currency, amount, **kwargs): self.id = fee_id + self.name = name self.currency = currency self.amount = amount + try: + self.from_birth_date = kwargs['from_birth_date'] + except KeyError: + self.from_birth_date = None + + try: + self.to_birth_date = kwargs['to_birth_date'] + except KeyError: + self.to_birth_date = None + + def asdict(self): + return { + 'id': self.id, + 'name': self.name, + 'currency': self.currency, + 'amount': self.amount, + 'from_birth_date': self.from_birth_date, + 'to_birth_date': self.to_birth_date + } + # TODO: Take string instead of file. def courses_from_ttime_conf(ttime_file): courses = [] @@ -609,45 +649,68 @@ def classes_from_ttime_conf(ttime_file, courses): loops += 1 return o_classes -def runners_from_xml_entries(xml_file, o_classes=[]): +def runners_from_xml_entries(xml_file): tree = ET.parse(xml_file) root = tree.getroot() - runnerarray = [] + runners = [] + + person_entries = root.findall('./{http://www.orienteering.org/datastandard/3.0}PersonEntry') + for p_entry in person_entries: + + rid = p_entry[1][0].text + + person = p_entry.find('./{http://www.orienteering.org/datastandard/3.0}Person') + name = person.find('./{http://www.orienteering.org/datastandard/3.0}Name') + first = name.find('./{http://www.orienteering.org/datastandard/3.0}Given').text + last = name.find('./{http://www.orienteering.org/datastandard/3.0}Family').text + + organisation = p_entry.find('./{http://www.orienteering.org/datastandard/3.0}Organisation') + club_id = organisation.find('./{http://www.orienteering.org/datastandard/3.0}Id').text + club_name = organisation.find('./{http://www.orienteering.org/datastandard/3.0}Name').text + club_name_short = organisation.find('./{http://www.orienteering.org/datastandard/3.0}ShortName').text + country = organisation.find('./{http://www.orienteering.org/datastandard/3.0}Country').attrib['code'] + + class_el = p_entry.find('./{http://www.orienteering.org/datastandard/3.0}Class') + class_str = class_el.find('./{http://www.orienteering.org/datastandard/3.0}Name').text - for person_root in root[1:]: - rid = person_root[1][0].text - first = person_root[1][1][1].text - last = person_root[1][1][0].text try: - club_id = person_root[2][0].text - club = person_root[2][1].text - except: - club = "None" - country = person_root[1][3].text - try: - card = int(person_root[3].text) - except: - card = 0 - runner_o_class = None - try: - xml_class_str = person_root[4][1].text - except: - # VELDIG MIDLERTIDIG - runner_o_class = o_class_list[0] - else: - for i in o_classes: - if i.name == xml_class_str: - runner_o_class = i - break - # Gjør sånn at den lager nye o klasser om den ikke finnes fra før + card = int(p_entry.find('./{http://www.orienteering.org/datastandard/3.0}ControlCard').text) + except AttributeError: + card = None start_time = None - runnerarray.append(Runner(rid, first, last, club=club, club_id=club_id, - country=country,card=card, o_class_str=xml_class_str, - o_class=runner_o_class, start_time=start_time)) - #print(rid, first, last, club_id, club, card, xml_class_str) + runners.append(Runner(rid, first, last, club=club_name, club_id=club_id, + country=country,card=card, o_class_str=class_str, + start_time=start_time)) + return runners - return runnerarray +def fees_from_xml_entries(xml_file): + tree = ET.parse(xml_file) + root = tree.getroot() + + allfees = root.findall('.//{http://www.orienteering.org/datastandard/3.0}Fee') + added_ids = [] + fee_objs = [] + for fee in allfees: + f_id = fee.find('./{http://www.orienteering.org/datastandard/3.0}Id').text + if f_id not in added_ids: + added_ids.append(f_id) + + fee_id = f_id + name = fee.find('./{http://www.orienteering.org/datastandard/3.0}Name').text + currency = fee.find('./{http://www.orienteering.org/datastandard/3.0}Amount').attrib['currency'] + amount = fee.find('./{http://www.orienteering.org/datastandard/3.0}Amount').text + try: + from_birth_date = fee.find('./{http://www.orienteering.org/datastandard/3.0}FromDateOfBirth').text + except AttributeError: + from_birth_date = None + try: + to_birth_date = fee.find('./{http://www.orienteering.org/datastandard/3.0}ToDateOfBirth').text + except AttributeError: + to_birth_date = None + + fee_objs.append(Fee(fee_id, name, currency, amount, from_birth_date=from_birth_date, to_birth_date=to_birth_date)) + return fee_objs # Checks if small list is in big list def contains(small, big):