def view_team_averages(request, only_us, only_here): teams = Match.objects.values_list('team_number', flat=True).distinct() print(teams) scores = [] for team_number in teams: total_auto = 0 total_tele = 0 matches = Match.objects.filter(team_number=team_number).exclude(location__name="TEST") if only_us: matches = matches.filter(scout__userprofile__team__team_number=request.user.userprofile.team.team_number) if only_here: matches = matches.filter(location=request.session.get('location_id')) for match in matches: auto_score, tele_score = match_score(match) total_auto += auto_score total_tele += tele_score try: avg_auto = total_auto / len(matches) avg_tele = total_tele / len(matches) avg_total = avg_auto + avg_tele avg_auto = str("%.2f" % avg_auto) avg_tele = str("%.2f" % avg_tele) avg_total = str("%.2f" % avg_total) scores.append({'team': team_number, 'auto': avg_auto, 'tele': avg_tele, 'total': avg_total}) except ZeroDivisionError: scores.append({'team': team_number, 'auto': "—", 'tele': "—", 'total': "—"}) scores = [s for s in scores if s['total'] != "—"] scores = sorted(scores, key=lambda k: 0 if k['total'] == "—" else float(k['total']), reverse=True) context = { 'nav_title': "Team Averages", 'parent': reverse('frc_scout:index'), 'scores': scores, 'opts': { 'only_us' : only_us, 'only_here' : only_here }, } return render(request, "frc_scout/results/team_averages.html", context)
def view_team_matches(request, team_number=None): matches = [] for match in Match.objects.filter(team_number=team_number).exclude(location__name="TEST").order_by('-timestamp'): match_dict = model_to_dict(match) if not match_dict['no_show']: match_dict['auto_total_score'], match_dict['tele_total_score'] = match_score(match) match_dict['total_score'] = match_dict['auto_total_score'] + match_dict['tele_total_score'] match_dict['auto_total_score'] = str("%.2f" % match_dict['auto_total_score']) match_dict['tele_total_score'] = str("%.2f" % match_dict['tele_total_score']) if match_dict['match_final_score'] is not None: try: match_dict['score_proportion'] = str("%.2f" % (match_dict['total_score'] / match_dict['match_final_score'] * 100)) except ZeroDivisionError: match_dict['score_proportion'] = "?" else: match_dict['score_proportion'] = "?" match_dict['total_score'] = str("%.2f" % match_dict['total_score']) stacks = ToteStack.objects.filter(match=match) if len(stacks) > 0: match_dict['tele_total_stacked'] = stacks.aggregate(Sum('totes_added'))['totes_added__sum'] match_dict['tele_average_totes_stacked'] = str("%.2f" % stacks.aggregate(Avg('totes_added'))['totes_added__avg']) match_dict['tele_average_stack_height'] = str("%.2f" % stacks.aggregate(Avg('start_height'))['start_height__avg']) else: match_dict['tele_total_stacked'] = 0 match_dict['tele_average_totes_stacked'] = "0.00" match_dict['tele_average_stack_height'] = "0.00" containers = ContainerStack.objects.filter(match=match) if len(containers) > 0: match_dict['tele_total_containers'] = containers.aggregate(Sum('containers_added'))['containers_added__sum'] match_dict['tele_average_container_height'] = str("%.2f" % containers.aggregate(Avg('height'))['height__avg']) else: match_dict['tele_total_containers'] = 0 match_dict['tele_average_container_height'] = "0.00" match_dict['tele_picked_up_total_containers'] = (match_dict['tele_picked_up_sideways_containers'] + match_dict['tele_picked_up_upright_containers'] + match_dict['tele_picked_up_center_step_containers']) match_dict['auto_total_acquired_containers'] = (match_dict['auto_step_center_acquired_containers'] + match_dict['auto_ground_acquired_containers']) match_dict['tele_picked_up_total_totes'] = (match_dict['tele_picked_up_ground_upright_totes'] + match_dict['tele_picked_up_sideways_totes'] + match_dict['tele_picked_up_upside_down_totes'] + match_dict['tele_picked_up_human_station_totes']) match_dict['auto_has_auto'] = not match_dict['auto_no_auto'] for key in match_dict: if key == "no_show": continue if str(match_dict[key]) == "True": match_dict[key] = "Yes" if str(match_dict[key]) == "False": match_dict[key] = "No" if match.scout.userprofile.team.team_number == request.user.userprofile.team.team_number: match_dict['match_private_comments'] = match.matchprivatecomments.comments match_dict['scout_name'] = match.scout.first_name + " on team " + str(match.scout.userprofile.team.team_number) match_dict['scout_first_name'] = match.scout.first_name else: match_dict['scout_name'] = 'team ' + str(match.scout.userprofile.team.team_number) match_dict['location'] = match.location.name matches.append(match_dict) context = { 'team_number': team_number, 'matches': matches, 'nav_title': team_number + "'s Matches", } return render(request, 'frc_scout/profiles/matches.html', context)
def view_team_profile(request, team_number=None): if not team_number: team_number = request.user.userprofile.team.team_number elif int(team_number) < 1: return HttpResponse("Team numbers cannot be less than 1.", status=400) # oh boy here we go statistics = {} matches = Match.objects.filter(team_number=team_number).exclude(location__name="TEST") # only take matches for this team if matches is not None: # iterate over possible match fields for field in Match._meta.fields: value = None # field_type = IntegerField, BooleanField, etc. field_type = str(field.__class__).split("'")[1].split('.')[4] # field_name = tele_picked_up_yellow_crates_blah, etc. field_name = str(field).split('.')[2] if field_type == "IntegerField": # if it's an integer and not a special field if field_name != "team_number" and field_name != "match_number" and field_name != "scout_team_number": # then calculate the average of it m = matches.aggregate(Avg(field_name))[field_name+"__avg"] if m is not None: #value = str("%.2f" % matches.aggregate(Avg(field_name))[field_name+"__avg"]) value = str("%.2f" % matches.aggregate(Avg(field_name))[field_name+"__avg"]) else: value = None else: # if it's special, skip it continue elif field_type == "BooleanField": # if it's a boolean, calculate the % that has true (but not if matches.count() == 0) try: value = str("%.2f" % (matches.filter(**{str(field_name): True}).count() / matches.count() * 100)) except ZeroDivisionError: pass else: # otherwise, skip it continue # put it in the hash -- much simpler than the crazy wacko system of before statistics[field_name] = value if statistics[field_name] == None: statistics[field_name] = "—" if (statistics['match_final_score'] != "—") and (statistics['match_final_score'] != 0): # exclude things that have a 0 statistics['match_final_score'] = matches.exclude(match_final_score=0).aggregate( Avg('match_final_score'))['match_final_score__avg'] if statistics['match_final_score'] is None: statistics['match_final_score'] = 0 # calculate some special stats print(statistics['auto_step_center_acquired_containers']) if (statistics['auto_step_center_acquired_containers'] != "—") and (statistics['auto_ground_acquired_containers'] != "—"): statistics['auto_total_acquired_containers'] = str("%.2f" % (float(statistics['auto_step_center_acquired_containers']) + float(statistics['auto_ground_acquired_containers']))) else: statistics['auto_total_acquired_containers'] = "—" if (statistics['tele_picked_up_ground_upright_totes'] != "—") and (statistics['tele_picked_up_upside_down_totes'] != "—") and ( statistics['tele_picked_up_sideways_totes'] != "—") and (statistics['tele_picked_up_human_station_totes'] != "—"): statistics['tele_picked_up_total_totes'] = str("%.2f" % (float(statistics['tele_picked_up_ground_upright_totes']) + float(statistics['tele_picked_up_upside_down_totes']) + float(statistics['tele_picked_up_sideways_totes']) + float(statistics['tele_picked_up_human_station_totes']))) else: statistics['tele_picked_up_total_totes'] = "—" if (statistics['tele_picked_up_sideways_containers'] != "—") and (statistics['tele_picked_up_upright_containers'] != "—") and ( statistics['tele_picked_up_center_step_containers'] != "—"): statistics['tele_picked_up_total_containers'] = str("%.2f" % (float(statistics['tele_picked_up_sideways_containers']) + float(statistics['tele_picked_up_upright_containers']) + float(statistics['tele_picked_up_center_step_containers']))) else: statistics['tele_picked_up_total_containers'] = "—" # aggregate totestacks, yay! stacks = ToteStack.objects.filter(match__team_number=team_number, coop_stack=False) if (len(stacks) != 0) and (stacks != None): statistics['tele_average_stack_height'] = str("%.2f" % stacks.aggregate(Avg('start_height'))['start_height__avg']) statistics['tele_average_totes_stacked'] = str("%.2f" % stacks.aggregate(Avg('totes_added'))['totes_added__avg']) # I'm pretty proud of this -- it's the averages of the sum per match match_stacks = stacks.values('match').annotate(total_totes=Sum('totes_added')) statistics['tele_average_totes_stacked_per_match'] = str("%.2f" % match_stacks.aggregate(Avg('total_totes'))['total_totes__avg']) else: statistics['tele_average_stack_height'] = "—" statistics['tele_average_totes_stacked'] = "—" statistics['tele_average_totes_stacked_per_match'] = "—" coop_stacks = ToteStack.objects.filter(match__team_number=team_number, coop_stack=True) if (len(coop_stacks) != 0) and (coop_stacks != None): match_coop_stacks = coop_stacks.values('match').annotate(total_totes=Sum('totes_added')) statistics['tele_average_coop_totes_stacked_per_match'] = str("%.2f" % match_coop_stacks.aggregate(Avg('total_totes'))['total_totes__avg']) else: statistics['tele_average_coop_totes_stacked_per_match'] = "—" # aggregate containerstacks containers = ContainerStack.objects.filter(match__team_number=team_number) if (len(containers) != 0) and (containers != None): statistics['tele_average_container_height'] = str("%.2f" % containers.aggregate(Avg('height'))['height__avg']) match_containers = containers.values('match').annotate(total_containers=Count('containers_added')) statistics['tele_average_containers_stacked_per_match'] = str("%.2f" % match_containers.aggregate(Avg('total_containers'))['total_containers__avg']) else: statistics['tele_average_container_height'] = "—" statistics['tele_average_containers_stacked_per_match'] = "—" # match scores -- I moved the thing from the 'edit match' screen into its own function # because it's pretty useful and recyclable match_scores = [match_score(m) for m in matches] auto_scores = [m[0] for m in match_scores] tele_scores = [m[1] for m in match_scores] if len(auto_scores) > 0: statistics['auto_average_score'] = str("%.2f" % (sum(auto_scores) / len(auto_scores))) statistics['tele_average_score'] = str("%.2f" % (sum(tele_scores) / len(tele_scores))) statistics['total_average_score'] = str("%.2f" % (float(statistics['auto_average_score']) + float(statistics['tele_average_score']))) else: statistics['auto_average_score'] = "—" statistics['tele_average_score'] = "—" statistics['total_average_score'] = "—" if statistics['match_final_score'] != "—": try: statistics['total_score_proportion'] = str("%.2f" % (float(statistics['total_average_score']) / float(statistics['match_final_score']) * 100)) except ZeroDivisionError: statistics['total_score_proportion'] = "?" else: statistics['total_score_proportion'] = "—" pit_scout_data = PitScoutData.objects.filter(team_number=team_number).order_by('id').exclude(location__name="TEST") aggregate_data = PitScoutData(team_number=team_number) for data in pit_scout_data: for field in PitScoutData._meta.fields: if getattr(data, field.name): setattr(aggregate_data, field.name, getattr(data, field.name)) self_scouted = PitScoutData.objects.filter\ (team_number=team_number, scout__userprofile__team__team_number=team_number).order_by('id') for data in self_scouted: for field in PitScoutData._meta.fields: if getattr(data, field.name): setattr(aggregate_data, field.name, getattr(data, field.name)) # then pass all the sections/data to the context pitdatas = PitScoutData.objects.filter(team_number=team_number).count() try: param = str("team/frc%i" % int(team_number)) json_decoded = make_tba_request(param) except ValueError: json_decoded = None context = { 'has_pit_data': pitdatas, 'aggregate_data': model_to_dict(aggregate_data), 'team_number': team_number, 'statistics': statistics, 'nav_title': str(team_number), 'matches': matches, 'tba_data': json_decoded } if pitdatas == 1: context['scout_name'] = PitScoutData.objects.get(team_number=team_number).scout.first_name return render(request, 'frc_scout/profiles/profile.html', context)