mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 09:28:38 -06:00
implement 3664
This commit is contained in:
parent
0d8fd45587
commit
5710f297f1
@ -173,12 +173,18 @@ class ConfigContextSerializer(ValidatedModelSerializer):
|
|||||||
required=False,
|
required=False,
|
||||||
many=True
|
many=True
|
||||||
)
|
)
|
||||||
|
tags = SerializedPKRelatedField(
|
||||||
|
queryset=Tag.objects.all(),
|
||||||
|
serializer=TagSerializer,
|
||||||
|
required=False,
|
||||||
|
many=True
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ConfigContext
|
model = ConfigContext
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'name', 'weight', 'description', 'is_active', 'regions', 'sites', 'roles', 'platforms',
|
'id', 'name', 'weight', 'description', 'is_active', 'regions', 'sites', 'roles', 'platforms',
|
||||||
'tenant_groups', 'tenants', 'data',
|
'tenant_groups', 'tenants', 'tags', 'data',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -180,6 +180,17 @@ class ConfigContextFilter(django_filters.FilterSet):
|
|||||||
to_field_name='slug',
|
to_field_name='slug',
|
||||||
label='Tenant (slug)',
|
label='Tenant (slug)',
|
||||||
)
|
)
|
||||||
|
tag_id = django_filters.ModelMultipleChoiceFilter(
|
||||||
|
field_name='tags',
|
||||||
|
queryset=Tag.objects.all(),
|
||||||
|
label='Tag',
|
||||||
|
)
|
||||||
|
tag = django_filters.ModelMultipleChoiceFilter(
|
||||||
|
field_name='tags__slug',
|
||||||
|
queryset=Tag.objects.all(),
|
||||||
|
to_field_name='slug',
|
||||||
|
label='Tag (slug)',
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ConfigContext
|
model = ConfigContext
|
||||||
|
@ -246,7 +246,7 @@ class ConfigContextForm(BootstrapMixin, forms.ModelForm):
|
|||||||
model = ConfigContext
|
model = ConfigContext
|
||||||
fields = [
|
fields = [
|
||||||
'name', 'weight', 'description', 'is_active', 'regions', 'sites', 'roles', 'platforms', 'tenant_groups',
|
'name', 'weight', 'description', 'is_active', 'regions', 'sites', 'roles', 'platforms', 'tenant_groups',
|
||||||
'tenants', 'data',
|
'tenants', 'tags', 'data',
|
||||||
]
|
]
|
||||||
widgets = {
|
widgets = {
|
||||||
'regions': APISelectMultiple(
|
'regions': APISelectMultiple(
|
||||||
@ -266,6 +266,9 @@ class ConfigContextForm(BootstrapMixin, forms.ModelForm):
|
|||||||
),
|
),
|
||||||
'tenants': APISelectMultiple(
|
'tenants': APISelectMultiple(
|
||||||
api_url="/api/tenancy/tenants/"
|
api_url="/api/tenancy/tenants/"
|
||||||
|
),
|
||||||
|
'tags': APISelectMultiple(
|
||||||
|
api_url="/api/extras/tags/"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -347,6 +350,14 @@ class ConfigContextFilterForm(BootstrapMixin, forms.Form):
|
|||||||
value_field="slug",
|
value_field="slug",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
tag = FilterChoiceField(
|
||||||
|
queryset=Tag.objects.all(),
|
||||||
|
to_field_name='slug',
|
||||||
|
widget=APISelectMultiple(
|
||||||
|
api_url="/api/extras/tags/",
|
||||||
|
value_field="slug",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
18
netbox/extras/migrations/0034_configcontext_tags.py
Normal file
18
netbox/extras/migrations/0034_configcontext_tags.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 2.2.6 on 2019-12-11 09:17
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('extras', '0033_graph_type_to_fk'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='configcontext',
|
||||||
|
name='tags',
|
||||||
|
field=models.ManyToManyField(blank=True, related_name='_configcontext_tags_+', to='extras.Tag'),
|
||||||
|
),
|
||||||
|
]
|
@ -673,6 +673,11 @@ class ConfigContext(models.Model):
|
|||||||
related_name='+',
|
related_name='+',
|
||||||
blank=True
|
blank=True
|
||||||
)
|
)
|
||||||
|
tags = models.ManyToManyField(
|
||||||
|
to='extras.Tag',
|
||||||
|
related_name='+',
|
||||||
|
blank=True
|
||||||
|
)
|
||||||
data = JSONField()
|
data = JSONField()
|
||||||
|
|
||||||
objects = ConfigContextQuerySet.as_manager()
|
objects = ConfigContextQuerySet.as_manager()
|
||||||
|
@ -39,6 +39,8 @@ class ConfigContextQuerySet(QuerySet):
|
|||||||
else:
|
else:
|
||||||
regions = []
|
regions = []
|
||||||
|
|
||||||
|
tags = obj.tags.slugs()
|
||||||
|
|
||||||
return self.filter(
|
return self.filter(
|
||||||
Q(regions__in=regions) | Q(regions=None),
|
Q(regions__in=regions) | Q(regions=None),
|
||||||
Q(sites=obj.site) | Q(sites=None),
|
Q(sites=obj.site) | Q(sites=None),
|
||||||
@ -46,5 +48,6 @@ class ConfigContextQuerySet(QuerySet):
|
|||||||
Q(platforms=obj.platform) | Q(platforms=None),
|
Q(platforms=obj.platform) | Q(platforms=None),
|
||||||
Q(tenant_groups=tenant_group) | Q(tenant_groups=None),
|
Q(tenant_groups=tenant_group) | Q(tenant_groups=None),
|
||||||
Q(tenants=obj.tenant) | Q(tenants=None),
|
Q(tenants=obj.tenant) | Q(tenants=None),
|
||||||
|
Q(tags__name__in=tags) | Q(tags=None),
|
||||||
is_active=True,
|
is_active=True,
|
||||||
).order_by('weight', 'name')
|
).order_by('weight', 'name')
|
||||||
|
@ -370,6 +370,8 @@ class ConfigContextTest(APITestCase):
|
|||||||
tenantgroup2 = TenantGroup.objects.create(name='Test Tenant Group 2', slug='test-tenant-group-2')
|
tenantgroup2 = TenantGroup.objects.create(name='Test Tenant Group 2', slug='test-tenant-group-2')
|
||||||
tenant1 = Tenant.objects.create(name='Test Tenant 1', slug='test-tenant-1')
|
tenant1 = Tenant.objects.create(name='Test Tenant 1', slug='test-tenant-1')
|
||||||
tenant2 = Tenant.objects.create(name='Test Tenant 2', slug='test-tenant-2')
|
tenant2 = Tenant.objects.create(name='Test Tenant 2', slug='test-tenant-2')
|
||||||
|
tag1 = Tag.objects.create(name='Test Tag 1', slug='test-ta-1')
|
||||||
|
tag2 = Tag.objects.create(name='Test Tag 2', slug='test-ta-2')
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'name': 'Test Config Context 4',
|
'name': 'Test Config Context 4',
|
||||||
@ -380,6 +382,7 @@ class ConfigContextTest(APITestCase):
|
|||||||
'platforms': [platform1.pk, platform2.pk],
|
'platforms': [platform1.pk, platform2.pk],
|
||||||
'tenant_groups': [tenantgroup1.pk, tenantgroup2.pk],
|
'tenant_groups': [tenantgroup1.pk, tenantgroup2.pk],
|
||||||
'tenants': [tenant1.pk, tenant2.pk],
|
'tenants': [tenant1.pk, tenant2.pk],
|
||||||
|
'tags': [tag1.pk, tag2.pk],
|
||||||
'data': {'foo': 'XXX'}
|
'data': {'foo': 'XXX'}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -402,6 +405,8 @@ class ConfigContextTest(APITestCase):
|
|||||||
self.assertEqual(tenantgroup2.pk, data['tenant_groups'][1])
|
self.assertEqual(tenantgroup2.pk, data['tenant_groups'][1])
|
||||||
self.assertEqual(tenant1.pk, data['tenants'][0])
|
self.assertEqual(tenant1.pk, data['tenants'][0])
|
||||||
self.assertEqual(tenant2.pk, data['tenants'][1])
|
self.assertEqual(tenant2.pk, data['tenants'][1])
|
||||||
|
self.assertEqual(tag1.pk, data['tags'][0])
|
||||||
|
self.assertEqual(tag2.pk, data['tags'][1])
|
||||||
self.assertEqual(configcontext4.data, data['data'])
|
self.assertEqual(configcontext4.data, data['data'])
|
||||||
|
|
||||||
def test_create_configcontext_bulk(self):
|
def test_create_configcontext_bulk(self):
|
||||||
|
@ -162,6 +162,20 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Tags</td>
|
||||||
|
<td>
|
||||||
|
{% if configcontext.tags.all %}
|
||||||
|
<ul>
|
||||||
|
{% for tag in configcontext.tags.all %}
|
||||||
|
<li><a href="{{ tag.get_absolute_url }}">{{ tag }}</a></li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% else %}
|
||||||
|
<span class="text-muted">None</span>
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
{% render_field form.platforms %}
|
{% render_field form.platforms %}
|
||||||
{% render_field form.tenant_groups %}
|
{% render_field form.tenant_groups %}
|
||||||
{% render_field form.tenants %}
|
{% render_field form.tenants %}
|
||||||
|
{% render_field form.tags %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="panel panel-default">
|
<div class="panel panel-default">
|
||||||
|
@ -144,7 +144,7 @@ class ObjectListView(View):
|
|||||||
table.columns.show('pk')
|
table.columns.show('pk')
|
||||||
|
|
||||||
# Construct queryset for tags list
|
# Construct queryset for tags list
|
||||||
if hasattr(model, 'tags'):
|
if hasattr(model, 'tags') and type(model.tags).__name__ is not 'ManyToManyDescriptor':
|
||||||
tags = model.tags.annotate(count=Count('extras_taggeditem_items')).order_by('name')
|
tags = model.tags.annotate(count=Count('extras_taggeditem_items')).order_by('name')
|
||||||
else:
|
else:
|
||||||
tags = None
|
tags = None
|
||||||
|
Loading…
Reference in New Issue
Block a user