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

Add tags to register host form

parent fd3109c4
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,7 @@ This module defines the network blueprint forms.
"""
from flask_wtf import FlaskForm
from wtforms import (SelectField, StringField, TextAreaField,
IntegerField, BooleanField, validators)
SelectMultipleField, BooleanField, validators)
from .. import utils, models
......@@ -65,9 +65,11 @@ class HostForm(FlaskForm):
validators=[validators.InputRequired(), validators.Regexp(models.HOST_NAME_RE)],
filters=[utils.lowercase_field])
mac_id = SelectField('MAC', coerce=utils.coerce_to_str_or_none)
tags = SelectMultipleField('Tags', coerce=utils.coerce_to_str_or_none)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.item_id.choices = utils.get_model_choices(models.Item, allow_none=True, attr='ics_id')
self.network_id.choices = utils.get_model_choices(models.Network, allow_none=False, attr='vlan_name')
self.mac_id.choices = utils.get_model_choices(models.Mac, allow_none=True, attr='address')
self.tags.choices = utils.get_model_choices(models.Tag, allow_none=True, attr='name')
......@@ -46,11 +46,17 @@ def create_host():
type=form.type.data,
description=form.description.data or None,
item_id=form.item_id.data)
# The total number of tags will always be quite small
# It's more efficient to retrieve all of them in one query
# and do the filtering here
all_tags = models.Tag.query.all()
tags = [tag for tag in all_tags if str(tag.id) in form.tags.data]
host.interfaces = [
models.Interface(name=form.interface_name.data,
ip=form.ip.data,
network_id=network_id,
mac_id=form.mac_id.data)
mac_id=form.mac_id.data,
tags=tags)
]
current_app.logger.debug(f'Trying to create: {host!r}')
db.session.add(host)
......
......@@ -25,6 +25,7 @@
{{ render_field(form.ip) }}
{{ render_field(form.interface_name, class_="text-lowercase") }}
{{ render_field(form.mac_id) }}
{{ render_field(form.tags) }}
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Submit</button>
......
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