From 267c59cbb35542371ba833642e0c969744a615b3 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sat, 23 Apr 2016 17:11:43 +0200 Subject: [PATCH] adds create and delete vehicle --- app/main.py | 36 ++++++++++++++++++++++++++-- app/templates/account.html | 2 ++ app/templates/createVehicleForm.html | 12 ++++++++++ app/templates/deleteVehicleForm.html | 11 +++++++++ app/templates/editVehicleForm.html | 1 + 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 app/templates/createVehicleForm.html create mode 100644 app/templates/deleteVehicleForm.html diff --git a/app/main.py b/app/main.py index 3db885a..8038a9a 100644 --- a/app/main.py +++ b/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/', 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() diff --git a/app/templates/account.html b/app/templates/account.html index d111351..0feafd5 100644 --- a/app/templates/account.html +++ b/app/templates/account.html @@ -5,11 +5,13 @@ Change password + create {% for vehicle in current_user.vehicles %} + {% endfor %}
{{vehicle.name}} editdelete
diff --git a/app/templates/createVehicleForm.html b/app/templates/createVehicleForm.html new file mode 100644 index 0000000..97702be --- /dev/null +++ b/app/templates/createVehicleForm.html @@ -0,0 +1,12 @@ +{% extends "layout.html" %} + +{% block body %} +

Create vehicle

+
+ {{ form.hidden_tag() }} + {{ render_field_with_errors(form.name) }} + +
+ + +{% endblock %} diff --git a/app/templates/deleteVehicleForm.html b/app/templates/deleteVehicleForm.html new file mode 100644 index 0000000..c39cd4d --- /dev/null +++ b/app/templates/deleteVehicleForm.html @@ -0,0 +1,11 @@ +{% extends "layout.html" %} + +{% block body %} +

Delete vehicle "{{vehicle.name}}"

+
+ {{ form.hidden_tag() }} + +
+ + +{% endblock %} diff --git a/app/templates/editVehicleForm.html b/app/templates/editVehicleForm.html index 09360ac..fe6b055 100644 --- a/app/templates/editVehicleForm.html +++ b/app/templates/editVehicleForm.html @@ -1,6 +1,7 @@ {% extends "layout.html" %} {% block body %} +

Edit vehicle

{{ form.hidden_tag() }} {{ render_field_with_errors(form.name) }}