Beispiel #1
0
    def get(self, year):
        year = int(year)
        year_dcmps = DistrictChampsInYearQuery(year).fetch()
        districts_to_write = []

        for dcmp in year_dcmps:
            district_abbrev = DistrictType.type_abbrevs[
                dcmp.event_district_enum]
            district_key = District.renderKeyName(year, district_abbrev)
            logging.info("Creating {}".format(district_key))

            district = District(
                id=district_key,
                year=year,
                abbreviation=district_abbrev,
                display_name=DistrictType.type_names[dcmp.event_district_enum],
                elasticsearch_name=next(
                    (k
                     for k, v in DistrictType.elasticsearch_names.iteritems()
                     if v == dcmp.event_district_enum), None))
            districts_to_write.append(district)

        logging.info("Writing {} new districts".format(
            len(districts_to_write)))
        DistrictManipulator.createOrUpdate(districts_to_write,
                                           run_post_update_hook=False)

        for dcmp in year_dcmps:
            district_abbrev = DistrictType.type_abbrevs[
                dcmp.event_district_enum]
            district_key = District.renderKeyName(year, district_abbrev)
            district_events_future = DistrictEventsQuery(
                district_key).fetch_async()

            district_events = district_events_future.get_result()
            logging.info("Found {} events to update".format(
                len(district_events)))
            events_to_write = []
            for event in district_events:
                event.district_key = ndb.Key(District, district_key)
                events_to_write.append(event)
            EventManipulator.createOrUpdate(events_to_write)

        for dcmp in year_dcmps:
            district_abbrev = DistrictType.type_abbrevs[
                dcmp.event_district_enum]
            district_key = District.renderKeyName(year, district_abbrev)
            districtteams_future = DistrictTeam.query(
                DistrictTeam.year == year,
                DistrictTeam.district == DistrictType.abbrevs.get(
                    district_abbrev, None)).fetch_async()

            districtteams = districtteams_future.get_result()
            logging.info("Found {} DistrictTeams to update".format(
                len(districtteams)))
            districtteams_to_write = []
            for districtteam in districtteams:
                districtteam.district_key = ndb.Key(District, district_key)
                districtteams_to_write.append(districtteam)
            DistrictTeamManipulator.createOrUpdate(districtteams_to_write)
    def get(self, year):
        year = int(year)
        year_dcmps = DistrictChampsInYearQuery(year).fetch()
        districts_to_write = []

        for dcmp in year_dcmps:
            district_abbrev = DistrictType.type_abbrevs[dcmp.event_district_enum]
            district_key = District.renderKeyName(year, district_abbrev)
            logging.info("Creating {}".format(district_key))

            district = District(
                id=district_key,
                year=year,
                abbreviation=district_abbrev,
                display_name=DistrictType.type_names[dcmp.event_district_enum],
                elasticsearch_name=next((k for k, v in DistrictType.elasticsearch_names.iteritems() if v == dcmp.event_district_enum), None)
            )
            districts_to_write.append(district)

        logging.info("Writing {} new districts".format(len(districts_to_write)))
        DistrictManipulator.createOrUpdate(districts_to_write, run_post_update_hook=False)

        for dcmp in year_dcmps:
            district_abbrev = DistrictType.type_abbrevs[dcmp.event_district_enum]
            district_key = District.renderKeyName(year, district_abbrev)
            district_events_future = DistrictEventsQuery(district_key).fetch_async()

            district_events = district_events_future.get_result()
            logging.info("Found {} events to update".format(len(district_events)))
            events_to_write = []
            for event in district_events:
                event.district_key = ndb.Key(District, district_key)
                events_to_write.append(event)
            EventManipulator.createOrUpdate(events_to_write)

        for dcmp in year_dcmps:
            district_abbrev = DistrictType.type_abbrevs[dcmp.event_district_enum]
            district_key = District.renderKeyName(year, district_abbrev)
            districtteams_future = DistrictTeam.query(DistrictTeam.year == year, DistrictTeam.district == DistrictType.abbrevs.get(district_abbrev, None)).fetch_async()

            districtteams = districtteams_future.get_result()
            logging.info("Found {} DistrictTeams to update".format(len(districtteams)))
            districtteams_to_write = []
            for districtteam in districtteams:
                districtteam.district_key = ndb.Key(District, district_key)
                districtteams_to_write.append(districtteam)
            DistrictTeamManipulator.createOrUpdate(districtteams_to_write)
Beispiel #3
0
    def get(self, district_key):
        district = District.get_by_id(district_key)
        if not district:
            self.response.out.write(
                "District {} does not exist!".format(district_key))
            return

        events_future = DistrictEventsQuery(district_key).fetch_async()
        teams_future = DistrictTeamsQuery(district_key).fetch_async()

        events = events_future.get_result()
        for event in events:
            event.prep_details()
        EventHelper.sort_events(events)
        team_totals = DistrictHelper.calculate_rankings(
            events, teams_future, district.year)

        rankings = []
        current_rank = 1
        for key, points in team_totals:
            point_detail = {}
            point_detail["rank"] = current_rank
            point_detail["team_key"] = key
            point_detail["event_points"] = []
            for event, event_points in points["event_points"]:
                event_points['event_key'] = event.key.id()
                event_points['district_cmp'] = (
                    event.event_type_enum == EventType.DISTRICT_CMP or
                    event.event_type_enum == EventType.DISTRICT_CMP_DIVISION)
                point_detail["event_points"].append(event_points)

            point_detail["rookie_bonus"] = points.get("rookie_bonus", 0)
            point_detail["point_total"] = points["point_total"]
            rankings.append(point_detail)
            current_rank += 1

        if rankings:
            district.rankings = rankings
            DistrictManipulator.createOrUpdate(district)

        if 'X-Appengine-Taskname' not in self.request.headers:  # Only write out if not in taskqueue
            self.response.out.write(
                "Finished calculating rankings for: {}".format(district_key))
