diff --git a/app/network/views.py b/app/network/views.py index fcef45e6ab647cfe670ac94a719075562f695c0d..3a8aa6caaadcf60199763d1ef5a715dbcadcb67c 100644 --- a/app/network/views.py +++ b/app/network/views.py @@ -62,12 +62,11 @@ def create_host(): kwargs = {"random_mac": True} # Try to get the network_id from the session # to pre-fill the form with the same network - try: - network_id = session["network_id"] - except KeyError: - pass - else: - kwargs["network_id"] = network_id + if session.get("network_id"): + kwargs["network_id"] = session["network_id"] + # Same for the device_type + if session.get("device_type_id"): + kwargs["device_type_id"] = session["device_type_id"] form = HostInterfaceForm(request.form, **kwargs) # Remove the host_id field inherited from the InterfaceForm # It's not used in this form @@ -75,6 +74,7 @@ def create_host(): # First interface name shall be identical to host name del form.interface_name if form.validate_on_submit(): + device_type_id = form.device_type_id.data network_id = form.network_id.data network = models.Network.query.get(network_id) if not current_user.has_access_to_network(network): @@ -85,7 +85,7 @@ def create_host(): try: host = models.Host( name=form.name.data, - device_type=models.DeviceType.query.get(form.device_type_id.data), + device_type=models.DeviceType.query.get(device_type_id), is_ioc=form.is_ioc.data, description=form.description.data or None, ansible_vars=form.ansible_vars.data or None, @@ -116,8 +116,9 @@ def create_host(): flash(f"{e}", "error") else: flash(f"Host {host} created!", "success") - # Save network_id to the session to retrieve it after the redirect + # Save network_id and device_type_id to the session to retrieve them after the redirect session["network_id"] = network_id + session["device_type_id"] = device_type_id return redirect(url_for("network.view_host", name=host.name)) return render_template("network/create_host.html", form=form)