reworked the crud funtions for vehicles

This commit is contained in:
Joachim Lusiardi 2016-06-27 22:09:42 +02:00
parent fa4126be66
commit a0be122186
2 changed files with 33 additions and 16 deletions

View File

@ -110,29 +110,37 @@ def index():
return render_template('index.html', login_user_form=LoginForm(), data=data)
@app.route('/account/edit_vehicle/<int:vid>', methods=['GET', 'POST'])
@app.route('/account/vehicle/edit/<int:vid>', 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/<int:vid>', methods=['GET', 'POST'])
@app.route('/account/vehicle/delete/<int:vid>', 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/<int:vid>', methods=['GET', 'POST'])