adds create and delete vehicle

This commit is contained in:
Joachim Lusiardi 2016-04-23 17:11:43 +02:00
parent d19b0a5858
commit 267c59cbb3
5 changed files with 60 additions and 2 deletions

View File

@ -129,6 +129,10 @@ class EditVehicleForm(Form):
name = StringField('Name', validators=[Length(1, 255)]) name = StringField('Name', validators=[Length(1, 255)])
class DeleteVehicleForm(Form):
pass
user_datastore = SQLAlchemyUserDatastore(db, User, Role) user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore) security = Security(app, user_datastore)
@ -181,6 +185,35 @@ def edit_vehicle(vid):
return render_template('editVehicleForm.html', form=form) return render_template('editVehicleForm.html', form=form)
@app.route('/account/delete_vehicle/<int:vid>', methods=['GET', 'POST'])
@login_required
def delete_vehicle(vid):
vehicle = Vehicle.query.filter(Vehicle.id == vid).first()
form = DeleteVehicleForm()
if form.validate_on_submit():
db.session.delete(vehicle)
db.session.commit()
return redirect(url_for('get_account_page'))
return render_template('deleteVehicleForm.html', form=form, vehicle=vehicle)
@app.route('/account/create_vehicle', methods=['GET', 'POST'])
@login_required
def create_vehicle():
form = EditVehicleForm()
if form.validate_on_submit():
new_vehicle = Vehicle(form.name.data)
db.session.add(new_vehicle)
current_user.vehicles.append(new_vehicle)
db.session.commit()
return redirect(url_for('get_account_page'))
return render_template('createVehicleForm.html', form=form)
@app.route('/pitstops/createForm', methods=['GET', 'POST']) @app.route('/pitstops/createForm', methods=['GET', 'POST'])
@login_required @login_required
def create_pit_stop_form(): def create_pit_stop_form():
@ -190,6 +223,7 @@ def create_pit_stop_form():
form = CreatePitstopForm() form = CreatePitstopForm()
form.set_pitstop(last_pitstop) form.set_pitstop(last_pitstop)
form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles]
if form.validate_on_submit(): if form.validate_on_submit():
v = Vehicle.query.filter(Vehicle.id == form.vehicle.data).first() v = Vehicle.query.filter(Vehicle.id == form.vehicle.data).first()
@ -199,8 +233,6 @@ def create_pit_stop_form():
db.session.commit() db.session.commit()
return redirect(url_for('get_pit_stops')) return redirect(url_for('get_pit_stops'))
# dynamically set values
form.vehicle.choices = [(g.id, g.name) for g in current_user.vehicles]
form.odometer.default = last_pitstop.odometer form.odometer.default = last_pitstop.odometer
form.litres.default = last_pitstop.litres form.litres.default = last_pitstop.litres
form.date.default = date.today() form.date.default = date.today()

View File

@ -5,11 +5,13 @@
<a href='{{ url_for('security.change_password') }}'>Change password</a> <a href='{{ url_for('security.change_password') }}'>Change password</a>
<a href="{{ url_for('create_vehicle') }}">create</a>
<table> <table>
{% for vehicle in current_user.vehicles %} {% for vehicle in current_user.vehicles %}
<tr> <tr>
<td>{{vehicle.name}}</td> <td>{{vehicle.name}}</td>
<td><a href="{{ url_for('edit_vehicle', vid=vehicle.id) }}">edit</a></td> <td><a href="{{ url_for('edit_vehicle', vid=vehicle.id) }}">edit</a></td>
<td><a href="{{ url_for('delete_vehicle', vid=vehicle.id) }}">delete</a></td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>

View File

@ -0,0 +1,12 @@
{% extends "layout.html" %}
{% block body %}
<h1>Create vehicle</h1>
<form class='form-horizontal' method="POST">
{{ form.hidden_tag() }}
{{ render_field_with_errors(form.name) }}
<input type="submit" value="Go">
</form>
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends "layout.html" %}
{% block body %}
<h1>Delete vehicle "{{vehicle.name}}"</h1>
<form class='form-horizontal' method="POST">
{{ form.hidden_tag() }}
<input type="submit" value="Go">
</form>
{% endblock %}

View File

@ -1,6 +1,7 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block body %} {% block body %}
<h1>Edit vehicle</h1>
<form class='form-horizontal' method="POST"> <form class='form-horizontal' method="POST">
{{ form.hidden_tag() }} {{ form.hidden_tag() }}
{{ render_field_with_errors(form.name) }} {{ render_field_with_errors(form.name) }}