def get(self): OAuthBaseHandler.get(self) if not self.auth_token: return competition_id = self.handler_data if not self.SetCompetition(competition_id): return response = self.GetWcaApi('/api/v0/competitions/%s/wcif' % competition_id) if response.status != 200: self.redirect(webapp2.uri_for('index', unknown=1)) return response_json = json.loads(response.read()) objects_to_put = [] schedule = Schedule() schedule.competition = self.competition.key schedule.creation_time = datetime.datetime.now() schedule.last_update_time = schedule.creation_time schedule.is_live = False schedule.put() for event in response_json['events']: event_key = ndb.Key(Event, event['id']) round_num = 0 next_round_count = 0 for round_json in event['rounds']: round_num += 1 round_object = ScheduleRound(id=ScheduleRound.Id( schedule.key.id(), event['id'], round_num)) round_object.schedule = schedule.key round_object.event = event_key round_object.number = round_num round_object.is_final = len(event['rounds']) == round_num round_object.format = ndb.Key(Format, round_json['format']) if round_json['cutoff']: round_object.cutoff = round_json['cutoff']['attemptResult'] if round_json['timeLimit'] and round_json['timeLimit'][ 'centiseconds']: round_object.time_limit = round_json['timeLimit'][ 'centiseconds'] round_object.wcif = json.dumps(round_json) if next_round_count: round_object.num_competitors = next_round_count objects_to_put.append(round_object) advancement_condition = round_json['advancementCondition'] if advancement_condition and advancement_condition[ 'type'] == 'ranking': next_round_count = advancement_condition['level'] else: next_round_count = 0 ndb.put_multi(objects_to_put) self.redirect( webapp2.uri_for('edit_schedule', schedule_version=schedule.key.id()))
def ImportTimeBlock(activity_data, schedule, stage, out, time_blocks, groups): if 'id' not in activity_data: out.errors.append('activityCode missing from Activity.') return if 'activityCode' not in activity_data: out.errors.append('activityCode missing from Activity %d.' % activity_data['id']) return try: activity_code = ActivityCode.ParseCode(activity_data['activityCode']) except Exception as e: out.errors.append(e.message) return if activity_code.other_string: # TODO: support non-event Activities. return if not activity_code.event_id: out.errors.append('Missing event ID for activity %s' % activity_data['activityCode']) return if not activity_code.round_id: # We ignore Activities that are not specific to a round. return if 'startTime' not in activity_data: out.errors.append('Missing startTime for activity %s' % activity_data['activityCode']) return if 'endTime' not in activity_data: out.errors.append('Missing endTime for activity %s' % activity_data['activityCode']) return extension = GetExtension('ScheduleTimeBlock', activity_data) round_id = ScheduleRound.Id(schedule.key.id(), activity_code.event_id, activity_code.round_id) time_block_id = '%s_%d' % (round_id, activity_data['id']) if time_block_id in time_blocks: time_block = time_blocks.pop(time_block_id) else: time_block = ScheduleTimeBlock(id=time_block_id) time_block.schedule = schedule.key time_block.round = ndb.Key(ScheduleRound, round_id) time_block.stage = stage.key if activity_code.attempt: time_block.attempt = activity_code.attempt time_block.start_time = timezones.StripTimezone( dateutil.parser.parse(activity_data['startTime'])) time_block.end_time = timezones.StripTimezone( dateutil.parser.parse(activity_data['endTime'])) if 'staffOnly' in extension: time_block.staff_only = extension['staffOnly'] if 'childActivities' in activity_data: for child_activity in activity_data['childActivities']: ImportData(child_activity, time_block, out, groups) out.entities_to_put.append(time_block)
def ImportEvents(wcif_data, schedule_key, out): existing_rounds = { e.key.id(): e for e in ScheduleRound.query( ScheduleRound.schedule == schedule_key).iter() } all_event_ids = set([e.key.id() for e in Event.query().iter()]) all_formats = set([f.key.id() for f in Format.query().iter()]) for event in wcif_data['events']: if 'id' not in event: out.errors.append( 'Malformed WCIF: missing \'id\' field for event.') continue if event['id'] not in all_event_ids: out.errors.append('Unrecognized event ID \'%s\'' % event['id']) continue event_key = ndb.Key(Event, event['id']) round_num = 0 next_round_count = 0 for round_json in event['rounds']: round_num += 1 round_id = ScheduleRound.Id(schedule_key.id(), event['id'], round_num) if round_id in existing_rounds: round_object = existing_rounds.pop(round_id) else: round_object = ScheduleRound(id=round_id) round_object.schedule = schedule_key round_object.event = event_key round_object.number = round_num round_object.is_final = len(event['rounds']) == round_num if 'format' not in round_json: out.errors.append( 'Malformed WCIF: missing \'format\' field for %s' % round_id) continue if round_json['format'] not in all_formats: out.errors.append('Unrecognized format ID \'%s\'' % round_json['format']) continue round_object.format = ndb.Key(Format, round_json['format']) if 'cutoff' in round_json and round_json['cutoff']: round_object.cutoff = round_json['cutoff']['attemptResult'] if ('timeLimit' in round_json and round_json['timeLimit'] and 'centiseconds' in round_json['timeLimit'] and round_json['timeLimit']['centiseconds']): round_object.time_limit = round_json['timeLimit'][ 'centiseconds'] round_object.wcif = json.dumps(round_json) if next_round_count: round_object.num_competitors = next_round_count next_round_count = 0 if 'advancementCondition' in round_json: advancement_condition = round_json['advancementCondition'] if (advancement_condition and 'type' in advancement_condition and advancement_condition['type'] == 'ranking'): next_round_count = advancement_condition['level'] out.entities_to_put.append(round_object) out.entities_to_delete.extend([r for r in existing_rounds.itervalues()]) # Also look for time blocks and groups that are now unused. for obj_class in (ScheduleTimeBlock, ScheduleGroup): for obj in obj_class.query(obj_class.schedule == schedule_key).iter(): if obj.round.id() in existing_rounds: out.entities_to_delete.append(obj)