diff --git a/app/network/forms.py b/app/network/forms.py index dc750f6e83f4f00f360ad3280be4ce9670162e90..f3ba1e8d3e58d129dc8299ec76d46711f7a52281 100644 --- a/app/network/forms.py +++ b/app/network/forms.py @@ -9,6 +9,7 @@ This module defines the network blueprint forms. :license: BSD 2-Clause, see LICENSE for more details. """ +from flask_login import current_user from flask_wtf import FlaskForm from wtforms import (SelectField, StringField, TextAreaField, SelectMultipleField, BooleanField, validators) @@ -82,6 +83,11 @@ class HostForm(FlaskForm): 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') + if current_user.is_admin: + network_query = models.Network.query + else: + network_query = models.Network.query.filter(models.Network.admin_only.is_(False)) + self.network_id.choices = utils.get_model_choices(models.Network, allow_none=False, + attr='vlan_name', query=network_query) 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') diff --git a/app/utils.py b/app/utils.py index 232cc52680ec3acf1e45ebd56f84f2782b1a988c..67128fa46128d1bb0f45203615c5f7eccc7cabac 100644 --- a/app/utils.py +++ b/app/utils.py @@ -107,12 +107,14 @@ def get_choices(iterable, allow_blank=False, allow_null=False): return choices -def get_model_choices(model, allow_none=False, attr='name'): +def get_model_choices(model, allow_none=False, attr='name', query=None): """Return a list of (value, label)""" choices = [] if allow_none: choices = [(None, '')] - choices.extend([(str(instance.id), getattr(instance, attr)) for instance in model.query.all()]) + if query is None: + query = model.query + choices.extend([(str(instance.id), getattr(instance, attr)) for instance in query.all()]) return choices