From 909dd31ebd0ad4f8cd478c4bba9090e05c58d4f6 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand <benjamin.bertrand@esss.se> Date: Wed, 13 Dec 2017 11:16:28 +0100 Subject: [PATCH] Clean create_host view Use the session instead of URL parameters to store the last network chosen. --- app/main/views.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/main/views.py b/app/main/views.py index 867225c..7decb19 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -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) -- GitLab