adds create and delete vehicle
This commit is contained in:
parent
d19b0a5858
commit
267c59cbb3
36
app/main.py
36
app/main.py
@ -129,6 +129,10 @@ class EditVehicleForm(Form):
|
||||
name = StringField('Name', validators=[Length(1, 255)])
|
||||
|
||||
|
||||
class DeleteVehicleForm(Form):
|
||||
pass
|
||||
|
||||
|
||||
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
|
||||
security = Security(app, user_datastore)
|
||||
|
||||
@ -181,6 +185,35 @@ def edit_vehicle(vid):
|
||||
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'])
|
||||
@login_required
|
||||
def create_pit_stop_form():
|
||||
@ -190,6 +223,7 @@ def create_pit_stop_form():
|
||||
|
||||
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()
|
||||
@ -199,8 +233,6 @@ def create_pit_stop_form():
|
||||
db.session.commit()
|
||||
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.litres.default = last_pitstop.litres
|
||||
form.date.default = date.today()
|
||||
|
@ -5,11 +5,13 @@
|
||||
|
||||
<a href='{{ url_for('security.change_password') }}'>Change password</a>
|
||||
|
||||
<a href="{{ url_for('create_vehicle') }}">create</a>
|
||||
<table>
|
||||
{% for vehicle in current_user.vehicles %}
|
||||
<tr>
|
||||
<td>{{vehicle.name}}</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>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
12
app/templates/createVehicleForm.html
Normal file
12
app/templates/createVehicleForm.html
Normal 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 %}
|
11
app/templates/deleteVehicleForm.html
Normal file
11
app/templates/deleteVehicleForm.html
Normal 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 %}
|
@ -1,6 +1,7 @@
|
||||
{% extends "layout.html" %}
|
||||
|
||||
{% block body %}
|
||||
<h1>Edit vehicle</h1>
|
||||
<form class='form-horizontal' method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field_with_errors(form.name) }}
|
||||
|
Loading…
Reference in New Issue
Block a user