Skip to content

Commit

Permalink
Fixes: #13918 - Add facilities field to Location model.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSheps committed Mar 16, 2024
1 parent 78bd7de commit f4fbd30
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 14 deletions.
4 changes: 4 additions & 0 deletions docs/models/dcim/location.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ A unique URL-friendly identifier. (This value can be used for filtering.)

The location's operational status.

### Facility

Data center or facility designation for identifying the site.

!!! tip
Additional statuses may be defined by setting `Location.status` under the [`FIELD_CHOICES`](../../configuration/data-validation.md#field_choices) configuration parameter.
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')
2 changes: 1 addition & 1 deletion netbox/dcim/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ 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():
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
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

0 comments on commit f4fbd30

Please sign in to comment.