def addLatestResultsToContext(self, context): """ Helper method to add latest results to a context dictionary. context = the view context (a dictionary) Adds a 'latest_results' entry to the context dictionary containing a list of dates, with each date containing the matches played on that date, ordered by team position The latest_results list contains a maximum of one result per team. Results are only included if the team is active and the match was played this season. """ latest_results = [] current_season = Season.current() today = timezone.now().date() dt_now = datetime.now() for team in ClubTeam.objects.active().only('pk'): match_qs = Match.objects.select_related('our_team', 'opp_team__club', 'venue', 'division__league', 'cup', 'season') match_qs = match_qs.filter(our_team_id=team.pk, date__lte=today, season=current_season) match_qs = match_qs.order_by('-date', '-time') # Have to ignore matches that are today but still in future. for m in match_qs: if m.datetime() < dt_now: latest_results.append(m) break context['latest_results'] = self.group_by_date(latest_results, True)
def add_latest_results_to_context(context): """ Helper method to add latest results to a context dictionary. context = the view context (a dictionary) Returns: the context dictionary, with a 'latest_results' entry containing a list of results The latest_results list contains a maximum of one result per team. Results are only included if the team is active and the match was played this season. """ latest_results = [] current_season = Season.current() today = timezone.now().date() for team in ClubTeam.objects.only('pk'): match_qs = Match.objects.select_related('our_team', 'opp_team__club', 'venue', 'division__league', 'cup', 'season') match_qs = match_qs.filter(our_team__active=True, our_team_id=team.pk, date__lte=today, season=current_season) match_qs = match_qs.order_by('-date', '-time') if match_qs.exists(): # Have to ignore matches that are today but still in future. dt_now = datetime.now() for m in match_qs: if m.datetime() < dt_now: latest_results.append(m) break context['latest_results'] = latest_results
def get_template_context(self, **kwargs): context = {} # The team is specified in the URL by its slug team = ClubTeam.objects.get(slug=kwargs['slug']) context['clubteam'] = team # The season is specified in the URL by its primary key season_id = int(kwargs['season_pk']) season = Season.objects.get(pk=season_id) context['season'] = season is_current_season = Season.is_current_season(season_id) context['is_current_season'] = is_current_season participation = ClubTeamSeasonParticipation.objects.select_related('team').filter(team=team, season_id=season_id).first() if participation.division_tables_url: context['participation'] = participation context['division'] = participation.division # Delete any existing data for this league table DivisionResult.objects.league_table(season=season, division=participation.division).delete() try: context['div_data'] = get_east_leagues_nw_division(participation.division_tables_url, participation.division, season) except Exception as e: print "Failed to parse league table: {}".format(e) return context
def get_context_data(self, **kwargs): context = super(MemberListView, self).get_context_data(**kwargs) current_season = Season.current() context['props'] = { 'canViewMap': self.request.user.has_perm('members.view_personal_data'), 'currentSeason': current_season.slug, 'teams': list(ClubTeam.objects.active().exclude(slug__in=['indoor', 'mixed']).values('long_name', 'slug')), } return context
def add_season_selector(context, season, season_list): """ Adds season information to the given context, facilitating the use of the core/_season_selector.html template. Returns the updated context. """ context['season'] = season context['season_list'] = season_list context['is_current_season'] = Season.is_current_season(season.id) return context
def add_season_selector(context, season, season_slug_list): """ Adds season information to the given context, facilitating the use of the core/_season_selector.html template. Returns the updated context. """ context['season'] = season context['season_slug_list'] = season_slug_list context['is_current_season'] = Season.is_current_season(season.id) return context
def get_context_data(self, **kwargs): context = super(VenueListView, self).get_context_data(**kwargs) current_season = Season.current() context['props'] = { 'currentSeason': current_season.slug, 'teams': js_clubteams(True), 'divisions': js_divisions(current_season), } return context
def get_season_from_kwargs(kwargs): """ Many URLs have an optional 'season_slug' parameter, specifying a particular season. If not specified, we default to the current season. """ season_slug = kwargs_or_none('season_slug', **kwargs) if season_slug is not None: season = Season.objects.get(slug=season_slug) else: season = Season.current() return season
def get_context_data(self, **kwargs): context = super(GoalKingView, self).get_context_data(**kwargs) context['props'] = { 'seasons': list( GoalKing.objects.order_by('-season').values_list( 'season__slug', flat=True).distinct()), 'teams': list(ClubTeam.objects.active().values('long_name', 'slug')), 'current_season': Season.current().slug, } return context
def handle(self, *args, **options): errors = [] season = Season.current() try: # Update goal king GoalKing.update_for_season(season) except Exception as e: errors.append("Failed to update Goal-King: {}".format(e)) try: # Update Southerners league update_southerners_stats_for_season(season) except Exception as e: errors.append("Failed to update Southerners League: {}".format(e)) try: # Update opposition stats update_all_club_stats() except Exception as e: errors.append("Failed to update Opposition Club Stats: {}".format(e)) try: # Update ClubTeamSeasonParticipation stats update_participation_stats_for_season(season) except Exception as e: errors.append("Failed to update Club Team Season Participation Stats: {}".format(e)) # Scrape league tables participations = ClubTeamSeasonParticipation.objects.current().select_related('team', 'division') for participation in participations: try: DivisionResult.objects.league_table(season=season, division=participation.division).delete() try: league_scraper.get_east_leagues_nw_division(participation.division_tables_url, participation.division, season) except Exception as e: print "Failed to parse league table: {}".format(e) except Exception as e: errors.append("Failed to scrape league table from {}: {}".format(participation.division_tables_url, e)) try: # Delete all training session entries from before yesterday (we don't care about # training sessions in the past) self.purge_training_sessions() except Exception as e: errors.append("Failed to purge training sessions: {}".format(e)) if errors: send_mail("Nightly build task failed", "\n".join(errors), '*****@*****.**', ['*****@*****.**'])
def priority(self, item): if item == Season.current(): return 1.0 else: return 0.7
def changefreq(self, item): if item == Season.current(): return 'monthly' else: return 'yearly'
def changefreq(self, item): if item == Season.current(): return 'monthly' else: return 'never'
def current(self): """ Returns only current committee membership, if any.""" return self.filter(season=Season.current()).order_by('position__index')
def current(self): """ Returns only current squad membership, if any.""" return self.get_queryset().filter(season=Season.current())
def this_season(self): """Returns only training sessions for this season""" season = Season.current() return self.filter(datetime__gte=season.start, datetime__lte=season.end).order_by('datetime')
def this_season(self): """ Returns only this season's matches""" return self.by_season(Season.current())
def current(self): """ Returns only current committee membership, if any.""" return self.get_queryset().filter( season=Season.current()).order_by('position__index')
def handle(self, *args, **options): errors = [] season = Season.current() try: # Update goal king GoalKing.update_for_season(season) print('Updated Goal King entries for the current season') except Exception as e: errors.append("Failed to update Goal-King: {}".format(e)) try: # Update Southerners league update_southerners_stats_for_season(season) print('Updated Southerners league for the current season') except Exception as e: errors.append("Failed to update Southerners League: {}".format(e)) try: # Update opposition stats update_all_club_stats() print('Updated all opposition club stats') except Exception as e: errors.append( "Failed to update Opposition Club Stats: {}".format(e)) try: # Update ClubTeamSeasonParticipation stats update_participation_stats_for_season(season) print( 'Updated Club Team Season Participation stats for the current season' ) except Exception as e: errors.append( "Failed to update Club Team Season Participation Stats: {}". format(e)) try: # Delete all training session entries from before yesterday # (we don't care about training sessions in the past) yesterday = timezone.now() - timedelta(days=1) TrainingSession.objects.before(yesterday).delete() print("Purged all training sessions from before yesterday") except Exception as e: errors.append("Failed to purge training sessions: {}".format(e)) # Scrape league tables query = Q(division_tables_url__isnull=True) | Q(division_tables_url='') participations = ClubTeamSeasonParticipation.objects.current().exclude( query).select_related('team', 'division') for participation in participations: try: league_scraper.get_east_leagues_division( participation.division_tables_url, participation.division, season) print('Scraped league table for ' + participation.division_tables_url) except Exception as e: errors.append( "Failed to scrape league table from {}: {}".format( participation.division_tables_url, e)) for error in errors: print(error)
def current(self): """ Returns just the participations for the current season""" return self.filter(season=Season.current())
def get_template_context(self, **kwargs): context = {} # The team is specified in the URL by its slug team = ClubTeam.objects.get(slug=kwargs['slug']) context['clubteam'] = team # The season is specified in the URL by its primary key season_id = int(kwargs['season_pk']) season = Season.objects.get(pk=season_id) context['season'] = season is_current_season = Season.is_current_season(season_id) # Get all matches for this team and season match_qs = Match.objects.select_related('our_team', 'opp_team__club', 'venue', 'division__league', 'cup', 'season') match_qs = match_qs.filter(our_team=team, season_id=season_id) # Get all appearances for this team and season app_qs = Appearance.objects.select_related('member__user', 'match__season') app_qs = app_qs.filter(match__season_id=season_id, match__our_team=team) # Get all the award winners for this team and season award_winners_qs = MatchAwardWinner.objects.select_related('member__user', 'match__season', 'award') award_winners_qs = award_winners_qs.filter(match__season_id=season_id, match__our_team=team) match_lookup = {match.pk: MatchStats(match) for match in match_qs} squad_lookup = {} for app in app_qs: if app.member_id not in squad_lookup: squad_lookup[app.member_id] = SquadMember(app.member) squad_lookup[app.member_id].add_appearance(app) match_lookup[app.match_id].add_appearance(app) for award_winner in award_winners_qs: if award_winner.member_id in squad_lookup: squad_lookup[award_winner.member_id].add_award(award_winner.award) match_lookup[award_winner.match_id].add_award_winner(award_winner) participation = ClubTeamSeasonParticipation.objects.select_related('team').get(team=team, season_id=season_id) context['participation'] = participation context['division'] = participation.division # Attempt to retrieve the data from the database context['div_data'] = DivisionResult.objects.league_table(season=season, division=participation.division) if not context['div_data']: LOG.warn("No league table available for team {} in {}".format(team, season)) # first build the list of list of matches by date matches_by_date = defaultdict(list) [matches_by_date[m.match.date].append(m) for m in match_lookup.itervalues()] if team.fill_blanks: # populate dates with no matches dates = saturdays_in_range(match_qs[0].date, match_qs[len(match_qs)-1].date) [matches_by_date[d] for d in dates] # Create a list of (date, list of matchstats) tuples match_tuple_list = sorted(matches_by_date.items()) context['is_current_season'] = is_current_season context['match_list'] = match_tuple_list # TODO: Sort squad by position? context['squad_list'] = sorted(squad_lookup.values(), key=lambda playerstats: playerstats.member.last_name) context['fill_blanks'] = team.fill_blanks return context
def current(self): """ Returns just the participations for the current season""" return self.get_queryset().filter(season=Season.current())
def current(self): """ Returns only current squad membership, if any.""" return self.filter(season=Season.current())