From bf044bd7a895e4564e6825b50e67a120c3f1313e Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Thu, 24 May 2018 16:49:02 +0200
Subject: [PATCH] Fix default selected tags in edit interface view

Passing tags as kwarg to the InterfaceForm doesn't work because
obj takes precedence (but interface.tags contain Tag instances and not id)
We need to update the default values. Calling process is required.
See https://stackoverflow.com/questions/5519729/wtforms-how-to-select-options-in-selectmultiplefield

JIRA INFRA-334
---
 app/network/views.py | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/app/network/views.py b/app/network/views.py
index e6cc053..4f639de 100644
--- a/app/network/views.py
+++ b/app/network/views.py
@@ -175,6 +175,12 @@ def edit_interface(name):
     ips = [interface.ip]
     ips.extend([str(address) for address in interface.network.available_ips()])
     form.ip.choices = utils.get_choices(ips)
+    # Passing tags as kwarg to the InterfaceForm doesn't work because
+    # obj takes precedence (but interface.tags contain Tag instances and not id)
+    # We need to update the default values. Calling process is required.
+    # See https://stackoverflow.com/questions/5519729/wtforms-how-to-select-options-in-selectmultiplefield
+    form.tags.default = [tag.id for tag in interface.tags]
+    form.tags.process(request.form)
     if form.validate_on_submit():
         interface.name = form.interface_name.data
         interface.ip = form.ip.data
-- 
GitLab