Skip to content

Commit 882d53e

Browse files
feat: read comments and course variations from IOF XML (#530)
1 parent 17ec0be commit 882d53e

File tree

2 files changed

+54
-27
lines changed

2 files changed

+54
-27
lines changed

sportorg/libs/iof/parser.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def variation_data(tree, ns):
9898
root = tree.getroot()
9999
if "CourseData" not in root.tag:
100100
return
101-
teams = []
101+
course_assignments = []
102102

103103
version = "0"
104104
if "iofVersion" in root.attrib:
@@ -108,22 +108,28 @@ def variation_data(tree, ns):
108108

109109
if version == "3":
110110
# TeamCourseAssignment - variations for relays
111-
for team_el in root.find("iof:RaceCourseData", ns).findall(
112-
"iof:TeamCourseAssignment", ns
113-
):
111+
for team_el in root.find("iof:RaceCourseData", ns).findall("iof:TeamCourseAssignment", ns):
114112
team = {
115113
"bib_number": int(team_el.find("iof:BibNumber", ns).text),
116-
"legs": [],
114+
"legs": []
117115
}
118116
for leg_el in team_el.findall("iof:TeamMemberCourseAssignment", ns):
119117
leg = {
120118
"leg_number": leg_el.find("iof:Leg", ns).text,
121-
"course_name": leg_el.find("iof:CourseName", ns).text,
119+
"course_name": leg_el.find("iof:CourseName", ns).text
122120
}
123121
team["legs"].append(leg)
124-
teams.append(team)
122+
course_assignments.append(team)
125123

126-
return teams
124+
for person_el in root.find("iof:RaceCourseData", ns).findall("iof:PersonCourseAssignment", ns):
125+
course_assignment = {
126+
"bib_number": int(person_el.find("iof:BibNumber", ns).text),
127+
"course_name": person_el.find("iof:CourseName", ns).text,
128+
"legs": []
129+
}
130+
course_assignments.append(course_assignment)
131+
132+
return course_assignments
127133

128134

129135
def entry_list(tree, ns):
@@ -164,6 +170,12 @@ def entry_list(tree, ns):
164170
if bib_el is not None:
165171
person["extensions"]["bib"] = bib_el.text
166172

173+
service_request_el = person_entry_el.find("iof:ServiceRequest", ns)
174+
if service_request_el is not None:
175+
comment_el = service_request_el.find("iof:Comment", ns)
176+
if comment_el is not None:
177+
person["comment"] = comment_el.text
178+
167179
org_el = person_entry_el.find("iof:Organisation", ns)
168180
organization = None
169181
if org_el:

sportorg/modules/iof/iof_xml.py

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,39 +111,51 @@ def import_from_course_data(courses) -> None:
111111
obj.courses.append(c)
112112

113113

114-
def import_from_variation_data(teams) -> None:
114+
def import_from_variation_data(course_assignments) -> None:
115115
obj = race()
116-
for team in teams:
117-
for leg in team["legs"]:
118-
leg_number = leg["leg_number"]
119-
course_name = leg["course_name"]
116+
for course_assignment in course_assignments:
117+
if course_assignment["legs"] and len(course_assignment["legs"]) > 0:
118+
# relay
119+
for leg in course_assignment["legs"]:
120+
leg_number = leg["leg_number"]
121+
course_name = leg["course_name"]
122+
course = find(obj.courses, name=course_name)
123+
if course:
124+
new_course = Course()
125+
new_course.name = str(course_assignment["bib_number"]) + "." + str(leg_number)
126+
new_course.length = course.length
127+
new_course.controls = copy.deepcopy(course.controls)
128+
obj.courses.append(new_course)
129+
130+
elif course_assignment["course_name"] is not None:
131+
# one man relay
132+
course_name = course_assignment["course_name"]
120133
course = find(obj.courses, name=course_name)
121134
if course:
122135
new_course = Course()
123-
new_course.name = str(team["bib_number"]) + "." + str(leg_number)
136+
new_course.name = str(course_assignment["bib_number"])
124137
new_course.length = course.length
125138
new_course.controls = copy.deepcopy(course.controls)
126139
obj.courses.append(new_course)
127140

128-
129141
def create_person(person_entry):
130142
obj = race()
131143

132144
person = Person()
133145

134146
name = person_entry["group"]["name"]
135147
if (
136-
"short_name" in person_entry["group"]
137-
and len(person_entry["group"]["short_name"]) > 0
148+
"short_name" in person_entry["group"]
149+
and len(person_entry["group"]["short_name"]) > 0
138150
):
139151
name = person_entry["group"]["short_name"]
140152
group = find(obj.groups, name=name)
141153
if group is None:
142154
group = Group()
143155
group.long_name = person_entry["group"]["name"]
144156
if (
145-
"short_name" in person_entry["group"]
146-
and len(person_entry["group"]["short_name"]) > 0
157+
"short_name" in person_entry["group"]
158+
and len(person_entry["group"]["short_name"]) > 0
147159
):
148160
group.name = person_entry["group"]["short_name"]
149161
else:
@@ -180,19 +192,22 @@ def create_person(person_entry):
180192
if "bib" in person_entry["person"] and person_entry["person"]["bib"]:
181193
person.set_bib(int(person_entry["person"]["bib"]))
182194
elif (
183-
"bib" in person_entry["person"]["extensions"]
184-
and person_entry["person"]["extensions"]["bib"]
195+
"bib" in person_entry["person"]["extensions"]
196+
and person_entry["person"]["extensions"]["bib"]
185197
):
186198
person.set_bib(int(person_entry["person"]["extensions"]["bib"]))
187199
if (
188-
"qual" in person_entry["person"]["extensions"]
189-
and person_entry["person"]["extensions"]["qual"]
200+
"qual" in person_entry["person"]["extensions"]
201+
and person_entry["person"]["extensions"]["qual"]
190202
):
191203
person.qual = Qualification.get_qual_by_name(
192204
person_entry["person"]["extensions"]["qual"]
193205
)
194206
if "start" in person_entry["person"] and person_entry["person"]["start"]:
195207
person.start_time = time_iof_to_otime(person_entry["person"]["start"])
208+
if "comment" in person_entry["person"]:
209+
person.comment = person_entry["person"]["comment"]
210+
196211

197212
obj.persons.append(person)
198213
return person
@@ -306,10 +321,10 @@ def import_from_event_data(data) -> None:
306321
if "name" in data:
307322
new_name = data["name"]
308323
if (
309-
new_name
310-
and len(new_name) > 0
311-
and new_name != "Event"
312-
and new_name.find("Example") < 0
324+
new_name
325+
and len(new_name) > 0
326+
and new_name != "Event"
327+
and new_name.find("Example") < 0
313328
):
314329
obj.data.title = data["name"]
315330

0 commit comments

Comments
 (0)