Skip to content

Commit 802ea05

Browse files
committed
Add team records view.
1 parent d032e19 commit 802ea05

File tree

5 files changed

+108
-4
lines changed

5 files changed

+108
-4
lines changed

driver27/models.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ def stats_rank(self, **filters):
215215
rank = sorted(rank, key=lambda x: x[0], reverse=True)
216216
return rank
217217

218+
def team_stats_rank(self, **filters):
219+
teams = self.teams.all()
220+
rank = []
221+
for team in teams:
222+
team_season = TeamSeason.objects.get(season=self, team=team)
223+
rank.append((team_season.get_stats(**filters), team))
224+
rank = sorted(rank, key=lambda x: x[0], reverse=True)
225+
return rank
226+
218227
def points_rank(self, scoring_code=None):
219228
contenders = self.contenders()
220229
scoring = self.get_scoring(scoring_code)
@@ -442,13 +451,25 @@ def check_delete_seat_restriction(team, season):
442451
return bool(seats_count)
443452
# return {'team': _('Seats with %(team)s exists in this season. Delete seats before.' % {'team': team})}
444453

454+
def get_results(self, limit_races=None):
455+
results = Result.objects.filter(race__season=self.season, seat__team=self.team)
456+
if isinstance(limit_races, int):
457+
results = results.filter(race__round__lte=limit_races)
458+
results = results.order_by('race__round')
459+
return results
460+
445461
def get_points(self):
446-
results = Result.objects.filter(race__season=self.season, seat__team=self.team) \
447-
.exclude(wildcard=True) \
448-
.order_by('race__round')
462+
results = self.get_results().exclude(wildcard=True).order_by('race__round')
449463
points_list = [result.points for result in results.all() if result.points is not None]
450464
return sum(points_list)
451465

466+
def get_filtered_results(self, **filters):
467+
results = self.get_results()
468+
return results.filter(**filters)
469+
470+
def get_stats(self, **filters):
471+
return self.get_filtered_results(**filters).count()
472+
452473
# def delete(self, *args, **kwargs):
453474
# team = self.team
454475
# season = self.season

driver27/record_filters.py

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
'label': _('Pole'),
77
'filter': {'qualifying__exact': 1}
88
},
9+
{
10+
'code': 'FIRST-ROW',
11+
'label': _('First row'),
12+
'filter': {'qualifying__gte': 1, 'qualifying__lte': 2}
13+
},
14+
{
15+
'code': 'COMEBACK-TO-TEN',
16+
'label': _('Comeback to 10 firsts'),
17+
'filter': {'qualifying__gte': 11, 'finish__gte': 1, 'finish__lte': 10}
18+
},
919
{
1020
'code': 'WIN',
1121
'label': _('Win'),
@@ -40,6 +50,10 @@
4050
'code': 'TOP5',
4151
'label': _('Top 5'),
4252
'filter': {'finish__gte': 1, 'finish__lte': 5}
53+
}, {
54+
'code': 'TOP10',
55+
'label': _('Top 10'),
56+
'filter': {'finish__gte': 1, 'finish__lte': 10}
4357
},
4458

4559
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{% extends 'driver27/base.html' %}
2+
{% load i18n %}
3+
{% block dr27_menu %}
4+
{% include 'driver27/season-menu-pre.html' %}
5+
<li class="nav-item">
6+
<a class="nav-link" href="{% url 'season-team-record' season.competition.slug season.year 'POLE' %}">{% trans 'Pole record' %}</a>
7+
</li>
8+
<li class="nav-item">
9+
<a class="nav-link" href="{% url 'season-team-record' season.competition.slug season.year 'WIN' %}">{% trans 'Win record' %}</a>
10+
</li>
11+
<li class="nav-item">
12+
<a class="nav-link" href="{% url 'season-team-record' season.competition.slug season.year 'FASTEST' %}">{% trans 'Fastest lap record' %}</a>
13+
</li>
14+
{% endblock %}
15+
{% block dr27_content %}
16+
{{ block.super }}
17+
{% load champion_filter i18n %}
18+
<form method="POST">
19+
{% csrf_token %}
20+
<select id="record" name="record" onchange="change_record()">
21+
{% for record_code in record_codes %}
22+
<option value="{% url 'season-team-record' season.competition.slug season.year record_code.code %}"
23+
{% if record_code.code == record %}selected{% endif %}
24+
>{{ record_code.label }}</option>
25+
{% endfor %}
26+
</select>
27+
</form>
28+
<table class="table">
29+
<tr>
30+
<th>{% trans 'Count' %}</th>
31+
<th>{% trans 'Team' %}</th>
32+
</tr>
33+
{% for count,team in rank %}
34+
<tr>
35+
<td>{{ count }}</td>
36+
<td>{{ team }}</td>
37+
</tr>
38+
{% endfor %}
39+
40+
</table>
41+
{% endblock %}
42+
43+
{% block bootstrap3_extra_script %}
44+
{{ block.super }}
45+
<script type="text/javascript">
46+
function change_record(){
47+
var record = $('#record').val();
48+
location.href = record;
49+
}
50+
</script>
51+
{% endblock %}

driver27/urls/season.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
url(r'^race$', views.race_list, name='season-race-list'),
1212
url(r'^race/(?P<race_id>\d+)$', views.race_view, name='season-race-view'),
1313
url(r'^race/(?P<race_id>\d+)/(?P<rank>[-\w\d]+)$', views.race_view, name='season-race-rank'),
14-
url(r'^record/(?P<record>[-\w\d]+)$', views.record_view, name='season-record')
14+
url(r'^record/(?P<record>[-\w\d]+)$', views.record_view, name='season-record'),
15+
url(r'^team-record/(?P<record>[-\w\d]+)$', views.record_team_view, name='season-team-record')
1516
]

driver27/views.py

+17
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,20 @@ def record_view(request, competition_slug, year, record):
164164
}
165165
tpl = 'driver27/record-view.html'
166166
return render(request, tpl, context)
167+
168+
169+
def record_team_view(request, competition_slug, year, record):
170+
season = get_season(competition_slug, year)
171+
record_config = get_record_config(record)
172+
rank = season.team_stats_rank(**record_config['filter']) if record_config else None
173+
title = _('%(record_label)s Team Record, %(season)s') \
174+
% {'record_label': record_config['label'], 'season': season}
175+
context = {'rank': rank,
176+
'season': season,
177+
'title': title,
178+
'record': record,
179+
'record_codes': DR27_RECORDS_FILTER
180+
}
181+
tpl = 'driver27/record-team-view.html'
182+
return render(request, tpl, context)
183+

0 commit comments

Comments
 (0)