Skip to content

Commit

Permalink
Allow import/export of regions (netbox-community#1347)
Browse files Browse the repository at this point in the history
  • Loading branch information
paravoid committed Jul 17, 2017
1 parent d73ea54 commit 4df9e8d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 1 deletion.
22 changes: 22 additions & 0 deletions netbox/dcim/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ class Meta:
fields = ['parent', 'name', 'slug']


class RegionCSVForm(forms.ModelForm):
parent = forms.ModelChoiceField(
queryset=Region.objects.all(),
required=False,
to_field_name='name',
help_text='Name of parent region',
error_messages={
'invalid_choice': 'Region not found.',
}
)

class Meta:
model = Region
fields = [
'name', 'slug', 'parent',
]
help_texts = {
'name': 'Region name',
'slug': 'URL-friendly slug',
}


#
# Sites
#
Expand Down
11 changes: 11 additions & 0 deletions netbox/dcim/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class Region(MPTTModel):
name = models.CharField(max_length=50, unique=True)
slug = models.SlugField(unique=True)

csv_headers = [
'name', 'slug', 'parent',
]

class MPTTMeta:
order_insertion_by = ['name']

Expand All @@ -52,6 +56,13 @@ def __str__(self):
def get_absolute_url(self):
return "{}?region={}".format(reverse('dcim:site_list'), self.slug)

def to_csv(self):
return csv_format([
self.name,
self.slug,
self.parent if self.parent else None,
])


#
# Sites
Expand Down
1 change: 1 addition & 0 deletions netbox/dcim/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# Regions
url(r'^regions/$', views.RegionListView.as_view(), name='region_list'),
url(r'^regions/add/$', views.RegionCreateView.as_view(), name='region_add'),
url(r'^regions/import/$', views.RegionBulkImportView.as_view(), name='region_import'),
url(r'^regions/delete/$', views.RegionBulkDeleteView.as_view(), name='region_bulk_delete'),
url(r'^regions/(?P<pk>\d+)/edit/$', views.RegionEditView.as_view(), name='region_edit'),

Expand Down
7 changes: 7 additions & 0 deletions netbox/dcim/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ class RegionEditView(RegionCreateView):
permission_required = 'dcim.change_region'


class RegionBulkImportView(PermissionRequiredMixin, BulkImportView):
permission_required = 'dcim.add_region'
model_form = forms.RegionCSVForm
table = tables.RegionTable
default_return_url = 'dcim:region_list'


class RegionBulkDeleteView(PermissionRequiredMixin, BulkDeleteView):
permission_required = 'dcim.delete_region'
cls = Region
Expand Down
3 changes: 2 additions & 1 deletion netbox/extras/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@

# Models which support export templates
EXPORTTEMPLATE_MODELS = [
'site', 'rack', 'device', 'consoleport', 'powerport', 'interfaceconnection', # DCIM
'site', 'region', 'rack', 'device', # DCIM
'consoleport', 'powerport', 'interfaceconnection', # DCIM
'aggregate', 'prefix', 'ipaddress', 'vlan', # IPAM
'provider', 'circuit', # Circuits
'tenant', # Tenants
Expand Down
1 change: 1 addition & 0 deletions netbox/templates/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<li><a href="{% url 'dcim:region_list' %}"><i class="fa fa-search" aria-hidden="true"></i> Regions</a></li>
{% if perms.dcim.add_region %}
<li><a href="{% url 'dcim:region_add' %}"><i class="fa fa-plus" aria-hidden="true"></i> Add a Region</a></li>
<li><a href="{% url 'dcim:region_import' %}"><i class="fa fa-download" aria-hidden="true"></i> Import Regions</a></li>
{% endif %}
<li class="divider"></li>
<li><a href="{% url 'tenancy:tenant_list' %}"><i class="fa fa-search" aria-hidden="true"></i> Tenants</a></li>
Expand Down
5 changes: 5 additions & 0 deletions netbox/templates/dcim/region_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
<span class="fa fa-plus" aria-hidden="true"></span>
Add a region
</a>
<a href="{% url 'dcim:region_import' %}" class="btn btn-info">
<span class="fa fa-download" aria-hidden="true"></span>
Import regions
</a>
{% endif %}
{% include 'inc/export_button.html' with obj_type='regions' %}
</div>
<h1>{{ block.title }}</h1>
<div class="row">
Expand Down

0 comments on commit 4df9e8d

Please sign in to comment.