Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes: #13918 - Add facility field #15456

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/models/dcim/location.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ The location's operational status.

!!! tip
Additional statuses may be defined by setting `Location.status` under the [`FIELD_CHOICES`](../../configuration/data-validation.md#field_choices) configuration parameter.

### Facility

Data center or facility designation for identifying the location.
4 changes: 2 additions & 2 deletions netbox/dcim/api/serializers_/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class LocationSerializer(NestedGroupModelSerializer):
class Meta:
model = Location
fields = [
'id', 'url', 'display', 'name', 'slug', 'site', 'parent', 'status', 'tenant', 'description', 'tags',
'custom_fields', 'created', 'last_updated', 'rack_count', 'device_count', '_depth',
'id', 'url', 'display', 'name', 'slug', 'site', 'parent', 'status', 'tenant', 'facility', 'description',
'tags', 'custom_fields', 'created', 'last_updated', 'rack_count', 'device_count', '_depth',
]
brief_fields = ('id', 'url', 'display', 'name', 'slug', 'description', 'rack_count', '_depth')
3 changes: 2 additions & 1 deletion netbox/dcim/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,14 @@ class LocationFilterSet(TenancyFilterSet, ContactModelFilterSet, OrganizationalM

class Meta:
model = Location
fields = ('id', 'name', 'slug', 'status', 'description')
fields = ('id', 'name', 'slug', 'status', 'facility', 'description')

def search(self, queryset, name, value):
if not value.strip():
return queryset
return queryset.filter(
Q(name__icontains=value) |
Q(facility__icontains=value) |
Q(description__icontains=value)
)

Expand Down
2 changes: 1 addition & 1 deletion netbox/dcim/forms/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class LocationImportForm(NetBoxModelImportForm):

class Meta:
model = Location
fields = ('site', 'parent', 'name', 'slug', 'status', 'tenant', 'description', 'tags')
fields = ('site', 'parent', 'name', 'slug', 'status', 'tenant', 'facility', 'description', 'tags')

def __init__(self, data=None, *args, **kwargs):
super().__init__(data, *args, **kwargs)
Expand Down
4 changes: 2 additions & 2 deletions netbox/dcim/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ class LocationForm(TenancyForm, NetBoxModelForm):
slug = SlugField()

fieldsets = (
(_('Location'), ('site', 'parent', 'name', 'slug', 'status', 'description', 'tags')),
(_('Location'), ('site', 'parent', 'name', 'slug', 'status', 'facility', 'description', 'tags')),
(_('Tenancy'), ('tenant_group', 'tenant')),
)

class Meta:
model = Location
fields = (
'site', 'parent', 'name', 'slug', 'status', 'description', 'tenant_group', 'tenant', 'tags',
'site', 'parent', 'name', 'slug', 'status', 'description', 'tenant_group', 'tenant', 'facility', 'tags',
)


Expand Down
18 changes: 18 additions & 0 deletions netbox/dcim/migrations/0186_location_facility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.4 on 2024-03-17 02:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dcim', '0185_gfk_indexes'),
]

operations = [
migrations.AddField(
model_name='location',
name='facility',
field=models.CharField(blank=True, max_length=50),
),
]
8 changes: 7 additions & 1 deletion netbox/dcim/models/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ class Location(ContactsMixin, ImageAttachmentsMixin, NestedGroupModel):
blank=True,
null=True
)
facility = models.CharField(
verbose_name=_('facility'),
max_length=50,
blank=True,
help_text=_('Local facility ID or description')
)

# Generic relations
vlan_groups = GenericRelation(
Expand All @@ -284,7 +290,7 @@ class Location(ContactsMixin, ImageAttachmentsMixin, NestedGroupModel):
related_query_name='location'
)

