def get(self): self._require_login() self._require_registration() user = self.user_bundle.account.key now = datetime.datetime.now() team_favorites_future = Favorite.query(Favorite.model_type == ModelType.TEAM, ancestor=user).fetch_async() live_events = EventHelper.getEventsWithinADay() favorite_team_keys = map(lambda f: ndb.Key(Team, f.model_key), team_favorites_future.get_result()) favorite_teams_future = ndb.get_multi_async(favorite_team_keys) live_eventteams_futures = [] for event in live_events: live_eventteams_futures.append(EventTeamsQuery(event.key_name).fetch_async()) favorite_teams = [team_future.get_result() for team_future in favorite_teams_future] favorite_teams_events_futures = [] for team in favorite_teams: favorite_teams_events_futures.append(TeamYearEventsQuery(team.key_name, now.year).fetch_async()) live_events_with_teams = EventTeamStatusHelper.buildEventTeamStatus(live_events, live_eventteams_futures, favorite_teams) future_events_by_event = {} for team, events_future in zip(favorite_teams, favorite_teams_events_futures): events = events_future.get_result() if not events: continue EventHelper.sort_events(events) next_event = next((e for e in events if e.start_date > now and not e.within_a_day), None) if next_event: if next_event.key_name not in future_events_by_event: future_events_by_event[next_event.key_name] = (next_event, []) future_events_by_event[next_event.key_name][1].append(team) future_events_with_teams = [] for event_key, data in future_events_by_event.iteritems(): future_events_with_teams.append((data[0], TeamHelper.sortTeams(data[1]))) future_events_with_teams.sort(key=lambda x: x[0].name) future_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) future_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) self.template_values.update({ 'live_events_with_teams': live_events_with_teams, 'future_events_with_teams': future_events_with_teams, }) path = os.path.join(os.path.dirname(__file__), '../templates/mytba_live.html') self.response.out.write(template.render(path, self.template_values))
def _render(self, district_abbrev, year=None, explicit_year=False): district = DistrictQuery('{}{}'.format(year, district_abbrev)).fetch() if not district: self.abort(404) events_future = DistrictEventsQuery(district.key_name).fetch_async() # needed for district teams district_teams_future = DistrictTeamsQuery(district.key_name).fetch_async() # needed for valid_years history_future = DistrictHistoryQuery(district.abbreviation).fetch_async() # needed for valid_districts districts_in_year_future = DistrictsInYearQuery(district.year).fetch_async() # needed for active team statuses live_events = [] if year == datetime.datetime.now().year: # Only show active teams for current year live_events = EventHelper.getWeekEvents() live_eventteams_futures = [] for event in live_events: live_eventteams_futures.append(EventTeamsQuery(event.key_name).fetch_async()) events = events_future.get_result() EventHelper.sort_events(events) events_by_key = {} for event in events: events_by_key[event.key.id()] = event week_events = EventHelper.groupByWeek(events) valid_districts = set() districts_in_year = districts_in_year_future.get_result() for dist in districts_in_year: valid_districts.add((dist.display_name, dist.abbreviation)) valid_districts = sorted(valid_districts, key=lambda (name, _): name) teams = TeamHelper.sortTeams(district_teams_future.get_result()) team_keys = set([t.key.id() for t in teams]) num_teams = len(teams) middle_value = num_teams / 2 if num_teams % 2 != 0: middle_value += 1 teams_a, teams_b = teams[:middle_value], teams[middle_value:] # Currently Competing Team Status event_team_keys = [] for event, teams_future in zip(live_events, live_eventteams_futures): for team in teams_future.get_result(): if team.key.id() in team_keys: event_team_keys.append(ndb.Key(EventTeam, '{}_{}'.format(event.key.id(), team.key.id()))) # Should be in context cache ndb.get_multi(event_team_keys) # Warms context cache live_events_with_teams = [] for event, teams_future in zip(live_events, live_eventteams_futures): teams_and_statuses = [] has_teams = False for team in teams_future.get_result(): if team.key.id() in team_keys: has_teams = True event_team = EventTeam.get_by_id('{}_{}'.format(event.key.id(), team.key.id())) # Should be in context cache status_str = { 'alliance': EventTeamStatusHelper.generate_team_at_event_alliance_status_string(team.key.id(), event_team.status), 'playoff': EventTeamStatusHelper.generate_team_at_event_playoff_status_string(team.key.id(), event_team.status), } teams_and_statuses.append(( team, event_team.status, status_str )) if has_teams: teams_and_statuses.sort(key=lambda x: x[0].team_number) live_events_with_teams.append((event, teams_and_statuses)) live_events_with_teams.sort(key=lambda x: x[0].name) live_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) live_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) # Get valid years district_history = history_future.get_result() valid_years = map(lambda d: d.year, district_history) valid_years = sorted(valid_years) self.template_values.update({ 'explicit_year': explicit_year, 'year': year, 'valid_years': valid_years, 'valid_districts': valid_districts, 'district_name': district.display_name, 'district_abbrev': district_abbrev, 'week_events': week_events, 'events_by_key': events_by_key, 'rankings': district.rankings, 'advancement': district.advancement, 'teams_a': teams_a, 'teams_b': teams_b, 'live_events_with_teams': live_events_with_teams, }) return jinja2_engine.render('district_details.html', self.template_values)
def get(self): self._require_registration() user = self.user_bundle.account.key now = datetime.datetime.now() team_favorites_future = Favorite.query(Favorite.model_type == ModelType.TEAM, ancestor=user).fetch_async() favorite_team_keys = map(lambda f: ndb.Key(Team, f.model_key), team_favorites_future.get_result()) favorite_teams_future = ndb.get_multi_async(favorite_team_keys) favorite_teams = [team_future.get_result() for team_future in favorite_teams_future] favorite_teams_events_futures = [] for team in favorite_teams: favorite_teams_events_futures.append(TeamYearEventsQuery(team.key_name, now.year).fetch_async()) past_events_by_event = {} live_events_by_event = {} future_events_by_event = {} for team, events_future in zip(favorite_teams, favorite_teams_events_futures): events = events_future.get_result() if not events: continue EventHelper.sort_events(events) # Sort by date for event in events: if event.within_a_day: if event.key_name not in live_events_by_event: live_events_by_event[event.key_name] = (event, []) live_events_by_event[event.key_name][1].append(team) elif event.start_date < now: if event.key_name not in past_events_by_event: past_events_by_event[event.key_name] = (event, []) past_events_by_event[event.key_name][1].append(team) else: if event.key_name not in future_events_by_event: future_events_by_event[event.key_name] = (event, []) future_events_by_event[event.key_name][1].append(team) past_events = [] past_eventteams = [] for past_event, past_eventteam in past_events_by_event.itervalues(): past_events.append(past_event) past_eventteams.append(past_eventteam) past_events_with_teams = EventTeamStatusHelper.buildEventTeamStatus(past_events, past_eventteams, favorite_teams) past_events_with_teams.sort(key=lambda x: x[0].name) past_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) past_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) live_events = [] live_eventteams = [] for live_event, live_eventteam in live_events_by_event.itervalues(): live_events.append(live_event) live_eventteams.append(live_eventteam) live_events_with_teams = EventTeamStatusHelper.buildEventTeamStatus(live_events, live_eventteams, favorite_teams) live_events_with_teams.sort(key=lambda x: x[0].name) future_events_with_teams = [] for event_key, data in future_events_by_event.iteritems(): future_events_with_teams.append((data[0], TeamHelper.sortTeams(data[1]))) future_events_with_teams.sort(key=lambda x: x[0].name) future_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) future_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) # Resolve future before rendering for _, teams_and_statuses_future in past_events_with_teams: for team_and_status_future in teams_and_statuses_future: team_and_status_future[1] = team_and_status_future[1].get_result() for _, teams_and_statuses_future in live_events_with_teams: for team_and_status_future in teams_and_statuses_future: team_and_status_future[1] = team_and_status_future[1].get_result() self.template_values.update({ 'past_events_with_teams': past_events_with_teams, 'live_events_with_teams': live_events_with_teams, 'future_events_with_teams': future_events_with_teams, }) path = os.path.join(os.path.dirname(__file__), '../templates/mytba_live.html') self.response.out.write(template.render(path, self.template_values))
def get(self): self._require_login() self._require_registration() user = self.user_bundle.account.key favorites = Favorite.query(ancestor=user).fetch() subscriptions = Subscription.query(ancestor=user).fetch() team_keys = set() team_fav = {} team_subs = {} event_keys = set() event_fav = {} event_subs = {} events = [] match_keys = set() match_event_keys = set() match_fav = {} match_subs = {} for item in favorites + subscriptions: if item.model_type == ModelType.TEAM: team_keys.add(ndb.Key(Team, item.model_key)) if type(item) == Favorite: team_fav[item.model_key] = item elif type(item) == Subscription: team_subs[item.model_key] = item elif item.model_type == ModelType.MATCH: match_keys.add(ndb.Key(Match, item.model_key)) match_event_keys.add(ndb.Key(Event, item.model_key.split('_')[0])) if type(item) == Favorite: match_fav[item.model_key] = item elif type(item) == Subscription: match_subs[item.model_key] = item elif item.model_type == ModelType.EVENT: if item.model_key.endswith('*'): # All year events wildcard event_year = int(item.model_key[:-1]) events.append(Event( # add fake event for rendering id=item.model_key, short_name='ALL EVENTS', event_short=item.model_key, year=event_year, start_date=datetime.datetime(event_year, 1, 1), end_date=datetime.datetime(event_year, 1, 1) )) else: event_keys.add(ndb.Key(Event, item.model_key)) if type(item) == Favorite: event_fav[item.model_key] = item elif type(item) == Subscription: event_subs[item.model_key] = item team_futures = ndb.get_multi_async(team_keys) event_futures = ndb.get_multi_async(event_keys) match_futures = ndb.get_multi_async(match_keys) match_event_futures = ndb.get_multi_async(match_event_keys) teams = sorted([team_future.get_result() for team_future in team_futures], key=lambda x: x.team_number) team_fav_subs = [] for team in teams: fav = team_fav.get(team.key.id(), None) subs = team_subs.get(team.key.id(), None) team_fav_subs.append((team, fav, subs)) events += [event_future.get_result() for event_future in event_futures] EventHelper.sort_events(events) event_fav_subs = [] for event in events: fav = event_fav.get(event.key.id(), None) subs = event_subs.get(event.key.id(), None) event_fav_subs.append((event, fav, subs)) matches = [match_future.get_result() for match_future in match_futures] match_events = [match_event_future.get_result() for match_event_future in match_event_futures] MatchHelper.natural_sort_matches(matches) match_fav_subs_by_event = {} for event in match_events: match_fav_subs_by_event[event.key.id()] = (event, []) for match in matches: event_key = match.key.id().split('_')[0] fav = match_fav.get(match.key.id(), None) subs = match_subs.get(match.key.id(), None) match_fav_subs_by_event[event_key][1].append((match, fav, subs)) event_match_fav_subs = sorted(match_fav_subs_by_event.values(), key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) event_match_fav_subs = sorted(event_match_fav_subs, key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) self.template_values['team_fav_subs'] = team_fav_subs self.template_values['event_fav_subs'] = event_fav_subs self.template_values['event_match_fav_subs'] = event_match_fav_subs self.template_values['status'] = self.request.get('status') self.template_values['year'] = datetime.datetime.now().year self.response.out.write(jinja2_engine.render('mytba.html', self.template_values))
def get(self): self._require_registration() user = self.user_bundle.account.key now = datetime.datetime.now() team_favorites_future = Favorite.query( Favorite.model_type == ModelType.TEAM, ancestor=user).fetch_async() favorite_team_keys = map(lambda f: ndb.Key(Team, f.model_key), team_favorites_future.get_result()) favorite_teams_future = ndb.get_multi_async(favorite_team_keys) favorite_teams = [ team_future.get_result() for team_future in favorite_teams_future ] favorite_teams_events_futures = [] for team in favorite_teams: favorite_teams_events_futures.append( TeamYearEventsQuery(team.key_name, now.year).fetch_async()) past_events_by_event = {} live_events_by_event = {} future_events_by_event = {} for team, events_future in zip(favorite_teams, favorite_teams_events_futures): events = events_future.get_result() if not events: continue EventHelper.sort_events(events) # Sort by date for event in events: if event.within_a_day: if event.key_name not in live_events_by_event: live_events_by_event[event.key_name] = (event, []) live_events_by_event[event.key_name][1].append(team) elif event.start_date < now: if event.key_name not in past_events_by_event: past_events_by_event[event.key_name] = (event, []) past_events_by_event[event.key_name][1].append(team) else: if event.key_name not in future_events_by_event: future_events_by_event[event.key_name] = (event, []) future_events_by_event[event.key_name][1].append(team) break # Only find one next event for each team past_events = [] past_eventteams = [] for past_event, past_eventteam in past_events_by_event.itervalues(): past_events.append(past_event) past_eventteams.append(past_eventteam) past_events_with_teams = EventTeamStatusHelper.buildEventTeamStatus( past_events, past_eventteams, favorite_teams) past_events_with_teams.sort(key=lambda x: x[0].name) past_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) past_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) live_events = [] live_eventteams = [] for live_event, live_eventteam in live_events_by_event.itervalues(): live_events.append(live_event) live_eventteams.append(live_eventteam) live_events_with_teams = EventTeamStatusHelper.buildEventTeamStatus( live_events, live_eventteams, favorite_teams) live_events_with_teams.sort(key=lambda x: x[0].name) future_events_with_teams = [] for event_key, data in future_events_by_event.iteritems(): future_events_with_teams.append( (data[0], TeamHelper.sortTeams(data[1]))) future_events_with_teams.sort(key=lambda x: x[0].name) future_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) future_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) # Resolve future before rendering for _, teams_and_statuses_future in past_events_with_teams: for team_and_status_future in teams_and_statuses_future: team_and_status_future[1] = team_and_status_future[ 1].get_result() for _, teams_and_statuses_future in live_events_with_teams: for team_and_status_future in teams_and_statuses_future: team_and_status_future[1] = team_and_status_future[ 1].get_result() self.template_values.update({ 'past_events_with_teams': past_events_with_teams, 'live_events_with_teams': live_events_with_teams, 'future_events_with_teams': future_events_with_teams, }) path = os.path.join(os.path.dirname(__file__), '../templates/mytba_live.html') self.response.out.write(template.render(path, self.template_values))
def get(self): self._require_registration() user = self.user_bundle.account.key favorites = Favorite.query(ancestor=user).fetch() subscriptions = Subscription.query(ancestor=user).fetch() team_keys = set() team_fav = {} team_subs = {} event_keys = set() event_fav = {} event_subs = {} events = [] match_keys = set() match_event_keys = set() match_fav = {} match_subs = {} for item in favorites + subscriptions: if item.model_type == ModelType.TEAM: team_keys.add(ndb.Key(Team, item.model_key)) if type(item) == Favorite: team_fav[item.model_key] = item elif type(item) == Subscription: team_subs[item.model_key] = item elif item.model_type == ModelType.MATCH: match_keys.add(ndb.Key(Match, item.model_key)) match_event_keys.add( ndb.Key(Event, item.model_key.split('_')[0])) if type(item) == Favorite: match_fav[item.model_key] = item elif type(item) == Subscription: match_subs[item.model_key] = item elif item.model_type == ModelType.EVENT: if item.model_key.endswith('*'): # All year events wildcard event_year = int(item.model_key[:-1]) events.append( Event( # add fake event for rendering id=item.model_key, short_name='ALL EVENTS', event_short=item.model_key, year=event_year, start_date=datetime.datetime(event_year, 1, 1), end_date=datetime.datetime(event_year, 1, 1))) else: event_keys.add(ndb.Key(Event, item.model_key)) if type(item) == Favorite: event_fav[item.model_key] = item elif type(item) == Subscription: event_subs[item.model_key] = item team_futures = ndb.get_multi_async(team_keys) event_futures = ndb.get_multi_async(event_keys) match_futures = ndb.get_multi_async(match_keys) match_event_futures = ndb.get_multi_async(match_event_keys) teams = sorted( [team_future.get_result() for team_future in team_futures], key=lambda x: x.team_number) team_fav_subs = [] for team in teams: fav = team_fav.get(team.key.id(), None) subs = team_subs.get(team.key.id(), None) team_fav_subs.append((team, fav, subs)) events += [event_future.get_result() for event_future in event_futures] EventHelper.sort_events(events) event_fav_subs = [] for event in events: fav = event_fav.get(event.key.id(), None) subs = event_subs.get(event.key.id(), None) event_fav_subs.append((event, fav, subs)) matches = [match_future.get_result() for match_future in match_futures] match_events = [ match_event_future.get_result() for match_event_future in match_event_futures ] MatchHelper.natural_sort_matches(matches) match_fav_subs_by_event = {} for event in match_events: match_fav_subs_by_event[event.key.id()] = (event, []) for match in matches: event_key = match.key.id().split('_')[0] fav = match_fav.get(match.key.id(), None) subs = match_subs.get(match.key.id(), None) match_fav_subs_by_event[event_key][1].append((match, fav, subs)) event_match_fav_subs = sorted( match_fav_subs_by_event.values(), key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) event_match_fav_subs = sorted( event_match_fav_subs, key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) self.template_values['team_fav_subs'] = team_fav_subs self.template_values['event_fav_subs'] = event_fav_subs self.template_values['event_match_fav_subs'] = event_match_fav_subs self.template_values['status'] = self.request.get('status') self.template_values['year'] = datetime.datetime.now().year self.response.out.write( jinja2_engine.render('mytba.html', self.template_values))
def get(self): self._require_registration() user = self.user_bundle.account.key now = datetime.datetime.now() year = self.request.get('year') if year and year.isdigit(): year = int(year) else: year = now.year team_favorites_future = Favorite.query(Favorite.model_type == ModelType.TEAM, ancestor=user).fetch_async() favorite_team_keys = map(lambda f: ndb.Key(Team, f.model_key), team_favorites_future.get_result()) favorite_teams_future = ndb.get_multi_async(favorite_team_keys) favorite_teams = [team_future.get_result() for team_future in favorite_teams_future] favorite_teams_events_futures = [] favorite_teams_awards_futures = {} for team in favorite_teams: favorite_teams_events_futures.append(TeamYearEventsQuery(team.key_name, year).fetch_async()) favorite_teams_awards_futures[team.key.id()] = TeamYearAwardsQuery(team.key_name, year).fetch_async() past_events_by_event = {} live_events_by_event = {} future_events_by_event = {} favorite_event_team_keys = [] for team, events_future in zip(favorite_teams, favorite_teams_events_futures): events = events_future.get_result() if not events: continue EventHelper.sort_events(events) # Sort by date for event in events: favorite_event_team_keys.append(ndb.Key(EventTeam, '{}_{}'.format(event.key.id(), team.key.id()))) if event.within_a_day: if event.key_name not in live_events_by_event: live_events_by_event[event.key_name] = (event, []) live_events_by_event[event.key_name][1].append(team) elif event.start_date < now: if event.key_name not in past_events_by_event: past_events_by_event[event.key_name] = (event, []) past_events_by_event[event.key_name][1].append(team) else: if event.key_name not in future_events_by_event: future_events_by_event[event.key_name] = (event, []) future_events_by_event[event.key_name][1].append(team) event_team_awards = defaultdict(lambda: defaultdict(list)) for team_key, awards_future in favorite_teams_awards_futures.items(): for award in awards_future.get_result(): event_team_awards[award.event.id()][team_key].append(award) ndb.get_multi(favorite_event_team_keys) # Warms context cache past_events_with_teams = [] for event, teams in past_events_by_event.itervalues(): teams_and_statuses = [] for team in teams: event_team = EventTeam.get_by_id('{}_{}'.format(event.key.id(), team.key.id())) # Should be in context cache status_str = { 'alliance': EventTeamStatusHelper.generate_team_at_event_alliance_status_string(team.key.id(), event_team.status), 'playoff': EventTeamStatusHelper.generate_team_at_event_playoff_status_string(team.key.id(), event_team.status), } teams_and_statuses.append(( team, event_team.status, status_str, AwardHelper.organizeAwards(event_team_awards[event.key.id()][team.key.id()]) )) teams_and_statuses.sort(key=lambda x: x[0].team_number) past_events_with_teams.append((event, teams_and_statuses)) past_events_with_teams.sort(key=lambda x: x[0].name) past_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) past_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) live_events_with_teams = [] for event, teams in live_events_by_event.itervalues(): teams_and_statuses = [] for team in teams: event_team = EventTeam.get_by_id('{}_{}'.format(event.key.id(), team.key.id())) # Should be in context cache status_str = { 'alliance': EventTeamStatusHelper.generate_team_at_event_alliance_status_string(team.key.id(), event_team.status), 'playoff': EventTeamStatusHelper.generate_team_at_event_playoff_status_string(team.key.id(), event_team.status), } teams_and_statuses.append(( team, event_team.status, status_str )) teams_and_statuses.sort(key=lambda x: x[0].team_number) live_events_with_teams.append((event, teams_and_statuses)) live_events_with_teams.sort(key=lambda x: x[0].name) live_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) live_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) future_events_with_teams = [] for event, teams in future_events_by_event.itervalues(): teams.sort(key=lambda t: t.team_number) future_events_with_teams.append((event, teams)) future_events_with_teams.sort(key=lambda x: x[0].name) future_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) future_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) self.template_values.update({ 'year': year, 'past_only': year < now.year, 'past_events_with_teams': past_events_with_teams, 'live_events_with_teams': live_events_with_teams, 'future_events_with_teams': future_events_with_teams, }) path = os.path.join(os.path.dirname(__file__), '../templates/mytba_live.html') self.response.out.write(template.render(path, self.template_values))
def _render(self, district_abbrev, year=None, explicit_year=False): district = DistrictQuery('{}{}'.format(year, district_abbrev)).fetch() if not district: self.abort(404) events_future = DistrictEventsQuery(district.key_name).fetch_async() # needed for district teams district_teams_future = DistrictTeamsQuery( district.key_name).fetch_async() # needed for valid_years history_future = DistrictHistoryQuery( district.abbreviation).fetch_async() # needed for valid_districts districts_in_year_future = DistrictsInYearQuery( district.year).fetch_async() # needed for active team statuses live_events = [] if year == datetime.datetime.now( ).year: # Only show active teams for current year live_events = EventHelper.getWeekEvents() live_eventteams_futures = [] for event in live_events: live_eventteams_futures.append( EventTeamsQuery(event.key_name).fetch_async()) events = events_future.get_result() EventHelper.sort_events(events) events_by_key = {} for event in events: events_by_key[event.key.id()] = event week_events = EventHelper.groupByWeek(events) valid_districts = set() districts_in_year = districts_in_year_future.get_result() for dist in districts_in_year: valid_districts.add((dist.display_name, dist.abbreviation)) valid_districts = sorted(valid_districts, key=lambda (name, _): name) teams = TeamHelper.sortTeams(district_teams_future.get_result()) team_keys = set([t.key.id() for t in teams]) num_teams = len(teams) middle_value = num_teams / 2 if num_teams % 2 != 0: middle_value += 1 teams_a, teams_b = teams[:middle_value], teams[middle_value:] # Currently Competing Team Status event_team_keys = [] for event, teams_future in zip(live_events, live_eventteams_futures): for team in teams_future.get_result(): if team.key.id() in team_keys: event_team_keys.append( ndb.Key(EventTeam, '{}_{}'.format( event.key.id(), team.key.id()))) # Should be in context cache ndb.get_multi(event_team_keys) # Warms context cache live_events_with_teams = [] for event, teams_future in zip(live_events, live_eventteams_futures): teams_and_statuses = [] has_teams = False for team in teams_future.get_result(): if team.key.id() in team_keys: has_teams = True event_team = EventTeam.get_by_id('{}_{}'.format( event.key.id(), team.key.id())) # Should be in context cache status_str = { 'alliance': EventTeamStatusHelper. generate_team_at_event_alliance_status_string( team.key.id(), event_team.status), 'playoff': EventTeamStatusHelper. generate_team_at_event_playoff_status_string( team.key.id(), event_team.status), } teams_and_statuses.append( (team, event_team.status, status_str)) if has_teams: teams_and_statuses.sort(key=lambda x: x[0].team_number) live_events_with_teams.append((event, teams_and_statuses)) live_events_with_teams.sort(key=lambda x: x[0].name) live_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) live_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) # Get valid years district_history = history_future.get_result() valid_years = map(lambda d: d.year, district_history) valid_years = sorted(valid_years) self.template_values.update({ 'explicit_year': explicit_year, 'year': year, 'valid_years': valid_years, 'valid_districts': valid_districts, 'district_name': district.display_name, 'district_abbrev': district_abbrev, 'week_events': week_events, 'events_by_key': events_by_key, 'rankings': district.rankings, 'advancement': district.advancement, 'teams_a': teams_a, 'teams_b': teams_b, 'live_events_with_teams': live_events_with_teams, }) return jinja2_engine.render('district_details.html', self.template_values)
def get(self): self._require_registration() user = self.user_bundle.account.key now = datetime.datetime.now() year = self.request.get('year') if year and year.isdigit(): year = int(year) else: year = now.year team_favorites_future = Favorite.query( Favorite.model_type == ModelType.TEAM, ancestor=user).fetch_async() favorite_team_keys = map(lambda f: ndb.Key(Team, f.model_key), team_favorites_future.get_result()) favorite_teams_future = ndb.get_multi_async(favorite_team_keys) favorite_teams = [ team_future.get_result() for team_future in favorite_teams_future ] favorite_teams_events_futures = [] favorite_teams_awards_futures = {} for team in favorite_teams: favorite_teams_events_futures.append( TeamYearEventsQuery(team.key_name, year).fetch_async()) favorite_teams_awards_futures[team.key.id()] = TeamYearAwardsQuery( team.key_name, year).fetch_async() past_events_by_event = {} live_events_by_event = {} future_events_by_event = {} favorite_event_team_keys = [] for team, events_future in zip(favorite_teams, favorite_teams_events_futures): events = events_future.get_result() if not events: continue EventHelper.sort_events(events) # Sort by date for event in events: favorite_event_team_keys.append( ndb.Key(EventTeam, '{}_{}'.format(event.key.id(), team.key.id()))) if event.within_a_day: if event.key_name not in live_events_by_event: live_events_by_event[event.key_name] = (event, []) live_events_by_event[event.key_name][1].append(team) elif event.start_date < now: if event.key_name not in past_events_by_event: past_events_by_event[event.key_name] = (event, []) past_events_by_event[event.key_name][1].append(team) else: if event.key_name not in future_events_by_event: future_events_by_event[event.key_name] = (event, []) future_events_by_event[event.key_name][1].append(team) event_team_awards = defaultdict(lambda: defaultdict(list)) for team_key, awards_future in favorite_teams_awards_futures.items(): for award in awards_future.get_result(): event_team_awards[award.event.id()][team_key].append(award) ndb.get_multi(favorite_event_team_keys) # Warms context cache past_events_with_teams = [] for event, teams in past_events_by_event.itervalues(): teams_and_statuses = [] for team in teams: event_team = EventTeam.get_by_id('{}_{}'.format( event.key.id(), team.key.id())) # Should be in context cache status_str = { 'alliance': EventTeamStatusHelper. generate_team_at_event_alliance_status_string( team.key.id(), event_team.status), 'playoff': EventTeamStatusHelper. generate_team_at_event_playoff_status_string( team.key.id(), event_team.status), } teams_and_statuses.append( (team, event_team.status, status_str, AwardHelper.organizeAwards( event_team_awards[event.key.id()][team.key.id()]))) teams_and_statuses.sort(key=lambda x: x[0].team_number) past_events_with_teams.append((event, teams_and_statuses)) past_events_with_teams.sort(key=lambda x: x[0].name) past_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) past_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) live_events_with_teams = [] for event, teams in live_events_by_event.itervalues(): teams_and_statuses = [] for team in teams: event_team = EventTeam.get_by_id('{}_{}'.format( event.key.id(), team.key.id())) # Should be in context cache status_str = { 'alliance': EventTeamStatusHelper. generate_team_at_event_alliance_status_string( team.key.id(), event_team.status), 'playoff': EventTeamStatusHelper. generate_team_at_event_playoff_status_string( team.key.id(), event_team.status), } teams_and_statuses.append( (team, event_team.status, status_str)) teams_and_statuses.sort(key=lambda x: x[0].team_number) live_events_with_teams.append((event, teams_and_statuses)) live_events_with_teams.sort(key=lambda x: x[0].name) live_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) live_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) future_events_with_teams = [] for event, teams in future_events_by_event.itervalues(): teams.sort(key=lambda t: t.team_number) future_events_with_teams.append((event, teams)) future_events_with_teams.sort(key=lambda x: x[0].name) future_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) future_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) self.template_values.update({ 'year': year, 'past_only': year < now.year, 'past_events_with_teams': past_events_with_teams, 'live_events_with_teams': live_events_with_teams, 'future_events_with_teams': future_events_with_teams, }) path = os.path.join(os.path.dirname(__file__), '../templates/mytba_live.html') self.response.out.write(template.render(path, self.template_values))
def get(self): self._require_login() self._require_registration() user = self.user_bundle.account.key now = datetime.datetime.now() team_favorites_future = Favorite.query( Favorite.model_type == ModelType.TEAM, ancestor=user).fetch_async() live_events = EventHelper.getEventsWithinADay() favorite_team_keys = map(lambda f: ndb.Key(Team, f.model_key), team_favorites_future.get_result()) favorite_teams_future = ndb.get_multi_async(favorite_team_keys) live_eventteams_futures = [] for event in live_events: live_eventteams_futures.append( EventTeamsQuery(event.key_name).fetch_async()) favorite_teams = [ team_future.get_result() for team_future in favorite_teams_future ] favorite_teams_events_futures = [] for team in favorite_teams: favorite_teams_events_futures.append( TeamYearEventsQuery(team.key_name, now.year).fetch_async()) live_events_with_teams = EventTeamStatusHelper.buildEventTeamStatus( live_events, live_eventteams_futures, favorite_teams) future_events_by_event = {} for team, events_future in zip(favorite_teams, favorite_teams_events_futures): events = events_future.get_result() if not events: continue EventHelper.sort_events(events) next_event = next( (e for e in events if e.start_date > now and not e.within_a_day), None) if next_event: if next_event.key_name not in future_events_by_event: future_events_by_event[next_event.key_name] = (next_event, []) future_events_by_event[next_event.key_name][1].append(team) future_events_with_teams = [] for event_key, data in future_events_by_event.iteritems(): future_events_with_teams.append( (data[0], TeamHelper.sortTeams(data[1]))) future_events_with_teams.sort(key=lambda x: x[0].name) future_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) future_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) self.template_values.update({ 'live_events_with_teams': live_events_with_teams, 'future_events_with_teams': future_events_with_teams, }) path = os.path.join(os.path.dirname(__file__), '../templates/mytba_live.html') self.response.out.write(template.render(path, self.template_values))