Exemple #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()))
Exemple #2
0
 def post(self, schedule_version):
     if not self.SetSchedule(int(schedule_version)):
         return
     if self.request.get('source') == 'wca':
         # WCA import, pre-redirect
         if not self.GetToken(handler_data={
                 'data_to_import':
                 self.request.POST.getall('data_to_import'),
         }):
             return
         self.ImportWcifFromWca(self.request.POST.getall('data_to_import'))
     elif 'sched_' in self.request.get('source'):
         schedule_to_import = int(
             self.request.get('source')[len('sched_'):])
         wcif_to_import = CompetitionToWcif(
             self.competition, Schedule.get_by_id(schedule_to_import))
         self.ImportWcif(
             wcif_to_import,
             data_to_import=self.request.POST.getall('data_to_import'))
     elif self.request.get('source') == 'custom':
         result = urlfetch.fetch(self.request.get('custom_uri'))
         if result.status_code != 200:
             template = JINJA_ENVIRONMENT.get_template('error.html')
             self.response.write(
                 template.render({
                     'c':
                     common.Common(self),
                     'error':
                     'Error fetching %s' % self.request.get('custom_uri')
                 }))
             return
         self.ImportWcif(
             json.loads(result.content),
             data_to_import=self.request.POST.getall('data_to_import'))
Exemple #3
0
 def get(self, competition_id, schedule_version):
     if not self.SetSchedule(int(schedule_version)):
         return
     template = JINJA_ENVIRONMENT.get_template(
         'scheduling/edit_schedule.html')
     event_keys = set([
         r.event for r in ScheduleRound.query(
             ScheduleRound.schedule == self.schedule.key).iter()
     ])
     events = [
         e.get() for e in sorted(event_keys, key=lambda e: e.get().rank)
     ]
     stages = (ScheduleStage.query(
         ScheduleRound.schedule == self.schedule.key).order(
             ScheduleRound.number).fetch())
     schedule_versions = Schedule.query(
         Schedule.competition == self.competition.key).fetch()
     self.response.write(
         template.render({
             'c': common.Common(self),
             'competition': self.competition,
             'schedule': self.schedule,
             'events': events,
             'stages': stages,
             'new_stage_id': random.randint(2**4, 2**10),
             'colors': sorted(Colors.keys()),
             'schedule_versions': schedule_versions,
         }))
Exemple #4
0
 def SetSchedule(self, schedule_version):
     self.schedule = Schedule.get_by_id(schedule_version)
     if not self.schedule:
         self.RespondWithError('Unknown schedule version %s' %
                               schedule_version)
         return False
     return self.SetCompetition(self.schedule.competition.id())
Exemple #5
0
    def get(self, competition_id):
        if not self.SetCompetition(competition_id):
            return

        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()

        self.redirect(
            webapp2.uri_for('edit_schedule',
                            competition_id=competition_id,
                            schedule_version=schedule.key.id()))
Exemple #6
0
 def get(self, schedule_version, set_live):
     if not self.SetSchedule(int(schedule_version)):
         return
     self.schedule.is_live = (set_live == '1')
     self.schedule.last_update = datetime.datetime.now()
     if self.schedule.is_live:
         for schedule in Schedule.query(
                 ndb.AND(Schedule.competition == self.competition.key,
                         Schedule.is_live == True)).iter():
             schedule.is_live = False
             schedule.put()
     self.schedule.put()
     self.redirect(
         webapp2.uri_for('edit_schedule',
                         schedule_version=self.schedule.key.id()))
