<commit_before>import datetime
import os
import logging

from google.appengine.api import memcache
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

import tba_config
from base_controller import BaseHandler
from helpers.event_helper import EventHelper
from helpers.match_helper import MatchHelper
from models.event import Event
from models.event_team import EventTeam
from models.match import Match
from models.team import Team

# The view of a list of teams.
class TeamList(BaseHandler):
    def get(self):
        
        memcache_key = "team_list"
        html = memcache.get(memcache_key)
        
        if html is None:
            teams = Team.all().order('team_number').fetch(10000)        

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

            template_values = {
                "teams_a": teams_a,
                "teams_b": teams_b,
                "num_teams": num_teams,
            }
        
            path = os.path.join(os.path.dirname(__file__), '../templates/team_list.html')
            html = template.render(path, template_values)
            if tba_config.CONFIG["memcache"]: memcache.set(memcache_key, html, 86400)
        
        self.response.out.write(html)
        
# The view of a single Team.
class TeamDetail(BaseHandler):
    def get(self, team_number, year=None):
        
        # /team/0201 should redirect to /team/201
        try:
            if str(int(team_number)) != team_number:
                if year is None:
                    return self.redirect("/team/%s" % int(team_number))
                else:
                    return self.redirect("/team/%s/%s" % (int(team_number), year))
        except ValueError, e:
            logging.info("%s", e)
            return self.redirect("/error/404")
        
        if type(year) == str: 
            try:
                year = int(year)
            except ValueError, e:
                logging.info("%s", e)
                return self.redirect("/team/%s" % team_number)
            explicit_year = True
        else:
            year = datetime.datetime.now().year
            explicit_year = False
        
        memcache_key = "team_detail_%s_%s" % ("frc" + team_number, year)
        html = memcache.get(memcache_key)
        
        if html is None:
            team = Team.get_by_key_name("frc" + team_number)
            
            if not team:
                return self.redirect("/error/404")
            
            events = [a.event for a in team.events if a.year == year]
            for event in events:
                if not event.start_date:
                    event.start_date = datetime.datetime(year, 12, 31) #unknown goes last
            events = sorted(events, key=lambda event: event.start_date)
            
            years = sorted(set([a.year for a in team.events if a.year != None]))
            
            participation = list()
            
            # Return an array of event names and a list of matches from that event that the
            # team was a participant in.
            year_wlt_list = list()
            for e in events:
                match_list = e.match_set.filter("team_key_names =", team.key().name())
                matches = MatchHelper.organizeMatches(match_list)
                wlt = EventHelper.getTeamWLTFromMatches(team.key().name(), match_list)
                year_wlt_list.append(wlt)
                if wlt["win"] + wlt["loss"] + wlt["tie"] == 0:
                    display_wlt = None
                else:
                    display_wlt = wlt
                participation.append({ 'event' : e,
                                       'matches' : matches,
                                       'wlt': display_wlt })
            
            team.do_split_address()
            
            year_wlt = {"win": 0, "loss": 0, "tie": 0}
            for wlt in year_wlt_list:
                year_wlt["win"] += wlt["win"]
                year_wlt["loss"] += wlt["loss"]
                year_wlt["tie"] += wlt["tie"]
            if year_wlt["win"] + year_wlt["loss"] + year_wlt["tie"] == 0:
                year_wlt = None
            
            template_values = { "explicit_year": explicit_year,
                                "team": team,
                                "participation": participation,
                                "year": year,
                                "years": years,
                                "year_wlt": year_wlt }
            
            path = os.path.join(os.path.dirname(__file__), '../templates/team_details.html')
            html = template.render(path, template_values)
            if tba_config.CONFIG["memcache"]: memcache.set(memcache_key, html, 300)
        
        self.response.out.write(html)


<commit_msg>Call team.events less, fixes #195<commit_after>import datetime
import os
import logging

from google.appengine.api import memcache
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

