Skip to content

Commit

Permalink
Closes #1444: Add field to Rack model
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Nov 2, 2018
1 parent d4ec309 commit 4ffe186
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ v2.5.0 (FUTURE)

## Enhancements

* [#1444](https://github.com/digitalocean/netbox/issues/1444) - Added an `asset_tag` field for racks
* [#2000](https://github.com/digitalocean/netbox/issues/2000) - Dropped support for Python 2
* [#2104](https://github.com/digitalocean/netbox/issues/2104) - Added a `status` field for racks
* [#2292](https://github.com/digitalocean/netbox/issues/2292) - Removed the deprecated UserAction model
Expand Down
5 changes: 3 additions & 2 deletions netbox/dcim/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ class RackSerializer(TaggitSerializer, CustomFieldModelSerializer):
class Meta:
model = Rack
fields = [
'id', 'name', 'facility_id', 'display_name', 'site', 'group', 'tenant', 'status', 'role', 'serial', 'type',
'width', 'u_height', 'desc_units', 'comments', 'tags', 'custom_fields', 'created', 'last_updated',
'id', 'name', 'facility_id', 'display_name', 'site', 'group', 'tenant', 'status', 'role', 'serial',
'asset_tag', 'type', 'width', 'u_height', 'desc_units', 'comments', 'tags', 'custom_fields', 'created',
'last_updated',
]
# Omit the UniqueTogetherValidator that would be automatically added to validate (group, facility_id). This
# prevents facility_id from being interpreted as a required field.
Expand Down
4 changes: 3 additions & 1 deletion netbox/dcim/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,14 @@ class RackFilter(CustomFieldFilterSet, django_filters.FilterSet):
to_field_name='slug',
label='Role (slug)',
)
asset_tag = NullableCharFieldFilter()
tag = django_filters.CharFilter(
name='tags__slug',
)

class Meta:
model = Rack
fields = ['name', 'serial', 'type', 'width', 'u_height', 'desc_units']
fields = ['name', 'serial', 'asset_tag', 'type', 'width', 'u_height', 'desc_units']

def search(self, queryset, name, value):
if not value.strip():
Expand All @@ -209,6 +210,7 @@ def search(self, queryset, name, value):
Q(name__icontains=value) |
Q(facility_id__icontains=value) |
Q(serial__icontains=value.strip()) |
Q(asset_tag__icontains=value.strip()) |
Q(comments__icontains=value)
)

Expand Down
10 changes: 7 additions & 3 deletions netbox/dcim/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ class RackForm(BootstrapMixin, TenancyForm, CustomFieldForm):
class Meta:
model = Rack
fields = [
'site', 'group', 'name', 'facility_id', 'tenant_group', 'tenant', 'status', 'role', 'serial', 'type',
'width', 'u_height', 'desc_units', 'comments', 'tags',
'site', 'group', 'name', 'facility_id', 'tenant_group', 'tenant', 'status', 'role', 'serial', 'asset_tag',
'type', 'width', 'u_height', 'desc_units', 'comments', 'tags',
]
help_texts = {
'site': "The site at which the rack exists",
Expand Down Expand Up @@ -437,6 +437,10 @@ class RackBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditFor
required=False,
label='Serial Number'
)
asset_tag = forms.CharField(
max_length=50,
required=False
)
type = forms.ChoiceField(
choices=add_blank_choice(RACK_TYPE_CHOICES),
required=False
Expand All @@ -459,7 +463,7 @@ class RackBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditFor
)

class Meta:
nullable_fields = ['group', 'tenant', 'role', 'serial', 'comments']
nullable_fields = ['group', 'tenant', 'role', 'serial', 'asset_tag', 'comments']


class RackFilterForm(BootstrapMixin, CustomFieldFilterForm):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 2.0.9 on 2018-11-01 19:44

from django.db import migrations, models

import utilities.fields


class Migration(migrations.Migration):

Expand All @@ -15,4 +15,9 @@ class Migration(migrations.Migration):
name='status',
field=models.PositiveSmallIntegerField(default=3),
),
migrations.AddField(
model_name='rack',
name='asset_tag',
field=utilities.fields.NullableCharField(blank=True, max_length=50, null=True, unique=True),
),
]
13 changes: 11 additions & 2 deletions netbox/dcim/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,14 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
blank=True,
verbose_name='Serial number'
)
asset_tag = NullableCharField(
max_length=50,
blank=True,
null=True,
unique=True,
verbose_name='Asset tag',
help_text='A unique tag used to identify this rack'
)
type = models.PositiveSmallIntegerField(
choices=RACK_TYPE_CHOICES,
blank=True,
Expand Down Expand Up @@ -518,8 +526,8 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
tags = TaggableManager()

csv_headers = [
'site', 'group_name', 'name', 'facility_id', 'tenant', 'status', 'role', 'type', 'serial', 'width', 'u_height',
'desc_units', 'comments',
'site', 'group_name', 'name', 'facility_id', 'tenant', 'status', 'role', 'type', 'serial', 'asset_tag', 'width',
'u_height', 'desc_units', 'comments',
]

class Meta:
Expand Down Expand Up @@ -579,6 +587,7 @@ def to_csv(self):
self.role.name if self.role else None,
self.get_type_display() if self.type else None,
self.serial,
self.asset_tag,
self.width,
self.u_height,
self.desc_units,
Expand Down
10 changes: 10 additions & 0 deletions netbox/templates/dcim/rack.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ <h1>{% block title %}Rack {{ rack }}{% endblock %}</h1>
{% endif %}
</td>
</tr>
<tr>
<td>Asset Tag</td>
<td>
{% if rack.asset_tag %}
<span>{{ rack.asset_tag }}</span>
{% else %}
<span class="text-muted">N/A</span>
{% endif %}
</td>
</tr>
<tr>
<td>Devices</td>
<td>
Expand Down
1 change: 1 addition & 0 deletions netbox/templates/dcim/rack_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{% render_field form.status %}
{% render_field form.role %}
{% render_field form.serial %}
{% render_field form.asset_tag %}
</div>
</div>
<div class="panel panel-default">
Expand Down

0 comments on commit 4ffe186

Please sign in to comment.