def from_json(comp_id: int, ref_id=None): """Reads competition from json result file takes com_id as argument""" from db.tables import TblResultFile as R with db_session() as db: if ref_id: file = db.query(R).get(ref_id).filename else: file = db.query(R.filename).filter(and_(R.comp_id == comp_id, R.task_id.is_(None), R.active == 1)).scalar() if file: comp = Comp(comp_id=comp_id) with open(path.join(RESULTDIR, file), 'r') as f: '''read task json file''' data = json.load(f) for k, v in data['info'].items(): # not using update to intercept changing in formats if hasattr(comp, k): setattr(comp, k, v) # comp.as_dict().update(data['info']) comp.stats = dict(**data['stats']) comp.rankings.update(data['rankings']) comp.tasks.extend(data['tasks']) comp.formula = Formula.from_dict(data['formula']) comp.data = dict(**data['file_stats']) results = [] for p in data['results']: '''get participants''' participant = Participant(comp_id=comp_id) participant.as_dict().update(p) results.append(participant) # should already be ordered, so probably not necessary comp.participants = sorted(results, key=lambda k: k.score, reverse=True) return comp
def create_participant_from_name(name): """get pilot info from pilot name database and create Participant obj It's almost sure that we get more than one result. This function gives back a Participant ONLY if we get a single result. get_pilots_from_name gives a list of Dict""" from pilot.participant import Participant body = f"""<?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <FindPilot xmlns="http://civlrankings.fai.org/"> <name>{name}</name> </FindPilot> </soap12:Body> </soap12:Envelope>""" response = requests.post(url, data=body, headers=headers) # print(response.content) root = ET.fromstring(response.content) data = root.find('.//{http://civlrankings.fai.org/}FindPilotResponse') if not len(data.findall( '{http://civlrankings.fai.org/}FindPilotResult/')) == 1: '''we don't have a single result''' return None result = data.findall('{http://civlrankings.fai.org/}FindPilotResult//') pilot = Participant() pilot.name = result[1].text pilot.nat = result[2].text pilot.civl_id = int(result[6].text) pilot.sex = 'F' if result[7].text == 'true' else 'M' pilot.team = result[8].text pilot.glider = result[9].text return pilot
def read(cls, fp, short_name=None, keep_task_path=False, from_CIVL=False): """ A XML reader to read FSDB files Unfortunately the fsdb format isn't published so much of this is simply an exercise in reverse engineering. Input: - fp: STR: filepath - from_CIVL: BOOL: look for pilot on CIVL database """ """read the fsdb file""" try: tree = ET.parse(fp) root = tree.getroot() except ET.Error: print("FSDB Read Error.") return None pilots = [] tasks = [] """Comp Info""" print("Getting Comp Info...") fs_comp = root.find('FsCompetition') comp = Comp.from_fsdb(fs_comp, short_name) """Formula""" comp.formula = Formula.from_fsdb(fs_comp) comp.formula.comp_class = comp.comp_class """Pilots""" print("Getting Pilots Info...") if from_CIVL: print('*** get from CIVL database') p = root.find('FsCompetition').find('FsParticipants') for pil in p.iter('FsParticipant'): pilot = Participant.from_fsdb(pil, from_CIVL=from_CIVL) # pp(pilot.as_dict()) pilots.append(pilot) comp.participants = pilots """Tasks""" print("Getting Tasks Info...") t = root.find('FsCompetition').find('FsTasks') for tas in t.iter('FsTask'): '''create task obj''' task = Task.from_fsdb(tas, comp.time_offset, keep_task_path) '''check if task was valid''' if task is not None: if not task.task_path: task.create_path() # task.time_offset = int(comp.time_offset) """Task Results""" node = tas.find('FsParticipants') if node is not None: task.pilots = [] print("Getting Results Info...") for res in node.iter('FsParticipant'): '''pilots results''' pilot = FlightResult.from_fsdb(res, task) task.pilots.append(pilot) tasks.append(task) return cls(comp, tasks, fp)
def create_participant_from_CIVLID(civl_id): """get pilot info from CIVL database and create Participant obj""" from pilot.participant import Participant body = f"""<?xml version='1.0' encoding='utf-8'?> <soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'> <soap12:Body> <GetPilot xmlns='http://civlrankings.fai.org/'> <CIVLID>{civl_id}</CIVLID> </GetPilot> </soap12:Body> </soap12:Envelope>""" response = requests.post(url, data=body, headers=headers) # print(response.content) root = ET.fromstring(response.content) data = root.find('.//{http://civlrankings.fai.org/}GetPilotResponse') if not data or not data.find( '{http://civlrankings.fai.org/}GetPilotResult'): '''we don't have a result''' return None result = data.findall('{http://civlrankings.fai.org/}GetPilotResult//') pilot = Participant(civl_id=civl_id) pilot.name = result[0].text pilot.nat = result[1].text pilot.sex = 'F' if result[6].text == 'true' else 'M' pilot.team = result[7].text pilot.glider = result[8].text return pilot
def get_participants(comp_id: int): """gets registered pilots list from database""" from db.tables import TblParticipant as R from pilot.participant import Participant pilots = [] with db_session() as db: results = db.query(R).filter_by(comp_id=comp_id).all() for p in results: pil = Participant(comp_id=comp_id) p.populate(pil) pilots.append(pil) return pilots
def _get_participants_and_status(compid): from pilot.participant import Participant pilot_list, _, _ = frontendUtils.get_participants(compid) status = None participant_info = None if current_user: if not current_user.is_authenticated: status = None elif any(p for p in pilot_list if p['pil_id'] == current_user.id): participant_info = next(p for p in pilot_list if p['pil_id'] == current_user.id) status = 'registered' else: participant = Participant.from_profile(current_user.id) participant_info = participant.as_dict() status = 'not_registered' return {'data': pilot_list, 'status': status, 'pilot_details': participant_info}