diff --git a/app/rollerverbrauch/__init__.py b/app/rollerverbrauch/__init__.py index e2ddb9f..cc496a8 100644 --- a/app/rollerverbrauch/__init__.py +++ b/app/rollerverbrauch/__init__.py @@ -27,7 +27,8 @@ mail = Mail(app) from rollerverbrauch.forms import \ CreatePitstopForm, \ EditVehicleForm, \ - DeleteVehicleForm + DeleteVehicleForm, \ + SelectVehicleForm from rollerverbrauch.entities import \ User, \ @@ -122,22 +123,41 @@ def create_vehicle(): return render_template('createVehicleForm.html', form=form) -@app.route('/pitstops/createForm', methods=['GET', 'POST']) +@app.route('/pitstops/select_vehicle', methods=['GET', 'POST']) @login_required -def create_pit_stop_form(): - last_pitstop = Pitstop.query.order_by(Pitstop.id.desc()).first() - if last_pitstop is None: +def select_vehicle_for_new_pitstop(): + form = SelectVehicleForm() + form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles] + + if form.validate_on_submit(): + vehicle = Vehicle.query.filter(Vehicle.id == form.vehicle.data).first() + if not vehicle in current_user.vehicles: + return render_template('selectVehice.html', form=form) + + return redirect(url_for('create_pit_stop_form', vid=form.vehicle.data)) + + return render_template('selectVehice.html', form=form) + + +@app.route('/pitstops/create/', methods=['GET', 'POST']) +@login_required +def create_pit_stop_form(vid): + vehicle = Vehicle.query.filter(Vehicle.id == vid).first() + if not vehicle in current_user.vehicles: + return redirect(url_for('select_vehicle_for_new_pitstop')) + + if len(vehicle.pitstops) > 0: + last_pitstop = vehicle.pitstops[-1] + else: last_pitstop = Pitstop(0, 0, date.today()) form = CreatePitstopForm() form.set_pitstop(last_pitstop) - form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles] if form.validate_on_submit(): - v = Vehicle.query.filter(Vehicle.id == form.vehicle.data).first() new_stop = Pitstop(form.odometer.data, form.litres.data, form.date.data) db.session.add(new_stop) - v.pitstops.append(new_stop) + vehicle.pitstops.append(new_stop) db.session.commit() return redirect(url_for('get_pit_stops')) @@ -145,7 +165,7 @@ def create_pit_stop_form(): form.litres.default = last_pitstop.litres form.date.default = date.today() form.process() - return render_template('newPitStopForm.html', form=form) + return render_template('newPitStopForm.html', form=form, vehicle = vehicle) @app.route('/pitstops', methods=['GET']) diff --git a/app/rollerverbrauch/forms.py b/app/rollerverbrauch/forms.py index a5402b2..d8a708f 100644 --- a/app/rollerverbrauch/forms.py +++ b/app/rollerverbrauch/forms.py @@ -18,8 +18,11 @@ def litres_check(form, field): raise ValidationError('You must fuel at least 0.1 l') -class CreatePitstopForm(Form): +class SelectVehicleForm(Form): vehicle = SelectField('Vehicle', coerce=int) + + +class CreatePitstopForm(Form): date = DateField('Date of Pitstop', validators=[date_check]) odometer = IntegerField('Odometer (km)', validators=[odometer_check]) litres = DecimalField('Litres (l)', places=1, validators=[litres_check]) @@ -35,7 +38,3 @@ class EditVehicleForm(Form): class DeleteVehicleForm(Form): pass - - - - diff --git a/app/rollerverbrauch/templates/account.html b/app/rollerverbrauch/templates/account.html index 0feafd5..8cec4de 100644 --- a/app/rollerverbrauch/templates/account.html +++ b/app/rollerverbrauch/templates/account.html @@ -1,18 +1,56 @@ {% extends "layout.html" %} {% block body %} -

Account management for {{current_user.email}}

- - Change password - - create - - {% for vehicle in current_user.vehicles %} +

Account management for {{current_user.email}}

+
+
Password
+ +
+
+
Vehicles
+ +
- - - + + + - {% endfor %} -
{{vehicle.name}}editdelete + Vehicle + + Info + + Actions +
+ {% for vehicle in current_user.vehicles %} + + + {{ vehicle.name }} + + + {{ vehicle.pitstops | length }} pitstops + + + + edit + + {% if current_user.vehicles | length > 1 %} + + delete + + {% else %} +   + {% endif %} + + + {% endfor %} + + {% endblock %} diff --git a/app/rollerverbrauch/templates/admin.html b/app/rollerverbrauch/templates/admin.html index 97c2db5..d57f4da 100644 --- a/app/rollerverbrauch/templates/admin.html +++ b/app/rollerverbrauch/templates/admin.html @@ -1,7 +1,7 @@ {% extends "layout.html" %} {% block body %} -

Admin

+

Admin

We have {{ data.users|length }} users so far: