diff --git a/app/network/forms.py b/app/network/forms.py index 5af7ca6e0d46d3749fbb46d6d49817a88022f1fa..b82c482ca76914485e3babed083b784ec8111d60 100644 --- a/app/network/forms.py +++ b/app/network/forms.py @@ -18,6 +18,21 @@ from ..validators import (Unique, RegexpList, IPNetwork, HOST_NAME_RE, from .. import utils, models +def starts_with_hostname(form, field): + """Check that interface name starts with hostname""" + try: + # Create / Edit interface form + host_id_field = form['host_id'] + except KeyError: + # Create host form + hostname = form['name'].data + else: + host = models.Host.query.get(host_id_field.data) + hostname = host.name + if not field.data.startswith(hostname): + raise validators.ValidationError(f'Interface name shall start with the hostname "{hostname}"') + + def validate_tags(form, field): choices = dict(field.choices) for tag_id in field.data: @@ -115,7 +130,8 @@ class InterfaceForm(CSEntryForm): description='name must be 2-20 characters long and contain only letters, numbers and dash', validators=[validators.InputRequired(), validators.Regexp(HOST_NAME_RE), - Unique(models.Interface)], + Unique(models.Interface), + starts_with_hostname], filters=[utils.lowercase_field]) mac_address = StringField( 'MAC', diff --git a/app/static/js/hosts.js b/app/static/js/hosts.js index c9bf938015d63deffd7ed93efc1e6268dd4c8183..4c4b976d8bc9cd9e6d5141c2dcb81bf19ff30e93 100644 --- a/app/static/js/hosts.js +++ b/app/static/js/hosts.js @@ -48,6 +48,10 @@ $(document).ready(function() { $("#interface_name").val(hostname); }); + // Force the first interface to have the hostname as name + // This only applies to the hostForm (not when editing or adding interfaces) + $("#hostForm input[name=interface_name]").prop("readonly", true); + var hosts_table = $("#hosts_table").DataTable({ "ajax": function(data, callback, settings) { $.getJSON(