Beispiel #4
0
    def _render(self, district_abbrev, year=None):
        self._set_district(district_abbrev, self.year)

        if self.year < 2009:
            return json.dumps([], ensure_ascii=True)

        events_future = DistrictEventsQuery(
            District.renderKeyName(self.year, district_abbrev)).fetch_async()
        district_teams_future = DistrictTeamsQuery("{}{}".format(
            year, district_abbrev)).fetch_async()

        events = events_future.get_result()
        if not events:
            return json.dumps([], ensure_ascii=True)
        EventHelper.sort_events(events)

        team_totals = DistrictHelper.calculate_rankings(
            events, district_teams_future.get_result(), self.year)

        rankings = []

        current_rank = 1
        for key, points in team_totals:
            point_detail = {}
            point_detail["rank"] = current_rank
            point_detail["team_key"] = key
            point_detail["event_points"] = {}
            for event in points["event_points"]:
                event_key = event[0].key_name
                point_detail["event_points"][event_key] = event[1]
                event_details = Event.get_by_id(event_key)
                point_detail["event_points"][event[0].key_name][
                    'district_cmp'] = True if event_details.event_type_enum == EventType.DISTRICT_CMP else False

            if "rookie_bonus" in points:
                point_detail["rookie_bonus"] = points["rookie_bonus"]
            else:
                point_detail["rookie_bonus"] = 0
            point_detail["point_total"] = points["point_total"]
            rankings.append(point_detail)
            current_rank += 1

        return json.dumps(rankings)
    def get(self, district_key):
        district = District.get_by_id(district_key)
        if not district:
            self.response.out.write("District {} does not exist!".format(district_key))
            return

        events_future = DistrictEventsQuery(district_key).fetch_async()
        teams_future = DistrictTeamsQuery(district_key).fetch_async()

        events = events_future.get_result()
        for event in events:
            event.prep_details()
        EventHelper.sort_events(events)
        team_totals = DistrictHelper.calculate_rankings(events, teams_future, district.year)

        rankings = []
        current_rank = 1
        for key, points in team_totals:
            point_detail = {}
            point_detail["rank"] = current_rank
            point_detail["team_key"] = key
            point_detail["event_points"] = []
            for event, event_points in points["event_points"]:
                event_points['event_key'] = event.key.id()
                event_points['district_cmp'] = (
                    event.event_type_enum == EventType.DISTRICT_CMP or
                    event.event_type_enum == EventType.DISTRICT_CMP_DIVISION)
                point_detail["event_points"].append(event_points)

            point_detail["rookie_bonus"] = points.get("rookie_bonus", 0)
            point_detail["point_total"] = points["point_total"]
            rankings.append(point_detail)
            current_rank += 1

        if rankings:
            district.rankings = rankings
            DistrictManipulator.createOrUpdate(district)

        if 'X-Appengine-Taskname' not in self.request.headers:  # Only write out if not in taskqueue
            self.response.out.write("Finished calculating rankings for: {}".format(district_key))
    def _render(self, district_abbrev, year=None):
        self._set_district(district_abbrev, self.year)

        if self.year < 2009:
            return json.dumps([], ensure_ascii=True)

        events_future = DistrictEventsQuery(District.renderKeyName(self.year, district_abbrev)).fetch_async()
        district_teams_future = DistrictTeamsQuery("{}{}".format(year, district_abbrev)).fetch_async()

        events = events_future.get_result()
        if not events:
            return json.dumps([], ensure_ascii=True)
        EventHelper.sort_events(events)

        team_totals = DistrictHelper.calculate_rankings(events, district_teams_future.get_result(), self.year)

        rankings = []

        current_rank = 1
        for key, points in team_totals:
            point_detail = {}
            point_detail["rank"] = current_rank
            point_detail["team_key"] = key
            point_detail["event_points"] = {}
            for event in points["event_points"]:
                event_key = event[0].key_name
                point_detail["event_points"][event_key] = event[1]
                event_details = Event.get_by_id(event_key)
                point_detail["event_points"][event[0].key_name]['district_cmp'] = True if event_details.event_type_enum == EventType.DISTRICT_CMP else False

            if "rookie_bonus" in points:
                point_detail["rookie_bonus"] = points["rookie_bonus"]
            else:
                point_detail["rookie_bonus"] = 0
            point_detail["point_total"] = points["point_total"]
            rankings.append(point_detail)
            current_rank += 1

        return json.dumps(rankings)
    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)
Beispiel #8
0
    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()

        # Temp disabled on 2017-02-18 -fangeugene
        # # 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())

        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:]

        # Temp disabled on 2017-02-18 -fangeugene
        # # Currently Competing Team Status
        # live_events_with_teams = EventTeamStatusHelper.buildEventTeamStatus(live_events, live_eventteams_futures, teams)
        # live_events_with_teams.sort(key=lambda x: x[0].name)

        # 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,
            'teams_a': teams_a,
            'teams_b': teams_b,
            # 'live_events_with_teams': live_events_with_teams,  # Temp disabled on 2017-02-18 -fangeugene
        })

        return jinja2_engine.render('district_details.html', 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)