Exemple #7
0
    def __init__(self, user, competition, schedule=None):
        self.user = user
        self.competition = competition
        if schedule:
            self.schedule = schedule
        else:
            self.schedule = Schedule.query(
                ndb.AND(Schedule.competition == competition.key,
                        Schedule.is_live == True)).get()
        if not self.schedule:
            return

        self.events = {}
        self.event_keys = []
        self.stage_ids = set()
        for r in ScheduleRound.query(
                ScheduleRound.schedule == self.schedule.key).iter():
            if r.event.id() not in self.events:
                self.events[r.event.id()] = EventDetails(r.event)
                self.event_keys.append(r.event)
            self.events[r.event.id()].AddRound(r)

        for t in (ScheduleTimeBlock.query(
                ndb.AND(ScheduleTimeBlock.schedule == self.schedule.key,
                        ScheduleTimeBlock.staff_only == False)).order(
                            ScheduleTimeBlock.start_time).iter()):
            self.events[t.round.get().event.id()].AddTimeBlock(t)
            self.stage_ids.add(t.stage.id())
        if user and user.wca_person:
            self.ranks_single = {
                r.event.id(): r
                for r in RankSingle.query(
                    RankSingle.person == user.wca_person).iter()
            }
            self.ranks_average = {
                r.event.id(): r
                for r in RankAverage.query(
                    RankAverage.person == user.wca_person).iter()
            }
        else:
            self.ranks_single = {}
            self.ranks_average = {}

        self.has_qualifying_number = False
Exemple #8
0
 def get(self, competition_id, schedule_version=-1):
     if not self.SetCompetition(competition_id,
                                edit_access_needed=False,
                                login_required=False):
         return
     if schedule_version != -1:
         if not self.SetSchedule(int(schedule_version)):
             return
         schedule = self.schedule
     else:
         schedule = Schedule.query(
             ndb.AND(Schedule.competition == self.competition.key,
                     Schedule.is_live == True)).get()
     self.response.headers.add('Content-Type', 'application/json')
     json_object = CompetitionToWcif(self.competition, schedule)
     if self.request.get('pretty'):
         self.response.write(
             json.dumps(json_object, sort_keys=True, indent=2))
     else:
         self.response.write(json.dumps(json_object))
    def get(self, competition_id):
        # Unlike most scheduling handlers, it's okay here if the competition doesn't
        # exist, because that may just mean the user is creating the competition
        # here for the first time.  In this case redirect to /update.
        if not ScheduleCompetition.get_by_id(competition_id):
            self.redirect_to('update_competition',
                             competition_id=competition_id)
            return
        if not self.SetCompetition(competition_id):
            return
        timezones_and_times = [
            (timezone, datetime.datetime.now(
                pytz.timezone(timezone)).strftime('%I:%M %p'))
            for timezone in pytz.country_timezones('us')
        ]
        schedule_versions = Schedule.query(
            Schedule.competition == self.competition.key).fetch()

        # We look at the live schedule, or the most-recently updated one, and make
        # sure that it has events and start/end dates.
        schedule_for_staff_signup = None
        has_live_schedule = False
        if schedule_versions:
            for schedule in schedule_versions:
                if schedule.is_live:
                    schedule_for_staff_signup = schedule
                    has_live_schedule = True
            if not schedule_for_staff_signup:
                schedule_for_staff_signup = sorted(
                    schedule_versions, key=lambda s: s.last_update_time)[-1]
            has_rounds = (ScheduleRound.query(
                ScheduleRound.schedule ==
                schedule_for_staff_signup.key).iter().has_next())

        championships = Championship.query(Championship.competition == ndb.Key(
            Competition, competition_id)).fetch()
        if championships:
            if championships[0].residency_deadline:
                residency_deadline = timezones.ToLocalizedTime(
                    championships[0].residency_deadline,
                    self.competition.timezone or 'America/New_York')
            else:
                # If the residency deadline hasn't been set, use the day before the
                # competition starts.
                residency_deadline = (
                    datetime.datetime.combine(
                        championships[0].competition.get().start_date,
                        datetime.time(0, 0, 0)) -
                    datetime.timedelta(days=1)).replace(tzinfo=pytz.timezone(
                        self.competition.timezone or 'America/New_York'))
        else:
            residency_deadline = None

        template = JINJA_ENVIRONMENT.get_template(
            'scheduling/edit_competition.html')
        self.response.write(
            template.render({
                'c':
                common.Common(self),
                'competition':
                self.competition,
                'timezones_and_times':
                timezones_and_times,
                'schedule_versions':
                schedule_versions,
                'staff_signup_enabled':
                schedule_for_staff_signup
                and schedule_for_staff_signup.start_date and has_rounds,
                'has_live_schedule':
                has_live_schedule,
                'residency_deadline':
                residency_deadline
            }))