diff --git a/app/models.py b/app/models.py index 89d11fe34636f24fbf48687167ec99b6613fd311..62de3ca86832b1b5c064e8531e468f89a94c2be8 100644 --- a/app/models.py +++ b/app/models.py @@ -1031,7 +1031,7 @@ class AnsibleGroup(CreatedMixin, db.Model): "name": self.name, "vars": self.vars, "type": self.type.name, - "hosts": [str(host) for host in self.hosts], + "hosts": [host.fqdn for host in self.hosts], "children": [str(child) for child in self.children], } ) diff --git a/app/network/forms.py b/app/network/forms.py index a8ea9defff94cf96233dd9b9716a9379f95e6f70..b91962afbf2da4b242a639fe39299f33a7d51936 100644 --- a/app/network/forms.py +++ b/app/network/forms.py @@ -265,4 +265,6 @@ class AnsibleGroupForm(CSEntryForm): self.children.choices = utils.get_model_choices( models.AnsibleGroup, attr="name" ) - self.hosts.choices = utils.get_model_choices(models.Host, attr="name") + self.hosts.choices = utils.get_model_choices( + models.Host, attr="fqdn", order_by="name" + ) diff --git a/app/templates/_helpers.html b/app/templates/_helpers.html index d2010409846dbcf9e5aaaf10528313a7a74cf3cf..4c1f01af4ec88e31269682a56121bb5b7a4f9a1d 100644 --- a/app/templates/_helpers.html +++ b/app/templates/_helpers.html @@ -39,13 +39,13 @@ {% if active %}active{% endif %} {%- endmacro %} -{% macro link_to_host(name) -%} - <a href="{{ url_for('network.view_host', name=name) }}">{{ name }}</a> +{% macro link_to_host(host) -%} + <a href="{{ url_for('network.view_host', name=host.name) }}">{{ host.fqdn|default(host.name) }}</a> {%- endmacro %} {% macro link_to_hosts(hosts) -%} {% for host in hosts %} - {{ link_to_host(host.name) }} + {{ link_to_host(host) }} {% endfor %} {%- endmacro %} diff --git a/app/utils.py b/app/utils.py index 6dc8d715c62d7b8e456ccd478ecb04a4dfb4e98a..bda66893cc8a93ee119fbe6e200c6c33ac5873b7 100644 --- a/app/utils.py +++ b/app/utils.py @@ -130,14 +130,14 @@ def get_choices(iterable, allow_blank=False, allow_null=False): return choices -def get_model_choices(model, allow_none=False, attr="name", query=None): +def get_model_choices(model, allow_none=False, attr="name", query=None, order_by=None): """Return a list of (value, label)""" choices = [] if allow_none: choices = [(None, "")] if query is None: query = model.query - query = query.order_by(getattr(model, attr)) + query = query.order_by(getattr(model, order_by or attr)) choices.extend( [(str(instance.id), getattr(instance, attr)) for instance in query.all()] )