Skip to content
Snippets Groups Projects
Commit 0bdf40c2 authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Save the device_type_id in the session

Allow to select by default the last chosen device_type when creating a
new host.

JIRA INFRA-987 #action In Progress
parent 9e500391
No related branches found
No related tags found
No related merge requests found
...@@ -62,12 +62,11 @@ def create_host(): ...@@ -62,12 +62,11 @@ def create_host():
kwargs = {"random_mac": True} kwargs = {"random_mac": True}
# Try to get the network_id from the session # Try to get the network_id from the session
# to pre-fill the form with the same network # to pre-fill the form with the same network
try: if session.get("network_id"):
network_id = session["network_id"] kwargs["network_id"] = session["network_id"]
except KeyError: # Same for the device_type
pass if session.get("device_type_id"):
else: kwargs["device_type_id"] = session["device_type_id"]
kwargs["network_id"] = network_id
form = HostInterfaceForm(request.form, **kwargs) form = HostInterfaceForm(request.form, **kwargs)
# Remove the host_id field inherited from the InterfaceForm # Remove the host_id field inherited from the InterfaceForm
# It's not used in this form # It's not used in this form
...@@ -75,6 +74,7 @@ def create_host(): ...@@ -75,6 +74,7 @@ def create_host():
# First interface name shall be identical to host name # First interface name shall be identical to host name
del form.interface_name del form.interface_name
if form.validate_on_submit(): if form.validate_on_submit():
device_type_id = form.device_type_id.data
network_id = form.network_id.data network_id = form.network_id.data
network = models.Network.query.get(network_id) network = models.Network.query.get(network_id)
if not current_user.has_access_to_network(network): if not current_user.has_access_to_network(network):
...@@ -85,7 +85,7 @@ def create_host(): ...@@ -85,7 +85,7 @@ def create_host():
try: try:
host = models.Host( host = models.Host(
name=form.name.data, 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, is_ioc=form.is_ioc.data,
description=form.description.data or None, description=form.description.data or None,
ansible_vars=form.ansible_vars.data or None, ansible_vars=form.ansible_vars.data or None,
...@@ -116,8 +116,9 @@ def create_host(): ...@@ -116,8 +116,9 @@ def create_host():
flash(f"{e}", "error") flash(f"{e}", "error")
else: else:
flash(f"Host {host} created!", "success") 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["network_id"] = network_id
session["device_type_id"] = device_type_id
return redirect(url_for("network.view_host", name=host.name)) return redirect(url_for("network.view_host", name=host.name))
return render_template("network/create_host.html", form=form) return render_template("network/create_host.html", form=form)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment