Mindre repetisjon i eksport funksjonen

This commit is contained in:
Trygve 2022-02-18 11:01:55 +01:00
parent ebc023764e
commit b33cf0274a

View File

@ -226,19 +226,21 @@ def rank_runners(allrunners, o_class):
runners_ranked.append(i) runners_ranked.append(i)
return runners_ranked return runners_ranked
def xml_child(parent, tag, content):
e = ET.SubElement(parent, tag)
e.text = str(content)
def gen_xml_result(runners, o_classes): def gen_xml_result(runners, o_classes):
root = ET.Element('ResultList') root = ET.Element('ResultList')
tree = ET.ElementTree(root) tree = ET.ElementTree(root)
event = ET.SubElement(root, 'Event') event = ET.SubElement(root, 'Event')
ename = ET.SubElement(event, 'Name') xml_child(event, 'Name', 'PLACEHOLDER')
ename.text = 'PLACEHOLDER'
for i in o_classes: for i in o_classes:
#<ClassResult> #<ClassResult>
class_result = ET.SubElement(root, 'ClassResult') class_result = ET.SubElement(root, 'ClassResult')
#<Class> #<Class>
t = ET.SubElement(class_result, 'Class') t = ET.SubElement(class_result, 'Class')
name = ET.SubElement(t, 'Name') xml_child(t, 'Name', i.name)
name.text = i.name
#<PersonResult> #<PersonResult>
runners_same_c = get_runners_in_class(runners, i) runners_same_c = get_runners_in_class(runners, i)
runners_ranked = rank_runners(runners_same_c, i) runners_ranked = rank_runners(runners_same_c, i)
@ -246,22 +248,17 @@ def gen_xml_result(runners, o_classes):
person_result = ET.SubElement(class_result, 'PersonResult') person_result = ET.SubElement(class_result, 'PersonResult')
#<Person> #<Person>
person = ET.SubElement(person_result, 'Person') person = ET.SubElement(person_result, 'Person')
eventorid = ET.SubElement(person, 'Id') xml_child(person, 'Id', '0')
eventorid.text = '0'
#<Name> #<Name>
name = ET.SubElement(person, 'Name') name = ET.SubElement(person, 'Name')
family = ET.SubElement(name, 'Family') xml_child(name, 'Family', n.last)
family.text = n.last xml_child(name, 'Given', n.first)
given = ET.SubElement(name, 'Given')
given.text = n.first
#</Name> #</Name>
#</Person> #</Person>
#<Organisation> #<Organisation>
org = ET.SubElement(person_result, 'Organisation') org = ET.SubElement(person_result, 'Organisation')
orgid = ET.SubElement(org, 'Id') xml_child(org, 'Id', '0')
orgid.text = '0' xml_child(org, 'Name', n.club)
name = ET.SubElement(org, 'Name')
name.text = n.club
country = ET.SubElement(org, 'Country') country = ET.SubElement(org, 'Country')
# TODO: hent land fra løperobjektet # TODO: hent land fra løperobjektet
country.text = 'Norway' country.text = 'Norway'
@ -270,35 +267,25 @@ def gen_xml_result(runners, o_classes):
#<Result> #<Result>
result = ET.SubElement(person_result, 'Result') result = ET.SubElement(person_result, 'Result')
if len(n.splits) > 2: if len(n.splits) > 2:
start_t = ET.SubElement(result, 'StartTime') xml_child(result, 'StartTime', n.s_time.isoformat())
start_t.text = n.s_time.isoformat() xml_child(result, 'FinishTime', n.f_time.isoformat())
finish_t = ET.SubElement(result, 'FinishTime') xml_child(result, 'Time', n.totaltime())
finish_t.text = n.f_time.isoformat()
time = ET.SubElement(result, 'Time')
time.text = str(n.totaltime())
if n.status() == 'OK': if n.status() == 'OK':
#<TimeBehind> #<TimeBehind>
totime = ET.SubElement(result, 'TimeBehind') xml_child(result, 'TimeBehind', n.totaltime() - runners_ranked[0].totaltime())
totime.text = str(n.totaltime() - runners_ranked[0].totaltime())
#</TimeBehind> #</TimeBehind>
pos = ET.SubElement(result, 'Position') xml_child(result, 'Position', n.rank(runners))
pos.text = str(n.rank(runners)) xml_child(result, 'Status', n.status())
status = ET.SubElement(result, 'Status')
status.text = n.status()
#<SplitTime> #<SplitTime>
for code,split in zip(n.o_class.course.codes, n.res_splits()): for code,split in zip(n.o_class.course.codes, n.res_splits()):
st = ET.SubElement(result, 'SplitTime') st = ET.SubElement(result, 'SplitTime')
ck = ET.SubElement(st, 'ControlCode') xml_child(st, 'ControlCode', code)
ck.text = str(code) xml_child(st, 'Time', split)
time = ET.SubElement(st, 'Time')
time.text = str(split)
#</SplitTime> #</SplitTime>
else: else:
status = ET.SubElement(result, 'Status') xml_child(result, 'Status', n.status())
status.text = n.status()
else: else:
status = ET.SubElement(result, 'Status') xml_child(result, 'Status', n.status())
status.text = n.status()
#</Result> #</Result>
#</PersonResult> #</PersonResult>
#</Class> #</Class>