Skip to content

Commit

Permalink
Merge pull request #67 from mlebreuil/bulk-import-contract-assignement
Browse files Browse the repository at this point in the history
Add contract assignement import possibility
  • Loading branch information
mlebreuil authored Jun 18, 2023
2 parents fe62c89 + 9ba4d71 commit 70ff264
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions assignement_import.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
content_type,object_id,contract
devices.device,1,3
17 changes: 16 additions & 1 deletion src/netbox_contract/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django import forms
from django.contrib.contenttypes.models import ContentType
import django_filters
from netbox.forms import NetBoxModelForm, NetBoxModelFilterSetForm, NetBoxModelBulkEditForm, NetBoxModelImportForm
from utilities.forms.fields import CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, MultipleChoiceField, CSVModelChoiceField, SlugField
from utilities.forms.fields import CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, MultipleChoiceField, CSVModelChoiceField, SlugField, CSVContentTypeField
from utilities.forms.widgets import DatePicker
from extras.filters import TagFilter
from circuits.models import Circuit
Expand Down Expand Up @@ -181,6 +182,7 @@ class ContractAssignementForm(NetBoxModelForm):
contract=DynamicModelChoiceField(
queryset=Contract.objects.all()
)

class Meta:
model = ContractAssignement
fields = ['content_type', 'object_id', 'contract','tags']
Expand All @@ -194,3 +196,16 @@ class ContractAssignementFilterSetForm(NetBoxModelFilterSetForm):
contract=DynamicModelChoiceField(
queryset=Contract.objects.all()
)

class ContractAssignementImportForm(NetBoxModelImportForm):
content_type = CSVContentTypeField(
queryset=ContentType.objects.all(),
help_text="Content Type in the form <app>.<model>"
)
contract = CSVModelChoiceField(
queryset=Contract.objects.all(),
help_text="Contract id"
)
class Meta:
model = ContractAssignement
fields = ['content_type', 'object_id', 'contract','tags']
7 changes: 6 additions & 1 deletion src/netbox_contract/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@
buttons=serviceprovider_buttons,
permissions=['netbox_contract.view_serviceprovider']
)
contract_assignemnt_menu_item = PluginMenuItem(
link='plugins:netbox_contract:contractassignement_list',
link_text='Contract assignements',
permissions=['netbox_contract.view_contractassignement']
)

items = (contract_menu_item,invoices_menu_item,service_provider_menu_item)
items = (contract_menu_item,invoices_menu_item,service_provider_menu_item, contract_assignemnt_menu_item)

if plugin_settings.get("top_level_menu"):
menu = PluginMenu(
Expand Down
1 change: 1 addition & 0 deletions src/netbox_contract/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
# Contract assignements
path('assignements/', views.ContractAssignementListView.as_view(), name='contractassignement_list'),
path('assignements/add/', views.ContractAssignementEditView.as_view(), name='contractassignement_add'),
path('assignements/import/', views.ContractAssignementBulkImportView.as_view(), name='contractassignement_import'),
path('assignements/<int:pk>/', views.ContractAssignementView.as_view(), name='contractassignement'),
path('assignements/<int:pk>/edit/', views.ContractAssignementEditView.as_view(), name='contractassignement_edit'),
path('assignements/<int:pk>/delete/', views.ContractAssignementDeleteView.as_view(), name='contractassignement_delete'),
Expand Down
7 changes: 6 additions & 1 deletion src/netbox_contract/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ContractAssignementEditView(generic.ObjectEditView):
form = forms.ContractAssignementForm

def alter_object(self, instance, request, args, kwargs):
if not instance.pk:
if not instance.pk and kwargs:
# Assign the object based on URL kwargs
content_type = get_object_or_404(ContentType, pk=request.GET.get('content_type'))
instance.object = get_object_or_404(content_type.model_class(), pk=request.GET.get('object_id'))
Expand All @@ -75,6 +75,11 @@ def get_extra_addanother_params(self, request):
class ContractAssignementDeleteView(generic.ObjectDeleteView):
queryset = models.ContractAssignement.objects.all()

class ContractAssignementBulkImportView(generic.BulkImportView):
queryset = models.ContractAssignement.objects.all()
model_form = forms.ContractAssignementImportForm
table = tables.ContractAssignementListTable

# Contract views

class ContractView(generic.ObjectView):
Expand Down

0 comments on commit 70ff264

Please sign in to comment.