Skip to content

Commit a0163d4

Browse files
committed
Add team records filtered by races, rewrite records urls and new items in menus.
1 parent 802ea05 commit a0163d4

13 files changed

+127
-70
lines changed

driver27/models.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,12 @@ 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):
218+
def team_stats_rank(self, unique_by_race=False, **filters):
219219
teams = self.teams.all()
220220
rank = []
221221
for team in teams:
222222
team_season = TeamSeason.objects.get(season=self, team=team)
223-
rank.append((team_season.get_stats(**filters), team))
223+
rank.append((team_season.get_stats(unique_by_race=unique_by_race, **filters), team))
224224
rank = sorted(rank, key=lambda x: x[0], reverse=True)
225225
return rank
226226

@@ -458,6 +458,12 @@ def get_results(self, limit_races=None):
458458
results = results.order_by('race__round')
459459
return results
460460

461+
def get_races(self, **filters):
462+
results = self.get_results().filter(**filters)\
463+
.values('race').annotate(count_race=models.Count('race'))\
464+
.order_by()
465+
return results
466+
461467
def get_points(self):
462468
results = self.get_results().exclude(wildcard=True).order_by('race__round')
463469
points_list = [result.points for result in results.all() if result.points is not None]
@@ -467,7 +473,12 @@ def get_filtered_results(self, **filters):
467473
results = self.get_results()
468474
return results.filter(**filters)
469475

470-
def get_stats(self, **filters):
476+
def get_filtered_races(self, **filters):
477+
return self.get_races(**filters)
478+
479+
def get_stats(self, unique_by_race=False, **filters):
480+
if unique_by_race:
481+
return self.get_filtered_races(**filters).count()
471482
return self.get_filtered_results(**filters).count()
472483

473484
# def delete(self, *args, **kwargs):

