Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add seasons add allow to filter lap times #7

Merged
merged 1 commit into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from flask_wtf import FlaskForm
from flask_babel import lazy_gettext as _l
from wtforms import StringField, TextField, SubmitField, SelectField, FieldList, FormField, HiddenField
from wtforms.validators import ValidationError, DataRequired
from wtforms.validators import ValidationError, DataRequired, Optional
from wtforms.fields.html5 import DateField
from app.models import Racer, Car


Expand Down Expand Up @@ -55,3 +56,10 @@ def validate_order_number(self, order_number):
car = Car.query.filter_by(order_number=order_number.data).first()
if car is not None:
raise ValidationError(_l('Car already registered'))


class SeasonRegistrationForm(FlaskForm):
description = TextField(_l('description'), validators=[DataRequired()])
started_at = DateField('DatePicker', format='%Y-%m-%d')
ended_at = DateField('DatePicker', format='%Y-%m-%d', validators=(Optional(),))
submit = SubmitField(_l('Add Season'))
25 changes: 23 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,19 @@ class Racer(db.Model):
def __repr__(self):
return '<Racer {}>'.format(self.name)

def fastest_laps(self):
return Lap.query.filter(Lap.time > 1000, Lap.racer_id == self.id).order_by(Lap.time).limit(5).all()
def fastest_laps(self, season=None):
if season is None:
return Lap.query.filter(Lap.time > 2000, Lap.racer_id == self.id).order_by(Lap.time).limit(5).all()
races_from = season.started_at
races_to = season.ended_at
if races_to is None:
races_to = datetime.datetime.now()
return Lap.query.join(Race).filter(
Race.created_at.between(races_from, races_to)
).filter(
Lap.time > 2000,
Lap.racer_id == self.id
).order_by(Lap.time).limit(5).all()


class Car(db.Model):
Expand Down Expand Up @@ -160,6 +171,16 @@ def formatted_time(self):
return '{:05.3f} s'.format(self.time / 1000)


class Season(db.Model):
id = db.Column(db.Integer, primary_key=True)
started_at = db.Column(db.DateTime)
ended_at = db.Column(db.DateTime)
description = db.Column(db.String(64), index=False, unique=False)

def __repr__(self):
return '<Season {} - {}>'.format(self.started_at, self.ended_at)


class Timing(object):
def __init__(self, num):
self.num = num
Expand Down
32 changes: 29 additions & 3 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from flask import request, render_template, flash, redirect, url_for
from flask_babel import lazy_gettext as _l
from app import app, db, services
from app.forms import CarRegistrationForm, RaceRegistrationForm, RacerRegistrationForm
from app.models import Car, Race, Racer
from app.forms import CarRegistrationForm, RaceRegistrationForm, RacerRegistrationForm, SeasonRegistrationForm
from app.models import Car, Race, Racer, Season


@app.route('/')
Expand All @@ -29,8 +29,10 @@ def car(car_id):
@app.route('/racers')
def racers():
racers = Racer.query.all()
last_season = Season.query.filter(Season.ended_at.isnot(None)).order_by(Season.ended_at.desc()).first()
current_season = Season.query.filter(Season.ended_at.is_(None)).order_by(Season.started_at.desc()).first()
app.logger.info('got racers:' + repr(racers))
return render_template('racers.html', title='Fahrer', racers=racers)
return render_template('racers.html', title='Fahrer', racers=racers, last_season=last_season, current_season=current_season)


@app.route('/races')
Expand Down Expand Up @@ -94,6 +96,13 @@ def race(race_id):
return render_template('race.html', title='Rennen vom ', race=Race.query.get(race_id))


@app.route('/seasons')
def seasons():
seasons = Season.query.all()
app.logger.info('got seasons:' + repr(seasons))
return render_template('seasons.html', title='Saison', seasons=seasons)


@app.route('/racer_registration', methods=['GET', 'POST'])
def racer_registration():
form = RacerRegistrationForm()
Expand Down Expand Up @@ -140,6 +149,23 @@ def register():
return render_template('car_registration.html', title='Neues Auto registrieren', form=form)


@app.route('/season_registration', methods=['GET', 'POST'])
def season_registration():
form = SeasonRegistrationForm()

if form.validate_on_submit():
season = Season(
description=form.description.data,
started_at=form.started_at.data,
ended_at=form.ended_at.data
)
db.session.add(season)
db.session.commit()
flash(_l('New season created'))
return redirect(url_for('seasons'))
return render_template('season_registration.html', title='Saison anlegen', form=form)


