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

Clean create_host view

Use the session instead of URL parameters to store the last network
chosen.
parent febf5fa9
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,7 @@ This module implements the main blueprint.
"""
import sqlalchemy as sa
from flask import (Blueprint, render_template, jsonify,
from flask import (Blueprint, render_template, jsonify, session,
redirect, url_for, request, flash, current_app)
from flask_login import login_required
from .forms import AttributeForm, HostForm, ItemForm
......@@ -156,17 +156,17 @@ def list_hosts():
@bp.route('/hosts/create', methods=('GET', 'POST'))
@login_groups_accepted('admin', 'create')
def create_host():
# Try to get the network_id from the URL parameters
# to display the form with the same selected network
# when reloading the page (redirect after submit)
# Try to get the network_id from the session
# to pre-fill the form with the same network
try:
network_id = request.args['network_id']
network_id = session['network_id']
except KeyError:
# No need to pass request.form when no extra keywords are given
form = HostForm()
else:
form = HostForm(request.form, network_id=network_id)
if form.validate_on_submit():
network_id = form.network_id.data
host = models.Host(ip=form.ip.data,
network_id=form.network_id.data,
name=form.name.data,
......@@ -182,7 +182,9 @@ def create_host():
flash(f'{e}', 'error')
else:
flash(f'Host {host} created!', 'success')
return redirect(url_for('main.create_host', network_id=host.network_id))
# Save network_id to the session to retrieve it after the redirect
session['network_id'] = host.network_id
return redirect(url_for('main.create_host'))
return render_template('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