driver27/record_filters.py

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
'code': 'OUT',
4646
'label': _('OUT'),
4747
'filter': {'retired': True}
48+
}, {
49+
'code': 'FINISH',
50+
'label': _('Finish'),
51+
'filter': {'retired': False}
4852
},
4953
{
5054
'code': 'TOP5',

driver27/templates/driver27/driver-list-table.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{% load champion_filter i18n %}
22
<table class="table">
33
<tr>
4-
<th>{% trans 'Points' %}</th>
5-
<th>{% trans 'Driver' %}</th>
6-
<th>{% trans 'Team' %}</th>
4+
<th {% if not pending_races %}class="col-md-4"{% endif %}>{% trans 'Points' %}</th>
5+
<th {% if not pending_races %}class="col-md-3"{% endif %}>{% trans 'Driver' %}</th>
6+
<th {% if not pending_races %}class="col-md-5" {% endif %}>{% trans 'Team' %}</th>
77
{% if pending_races %}
88
{% for pending_race in pending_races %}
99
<th>{{ pending_race }}</th>

driver27/templates/driver27/driver-list.html

+1-15
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,7 @@
22
{% load i18n %}
33
{% block dr27_menu %}
44
{% include 'driver27/season-menu-pre.html' %}
5-
<li class="nav-item">
6-
<a class="nav-link" href="{% url 'season-driver' season.competition.slug season.year %}">{% trans 'Drivers' %}</a>
7-
</li>
8-
<li class="nav-item">
9-
<a>>></a>
10-
</li>
11-
<li class="nav-item">
12-
<a class="nav-link" href="{% url 'season-driver-olympic' season.competition.slug season.year %}">{% trans 'Olympic' %}</a>
13-
</li>
14-
{% if access_to_road %}
15-
<li class="nav-item">
16-
<a class="nav-link"
17-
href="{% url 'season-driver-road' season.competition.slug season.year %}">{% trans 'Road to championship' %}</a>
18-
</li>
19-
{% endif %}
5+
{% include 'driver27/driver-menu.html' %}
206
{% endblock %}
217
{% block dr27_content %}
228
{{ block.super }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{% load i18n %}
2+
<li class="nav-item">
3+
<a class="nav-link" href="{% url 'season-driver' season.competition.slug season.year %}">{% trans 'Drivers' %}</a>
4+
</li>
5+
<li class="nav-item">
6+
<a>>></a>
7+
</li>
8+
<li class="nav-item">
9+
<a class="nav-link" href="{% url 'season-driver-record-index' season.competition.slug season.year %}">{% trans 'Records' %}</a>
10+
</li>
11+
12+
<li class="nav-item">
13+
<a class="nav-link" href="{% url 'season-driver-olympic' season.competition.slug season.year %}">{% trans 'Olympic' %}</a>
14+
</li>
15+
16+
{% if access_to_road %}
17+
<li class="nav-item">
18+
<a class="nav-link"
19+
href="{% url 'season-driver-road' season.competition.slug season.year %}">{% trans 'Road to championship' %}</a>
20+
</li>
21+
{% endif %}

driver27/templates/driver27/record-view.html driver27/templates/driver27/driver-record.html

+7-10
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,25 @@
22
{% load i18n %}
33
{% block dr27_menu %}
44
{% include 'driver27/season-menu-pre.html' %}
5-
<li class="nav-item">
6-
<a class="nav-link" href="{% url 'season-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-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-record' season.competition.slug season.year 'FASTEST' %}">{% trans 'Fastest lap record' %}</a>
13-
</li>
5+
{% include 'driver27/driver-menu.html' %}
146
{% endblock %}
157
{% block dr27_content %}
168
{{ block.super }}
179
{% load champion_filter i18n %}
1810
<form method="POST">
1911
{% csrf_token %}
2012
<select id="record" name="record" onchange="change_record()">
13+
{% if not record %}
14+
<option value="">---</option>
15+
{% endif %}
2116
{% for record_code in record_codes %}
22-
<option value="{% url 'season-record' season.competition.slug season.year record_code.code %}"
17+
<option value="{% url 'season-driver-record' season.competition.slug season.year record_code.code %}"
2318
{% if record_code.code == record %}selected{% endif %}
2419
>{{ record_code.label }}</option>
2520
{% endfor %}
2621
</select>
2722
</form>
23+
{% if rank %}
2824
<table class="table">
2925
<tr>
3026
<th>{% trans 'Count' %}</th>
@@ -40,6 +36,7 @@
4036
{% endfor %}
4137

4238
</table>
39+
{% endif %}
4340
{% endblock %}
4441

4542
{% block bootstrap3_extra_script %}

driver27/templates/driver27/season-menu-pre.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<a class="nav-link" href="{% url 'competition-view' season.competition.slug %}">{{ season.competition }}</a>
44
</li>
55
<li class="nav-item">
6-
<a class="nav-link" href="{% url 'season-view' season.competition.slug season.year %}">{{ season }}</a>
6+
<a><<</a>
77
</li>
88
<li class="nav-item">
9-
<a><<</a>
10-
</li>
9+
<a class="nav-link" href="{% url 'season-view' season.competition.slug season.year %}">{{ season }}</a>
10+
</li>

driver27/templates/driver27/team-list-table.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{% load i18n %}
22
<table class="table">
33
<tr>
4-
<th>{% trans 'Points' %}</th>
5-
<th>{% trans 'Team' %}</th>
4+
<th class="col-md-4">{% trans 'Points' %}</th>
5+
<th class="col-md-8">{% trans 'Team' %}</th>
66
</tr>
77
{% for points,team in rank %}
88
<tr>

driver27/templates/driver27/team-list.html

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
{% load i18n %}
33
{% block dr27_menu %}
44
{% include 'driver27/season-menu-pre.html' %}
5-
6-
<li class="nav-item">
7-
<a class="nav-link" href="{% url 'season-team' season.competition.slug season.year %}">{% trans 'Teams' %}</a>
8-
</li>
5+
{% include 'driver27/team-menu.html' %}
96
{% endblock %}
107
{% block dr27_content %}
118
{{ block.super }}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% load i18n %}
2+
<li class="nav-item">
3+
<a class="nav-link" href="{% url 'season-team' season.competition.slug season.year %}">{% trans 'Teams' %}</a>
4+
</li>
5+
<li class="nav-item">
6+
<a>>></a>
7+
</li>
8+
<li class="nav-item">
9+
<a class="nav-link" href="{% url 'season-team-record-index' season.competition.slug season.year %}">{% trans 'Records' %}</a>
10+
</li>

driver27/templates/driver27/record-team-view.html driver27/templates/driver27/team-record.html

+16-9
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,35 @@
22
{% load i18n %}
33
{% block dr27_menu %}
44
{% 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>
5+
{% include 'driver27/team-menu.html' %}
6+
147
{% endblock %}
158
{% block dr27_content %}
169
{{ block.super }}
1710
{% load champion_filter i18n %}
1811
<form method="POST">
1912
{% csrf_token %}
2013
<select id="record" name="record" onchange="change_record()">
14+
{% if not record %}
15+
<option value="">---</option>
16+
{% endif %}
2117
{% for record_code in record_codes %}
2218
<option value="{% url 'season-team-record' season.competition.slug season.year record_code.code %}"
2319
{% if record_code.code == record %}selected{% endif %}
2420
>{{ record_code.label }}</option>
2521
{% endfor %}
2622
</select>
2723
</form>
24+
{% if rank %}
25+
<span class="container">
26+
{% if by_race %}
27+
<a href="{% url 'season-team-record' season.competition.slug season.year record %}">
28+
{% trans 'Current record' %}</a>
29+
{% else %}
30+
<a href="{% url 'season-team-record-by-race' season.competition.slug season.year record %}">
31+
{% trans 'Current record by race' %}</a>
32+
{% endif %}
33+
</span>
2834
<table class="table">
2935
<tr>
3036
<th>{% trans 'Count' %}</th>
@@ -38,6 +44,7 @@
3844
{% endfor %}
3945

4046
</table>
47+
{% endif %}
4148
{% endblock %}
4249

4350
{% block bootstrap3_extra_script %}

driver27/urls/season.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
url(r'^driver$', views.driver_rank_view, name='season-driver'),
88
url(r'^driver/road-to-championship$', views.driver_road_view, name='season-driver-road'),
99
url(r'^driver/olympic$', views.driver_olympic_view, name='season-driver-olympic'),
10+
url(r'^driver/record$', views.driver_record_view, name='season-driver-record-index'),
11+
url(r'^driver/record/(?P<record>[-\w\d]+)$', views.driver_record_view, name='season-driver-record'),
1012
url(r'^team$', views.team_rank_view, name='season-team'),
13+
url(r'^team/record$', views.team_record_view, name='season-team-record-index'),
14+
url(r'^team/record/(?P<record>[-\w\d]+)$', views.team_record_view, name='season-team-record'),
15+
url(r'^team/record/(?P<record>[-\w\d]+)/by-race$', views.team_record_by_race_view, name='season-team-record-by-race'),
1116
url(r'^race$', views.race_list, name='season-race-list'),
1217
url(r'^race/(?P<race_id>\d+)$', views.race_view, name='season-race-view'),
1318
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'),
15-
url(r'^team-record/(?P<record>[-\w\d]+)$', views.record_team_view, name='season-team-record')
1619
]

driver27/views.py

+40-19
Original file line numberDiff line numberDiff line change
@@ -150,34 +150,55 @@ def get_record_config(record):
150150
return None
151151

152152

153-
def record_view(request, competition_slug, year, record):
153+
def get_record_common_context(request, competition_slug, year, record=None):
154154
season = get_season(competition_slug, year)
155-
record_config = get_record_config(record)
156-
rank = season.stats_rank(**record_config['filter']) if record_config else None
157-
title = _('%(record_label)s Record, %(season)s') \
155+
if record:
156+
record_config = get_record_config(record)
157+
title = _('%(record_label)s Record, %(season)s') \
158158
% {'record_label': record_config['label'], 'season': season}
159-
context = {'rank': rank,
159+
else:
160+
title = _('Select a %(season)s record' % {'season': season})
161+
162+
context = {
160163
'season': season,
161164
'title': title,
162165
'record': record,
163166
'record_codes': DR27_RECORDS_FILTER
164167
}
165-
tpl = 'driver27/record-view.html'
168+
169+
return context
170+
171+
172+
def driver_record_view(request, competition_slug, year, record=None):
173+
context = get_record_common_context(request, competition_slug, year, record)
174+
175+
if record:
176+
season = context.get('season', None)
177+
record_config = get_record_config(record)
178+
rank = season.stats_rank(**record_config['filter']) if record_config else None
179+
else:
180+
rank = None
181+
context['rank'] = rank
182+
tpl = 'driver27/driver-record.html'
166183
return render(request, tpl, context)
167184

168185

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'
186+
def team_record_by_race_view(request, competition_slug, year, record=None):
187+
return team_record_view(request, competition_slug, year, record=record, unique_by_race=True)
188+
189+
190+
def team_record_view(request, competition_slug, year, record=None, unique_by_race=False):
191+
context = get_record_common_context(request, competition_slug, year, record)
192+
193+
if record:
194+
season = context.get('season', None)
195+
record_config = get_record_config(record)
196+
rank = season.team_stats_rank(unique_by_race=unique_by_race, **record_config['filter']) \
197+
if record_config else None
198+
else:
199+
rank = None
200+
context['rank'] = rank
201+
context['by_race'] = bool(unique_by_race)
202+
tpl = 'driver27/team-record.html'
182203
return render(request, tpl, context)
183204

0 commit comments

Comments
 (0)