clone_fields = ('site', 'parent', 'status', 'tenant', 'description')
clone_fields = ('site', 'parent', 'status', 'tenant', 'facility', 'description')
prerequisite_models = (
'dcim.Site',
)
Expand Down
3 changes: 2 additions & 1 deletion netbox/dcim/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ class LocationIndex(SearchIndex):
model = models.Location
fields = (
('name', 100),
('facility', 100),
('slug', 110),
('description', 500),
)
display_attrs = ('site', 'status', 'tenant', 'description')
display_attrs = ('site', 'status', 'tenant', 'facility', 'description')


@register_search
Expand Down
8 changes: 5 additions & 3 deletions netbox/dcim/tables/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ class LocationTable(TenancyColumnsMixin, ContactsColumnMixin, NetBoxTable):
class Meta(NetBoxTable.Meta):
model = Location
fields = (
'pk', 'id', 'name', 'site', 'status', 'tenant', 'tenant_group', 'rack_count', 'device_count', 'description',
'slug', 'contacts', 'tags', 'actions', 'created', 'last_updated',
'pk', 'id', 'name', 'site', 'status', 'facility', 'tenant', 'tenant_group', 'rack_count', 'device_count',
'description', 'slug', 'contacts', 'tags', 'actions', 'created', 'last_updated',
)
default_columns = (
'pk', 'name', 'site', 'status', 'facility', 'tenant', 'rack_count', 'device_count', 'description'
)
default_columns = ('pk', 'name', 'site', 'status', 'tenant', 'rack_count', 'device_count', 'description')
10 changes: 7 additions & 3 deletions netbox/dcim/tests/test_filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ def setUpTestData(cls):
location.save()

locations = (
Location(name='Location 1A', slug='location-1a', site=sites[0], parent=parent_locations[0], status=LocationStatusChoices.STATUS_PLANNED, description='foobar1'),
Location(name='Location 2A', slug='location-2a', site=sites[1], parent=parent_locations[1], status=LocationStatusChoices.STATUS_STAGING, description='foobar2'),
Location(name='Location 3A', slug='location-3a', site=sites[2], parent=parent_locations[2], status=LocationStatusChoices.STATUS_DECOMMISSIONING, description='foobar3'),
Location(name='Location 1A', slug='location-1a', site=sites[0], parent=parent_locations[0], status=LocationStatusChoices.STATUS_PLANNED, facility='Facility 1', description='foobar1'),
Location(name='Location 2A', slug='location-2a', site=sites[1], parent=parent_locations[1], status=LocationStatusChoices.STATUS_STAGING, facility='Facility 2', description='foobar2'),
Location(name='Location 3A', slug='location-3a', site=sites[2], parent=parent_locations[2], status=LocationStatusChoices.STATUS_DECOMMISSIONING, facility='Facility 3', description='foobar3'),
)
for location in locations:
location.save()
Expand Down Expand Up @@ -390,6 +390,10 @@ def test_status(self):
params = {'status': [LocationStatusChoices.STATUS_PLANNED, LocationStatusChoices.STATUS_STAGING]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_facility(self):
params = {'facility': ['Facility 1', 'Facility 2']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_description(self):
params = {'description': ['foobar1', 'foobar2']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
Expand Down
1 change: 1 addition & 0 deletions netbox/dcim/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def setUpTestData(cls):
'slug': 'location-x',
'site': site.pk,
'status': LocationStatusChoices.STATUS_PLANNED,
'facility': 'Facility X',
'tenant': tenant.pk,
'description': 'A new location',
'tags': [t.pk for t in tags],
Expand Down
4 changes: 4 additions & 0 deletions netbox/templates/dcim/location.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ <h5 class="card-header">{% trans "Location" %}</h5>
{{ object.tenant|linkify|placeholder }}
</td>
</tr>
<tr>
<th scope="row">{% trans "Facility" %}</th>
<td>{{ object.facility|placeholder }}</td>
</tr>
</table>
</div>
{% include 'inc/panels/tags.html' %}
Expand Down
Loading