import tba_config
from base_controller import BaseHandler
from helpers.event_helper import EventHelper
from helpers.match_helper import MatchHelper
from models.event import Event
from models.event_team import EventTeam
from models.match import Match
from models.team import Team

# The view of a list of teams.
class TeamList(BaseHandler):
    def get(self):
        
        memcache_key = "team_list"
        html = memcache.get(memcache_key)
        
        if html is None:
            teams = Team.all().order('team_number').fetch(10000)        

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

            template_values = {
                "teams_a": teams_a,
                "teams_b": teams_b,
                "num_teams": num_teams,
            }
        
            path = os.path.join(os.path.dirname(__file__), '../templates/team_list.html')
            html = template.render(path, template_values)
            if tba_config.CONFIG["memcache"]: memcache.set(memcache_key, html, 86400)
        
        self.response.out.write(html)
        
# The view of a single Team.
class TeamDetail(BaseHandler):
    def get(self, team_number, year=None):
        
        # /team/0201 should redirect to /team/201
        try:
            if str(int(team_number)) != team_number:
                if year is None:
                    return self.redirect("/team/%s" % int(team_number))
                else:
                    return self.redirect("/team/%s/%s" % (int(team_number), year))
        except ValueError, e:
            logging.info("%s", e)
            return self.redirect("/error/404")
        
        if type(year) == str: 
            try:
                year = int(year)
            except ValueError, e:
                logging.info("%s", e)
                return self.redirect("/team/%s" % team_number)
            explicit_year = True
        else:
            year = datetime.datetime.now().year
            explicit_year = False
        
        memcache_key = "team_detail_%s_%s" % ("frc" + team_number, year)
        html = memcache.get(memcache_key)
        
        if html is None:
            team = Team.get_by_key_name("frc" + team_number)
            
            if not team:
                return self.redirect("/error/404")
            
            team_events = [e for e in team.events]

            events = [a.event for a in team_events if a.year == year]
            for event in events:
                if not event.start_date:
                    event.start_date = datetime.datetime(year, 12, 31) #unknown goes last
            events = sorted(events, key=lambda event: event.start_date)
            
            years = sorted(set([a.year for a in team_events if a.year != None]))
            
            participation = list()
            
            # Return an array of event names and a list of matches from that event that the
            # team was a participant in.
            year_wlt_list = list()
            for e in events:
                match_list = e.match_set.filter("team_key_names =", team.key().name())
                matches = MatchHelper.organizeMatches(match_list)
                wlt = EventHelper.getTeamWLTFromMatches(team.key().name(), match_list)
                year_wlt_list.append(wlt)
                if wlt["win"] + wlt["loss"] + wlt["tie"] == 0:
                    display_wlt = None
                else:
                    display_wlt = wlt
                participation.append({ 'event' : e,
                                       'matches' : matches,
                                       'wlt': display_wlt })
            
            team.do_split_address()
            
            year_wlt = {"win": 0, "loss": 0, "tie": 0}
            for wlt in year_wlt_list:
                year_wlt["win"] += wlt["win"]
                year_wlt["loss"] += wlt["loss"]
                year_wlt["tie"] += wlt["tie"]
            if year_wlt["win"] + year_wlt["loss"] + year_wlt["tie"] == 0:
                year_wlt = None
            
            template_values = { "explicit_year": explicit_year,
                                "team": team,
                                "participation": participation,
                                "year": year,
                                "years": years,
                                "year_wlt": year_wlt }
            
            path = os.path.join(os.path.dirname(__file__), '../templates/team_details.html')
            html = template.render(path, template_values)
            if tba_config.CONFIG["memcache"]: memcache.set(memcache_key, html, 300)
        
        self.response.out.write(html)



{'fraction_non_alphanumeric': 0.10078062449959968, 'fraction_numerical': 0.00810648518815052, 'mean_word_length': 1.5369383092155369, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4202932', 'n_tokens_mistral': 2864, 'n_tokens_neox': 2724, 'n_words': 656}