From a0be1221869cfa2d78b5be0d33a2920d53d62efa Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Mon, 27 Jun 2016 22:09:42 +0200 Subject: [PATCH] reworked the crud funtions for vehicles --- app/rollerverbrauch/__init__.py | 49 +++++++++++++------ .../{selectVehice.html => selectVehicle.html} | 0 2 files changed, 33 insertions(+), 16 deletions(-) rename app/rollerverbrauch/templates/{selectVehice.html => selectVehicle.html} (100%) diff --git a/app/rollerverbrauch/__init__.py b/app/rollerverbrauch/__init__.py index 1d01bde..350e46e 100644 --- a/app/rollerverbrauch/__init__.py +++ b/app/rollerverbrauch/__init__.py @@ -110,29 +110,37 @@ def index(): return render_template('index.html', login_user_form=LoginForm(), data=data) -@app.route('/account/edit_vehicle/', methods=['GET', 'POST']) +@app.route('/account/vehicle/edit/', methods=['GET', 'POST']) @login_required def edit_vehicle(vid): vehicle = Vehicle.query.filter(Vehicle.id == vid).first() + + # prevent edit of foreign vehicles + if vehicle not in current_user.vehicles: + return redirect(url_for('get_account_page')) + form = EditVehicleForm() - #form.consumables.choices = [(g.id, '%s (%s)' % (g.name, g.unit)) for g in Consumable.query.all()] + + if form.name.data is not None: + form.name.default = form.name.data if form.validate_on_submit(): - if not tools.check_vehicle_name_is_unique(current_user, form.name): - return render_template('editVehicleForm.html', form=form) vehicle.name = form.name.data - db.session.commit() - tools.db_log_update(vehicle) + try: + db.session.commit() + tools.db_log_update(vehicle) + except IntegrityError: + db.session.rollback() + form.name.errors.append('"%s" is not unique.' % (form.name.data)) + return render_template('editVehicleForm.html', form=form) return redirect(url_for('get_account_page')) form.name.default = vehicle.name - #form.consumables.default = [g.id for g in vehicle.consumables] - #print(form.consumables.name) form.process() return render_template('editVehicleForm.html', form=form, vehicle=vehicle) -@app.route('/account/delete_vehicle/', methods=['GET', 'POST']) +@app.route('/account/vehicle/delete/', methods=['GET', 'POST']) @login_required def delete_vehicle(vid): vehicle = Vehicle.query.filter(Vehicle.id == vid).first() @@ -141,6 +149,9 @@ def delete_vehicle(vid): if vehicle not in current_user.vehicles: return redirect(url_for('get_account_page')) + if len(current_user.vehicles) == 1: + return redirect(url_for('get_account_page')) + form = DeleteVehicleForm() if form.validate_on_submit(): @@ -152,20 +163,26 @@ def delete_vehicle(vid): return render_template('deleteVehicleForm.html', form=form, vehicle=vehicle) -@app.route('/account/create_vehicle', methods=['GET', 'POST']) +@app.route('/account/vehicle/create', methods=['GET', 'POST']) @login_required def create_vehicle(): form = EditVehicleForm() + if form.name.data is not None: + form.name.default = form.name.data + if form.validate_on_submit(): vehicle_name = form.name.data - if not tools.check_vehicle_name_is_unique(current_user, form.name): - return render_template('createVehicleForm.html', form=form) new_vehicle = Vehicle(vehicle_name) db.session.add(new_vehicle) current_user.vehicles.append(new_vehicle) - db.session.commit() - tools.db_log_add(new_vehicle) + try: + db.session.commit() + tools.db_log_add(new_vehicle) + except IntegrityError: + db.session.rollback() + form.name.errors.append('"%s" is not unique.' % (form.name.data)) + return render_template('createVehicleForm.html', form=form) return redirect(url_for('get_account_page')) return render_template('createVehicleForm.html', form=form) @@ -180,11 +197,11 @@ def select_vehicle_for_new_pitstop(): if form.validate_on_submit(): vehicle = Vehicle.query.filter(Vehicle.id == form.vehicle.data).first() if vehicle not in current_user.vehicles: - return render_template('selectVehice.html', form=form) + return render_template('selectVehicle.html', form=form) return redirect(url_for('create_pit_stop_form', vid=form.vehicle.data)) - return render_template('selectVehice.html', form=form) + return render_template('selectVehicle.html', form=form) @app.route('/pitstops/create/', methods=['GET', 'POST']) diff --git a/app/rollerverbrauch/templates/selectVehice.html b/app/rollerverbrauch/templates/selectVehicle.html similarity index 100% rename from app/rollerverbrauch/templates/selectVehice.html rename to app/rollerverbrauch/templates/selectVehicle.html