def add_vehicle(): """Add a new vehicle to the fleet.""" form = VehicleForm() if form.validate_on_submit(): vehicle_type = form.vehicle_type.data try: new_info = movr.add_vehicle(vehicle_type=vehicle_type, longitude=form.longitude.data, latitude=form.latitude.data, battery=form.battery.data) except IntegrityError as e: return render_error_page(e, movr) vehicle_id = new_info['vehicle_id'] # check to verify that vehicle was added new_vehicle = movr.get_vehicle(vehicle_id) if new_vehicle is None: # Insert didn't work flash(("Vehicle with id `{}` " "NOT successfully added. Edit add_vehicle_txn in " "movr/transactions.py to add the vehicle to the database." ).format(vehicle_id)) redirect(url_for('add_vehicle', _external=True)) else: # Inserted vehicle was found flash('Vehicle added! \nid: {}'.format(vehicle_id)) return redirect( url_for('vehicle', vehicle_id=vehicle_id, _external=True)) # form not properly filled out yet return render_template('add_vehicle.html', title='Add a vehicle', form=form)
def remove_vehicle(vehicle_id): """Delete a vehicle from the database.""" vehicle_deleted = movr.remove_vehicle(vehicle_id) if vehicle_deleted: # Vehicle looks like it was deleted # Verify that it was actually deleted as required by the lab. if movr.get_vehicle(vehicle_id) is None: # Vehicle looks deleted flash("Deleted vehicle with id " "`{id}` from database.".format(id=vehicle_id)) return redirect(url_for('vehicles', _external=True)) # else vehicle is still in the database. flash(("Vehicle `{}` not successfully deleted. Please modify " "remove_vehicle_txn in `movr/transactions.py` to implement " "the correct functionality." ).format(vehicle_id)) return redirect(url_for('vehicle', vehicle_id=vehicle_id, _external=True)) elif vehicle_deleted is None: # Vehicle in use or not in database flash(("Vehicle `{}` not found in database or not in use. " "Cannot delete it.").format(vehicle_id)) return redirect(url_for('vehicles', _external=True)) return render_error_page(RuntimeError( ("Attempt to remove vehicle hit unexpected state. " "Please notify [email protected] via email of this error " "message. Attempted to remove vehicle `{vehicle_id}`. " "Current row state is `{row}`." ).format(vehicle_id=vehicle_id, row=movr.get_vehicle(vehicle_id))), movr)
def ride(vehicle_id): """ Show the user the form to end a ride. """ form = EndRideForm() vehicle_at_start = movr.get_vehicle(vehicle_id) if vehicle_at_start is None: # Vehicle not found in database flash("Vehicle `{}` not found.".format(vehicle_id)) return redirect(url_for('vehicles', _external=True)) elif not vehicle_at_start['in_use']: # Ride hasn't started. flash("Cannot view the ride for this vehicle. It is not currently in " "use.") return redirect(url_for('vehicle', vehicle_id=vehicle_id, _external=True)) if form.validate_on_submit(): try: if movr.end_ride(vehicle_id, form.longitude.data, form.latitude.data, form.battery.data): vehicle_at_end = movr.get_vehicle(vehicle_id) for message in generate_end_ride_messages(vehicle_at_start, vehicle_at_end): flash(message) return redirect(url_for('vehicle', vehicle_id=vehicle_id, _external=True)) # else: end_ride didn't work flash("Unable to end ride for vehicle `{id}`.".format(id=vehicle_id)) return redirect(url_for('ride', vehicle_id=vehicle_id, _external=True)) except ValueError as e: return render_error_page(e, movr) return render_template('ride.html', title=('Riding a {}' ).format(vehicle_at_start["vehicle_type"]), form=form, vehicle=vehicle_at_start, _external=True)
def vehicles(max_vehicles=_MAX_RECORDS): """ Shows the vehicles page, listing all vehicles. """ try: start_ride_form = StartRideForm() see_vehicle_form = SeeVehicleForm() some_vehicles = movr.get_vehicles(max_vehicles=max_vehicles) return render_template('vehicles.html', title='Vehicles', vehicles=some_vehicles, start_ride_form=start_ride_form, see_vehicle_form=see_vehicle_form) except ProgrammingError as error: return render_error_page(error, movr)