def cancel_current_race():
race = Race.current()
if race is None:
Expand Down
1 change: 1 addition & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="{{ url_for('index') }}">{{ _('Index') }}</a></li>
<li><a href="{{ url_for('seasons') }}">{{ _('Seasons') }}</a></li>
<li><a href="{{ url_for('racers') }}">{{ _('Racers') }}</a></li>
<li><a href="{{ url_for('cars') }}">{{ _('Car park') }}</a></li>
<li><a href="{{ url_for('current_race') }}">{{ _('Current race') }}</a></li>
Expand Down
5 changes: 4 additions & 1 deletion app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ <h3><a href="{{ url_for('racers') }}">{{ _('Registered racers') }}</a></h3>
<h3><a href="{{ url_for('cars') }}">{{ _('Car park') }}</a></h3>
<p>
{{ _('Here you find all available race cars.') }}

</p>
<h3><a href="{{ url_for('seasons') }}">{{ _('Seasons') }}</a></h3>
<p>
{{ _('Create new seasons') }}
</p>
<h3><a href="{{ url_for('races') }}">{{ _('Previous races') }}</a></h3>
<p>
Expand Down
63 changes: 51 additions & 12 deletions app/templates/racers.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,57 @@ <h1>{{ _('Registered racers') }}</h1>
<li>
<h2> {{ racer.name }} </h2>
<h3>{{ _('Fastest Laps') }}</h3>
<table width = "80%">
<tr>
<th>{{ _('Time') }}</th>
<th>{{ _('Racer') }}</th>
</tr>
{% for lap in racer.fastest_laps() %}
<tr>
<td>{{ lap.formatted_time() }}</td>
<td>{{ lap.car().name }}</td>
</tr>
{% endfor %}
</table>

<div id="lab_table" class="lab_container">
<ul class="nav nav-pills">
<li class="active"><a href="#{{racer.id}}-overall" data-toggle="tab">{{ _('overall') }}</a></li>
<li><a href="#{{racer.id}}-current_season" data-toggle="tab">{{ _('current season') }}</a></li>
<li><a href="#{{racer.id}}-last_season" data-toggle="tab">{{ _('last season') }}</a></li>
</ul>
<div class="tab-content clearfix">
<div class="tab-pane active" id="{{racer.id}}-overall">
<table width = "80%">
<tr>
<th>{{ _('Time') }}</th>
<th>{{ _('Car') }}</th>
</tr>
{% for lap in racer.fastest_laps() %}
<tr>
<td>{{ lap.formatted_time() }}</td>
<td>{{ lap.car().name }}</td>
</tr>
{% endfor %}
</table>
</div>
<div class="tab-pane" id="{{racer.id}}-current_season">
<table width = "80%">
<tr>
<th>{{ _('Time') }}</th>
<th>{{ _('Car') }}</th>
</tr>
{% for lap in racer.fastest_laps(season=current_season) %}
<tr>
<td>{{ lap.formatted_time() }}</td>
<td>{{ lap.car().name }}</td>
</tr>
{% endfor %}
</table>
</div>
<div class="tab-pane" id="{{racer.id}}-last_season">
<table width = "80%">
<tr>
<th>{{ _('Time') }}</th>
<th>{{ _('Car') }}</th>
</tr>
{% for lap in racer.fastest_laps(season=last_season) %}
<tr>
<td>{{ lap.formatted_time() }}</td>
<td>{{ lap.car().name }}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</li>
</ul>
</div>
Expand Down
31 changes: 31 additions & 0 deletions app/templates/season_registration.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}

{% block app_content %}
<h1>{{ _('Create a new season') }}</h1>
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p>
{{ form.started_at.label }}<br>
{{ form.started_at }}
{% for error in form.started_at.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>
{{ form.ended_at.label }}<br>
{{ form.ended_at }}
{% for error in form.ended_at.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>
{{ form.description.label }}<br>
{{ form.description(size=64) }}
{% for error in form.description.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>{{ form.submit() }}</p>
</form>
{% endblock %}
22 changes: 22 additions & 0 deletions app/templates/seasons.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends "base.html" %}
{% block app_content %}

<h1>{{ _('Seasons') }}</h1>

<a href="{{ url_for('season_registration') }}">{{ _('Create a new season') }}</a>

{% if seasons %}
<div class = "season">
<div class = "season-data">
<ul>
{% for season in seasons %}
<li>
<p> {{ season.started_at.strftime('%d.%m.%Y') }} - {% if season.ended_at != None %}{{ season.ended_at.strftime('%d.%m.%Y') }}{% endif %}: {{ season.description }}</p>
</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}

{% endblock %}
Binary file modified app/translations/de/LC_MESSAGES/messages.mo
Binary file not shown.
Loading