Exemplo n.º 1
0
    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()))
Exemplo n.º 2
0
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)