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

Add CSV import/export for the Software LCM models. #18

Merged
merged 2 commits into from Sep 28, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions nautobot_device_lifecycle_mgmt/forms.py
@@ -1,6 +1,7 @@
"""Forms implementation for the LifeCycle Management plugin."""
import logging
from django import forms
from django.contrib.contenttypes.models import ContentType

from nautobot.utilities.forms import BootstrapMixin, DatePicker, DynamicModelMultipleChoiceField
from nautobot.dcim.models import Device, DeviceType, InventoryItem, Platform
Expand All @@ -16,6 +17,7 @@
StaticSelect2,
BOOLEAN_WITH_BLANK_CHOICES,
add_blank_choice,
CSVModelChoiceField,
)
from nautobot_device_lifecycle_mgmt.choices import ContractTypeChoices, CurrencyChoices, PoCTypeChoices, CountryCodes
from nautobot_device_lifecycle_mgmt.models import (
Expand Down Expand Up @@ -214,6 +216,23 @@ class Meta:
}


class SoftwareLCMCSVForm(CustomFieldModelCSVForm):
"""Form for bulk creating SoftwareLCM objects."""

device_platform = CSVModelChoiceField(
queryset=Platform.objects.all(),
required=True,
to_field_name="slug",
help_text="Device platform",
)

class Meta:
"""Meta attributes for the SoftwareLCMCSVForm class."""

model = SoftwareLCM
fields = SoftwareLCM.csv_headers


class ValidatedSoftwareLCMForm(BootstrapMixin, CustomFieldModelForm, RelationshipModelForm):
"""ValidatedSoftwareLCM creation/edit form."""

Expand Down Expand Up @@ -331,6 +350,23 @@ class Meta:
]


class ValidatedSoftwareLCMCSVForm(CustomFieldModelCSVForm):
"""Form for bulk creating ValidatedSoftwareLCM objects."""

assigned_to_content_type = CSVModelChoiceField(
queryset=ContentType.objects.all(),
required=True,
to_field_name="model",
help_text="Assigned to object type",
)

class Meta:
"""Meta attributes for the ValidatedSoftwareLCM class."""

model = ValidatedSoftwareLCM
fields = ValidatedSoftwareLCM.csv_headers


class ContractLCMForm(BootstrapMixin, CustomFieldModelForm, RelationshipModelForm):
"""Device LifeCycle Contracts creation/edit form."""

Expand Down
6 changes: 3 additions & 3 deletions nautobot_device_lifecycle_mgmt/models.py
Expand Up @@ -204,7 +204,7 @@ def get_absolute_url(self):
def to_csv(self):
"""Return fields for bulk view."""
return (
self.device_platform,
self.device_platform.slug,
self.version,
self.alias,
self.release_date,
Expand Down Expand Up @@ -288,8 +288,8 @@ def valid(self):
def to_csv(self):
"""Return fields for bulk view."""
return (
self.software,
self.assigned_to_content_type,
self.software.id,
self.assigned_to_content_type.model,
self.assigned_to_object_id,
self.start,
self.end,
Expand Down
28 changes: 28 additions & 0 deletions nautobot_device_lifecycle_mgmt/navigation.py
Expand Up @@ -54,6 +54,13 @@
"nautobot_device_lifecycle_mgmt.add_softwarelcm",
],
),
NavMenuButton(
link="plugins:nautobot_device_lifecycle_mgmt:softwarelcm_import",
title="Import",
icon_class="mdi mdi-database-import-outline",
button_class=ButtonColorChoices.BLUE,
permissions=["nautobot_device_lifecycle_mgmt.add_softwarelcm"],
),
),
permissions=[
"nautobot_device_lifecycle_mgmt.view_softwarelcm",
Expand All @@ -72,6 +79,13 @@
"nautobot_device_lifecycle_mgmt.add_validatedsoftwarelcm",
],
),
NavMenuButton(
link="plugins:nautobot_device_lifecycle_mgmt:validatedsoftwarelcm_import",
title="Import",
icon_class="mdi mdi-database-import-outline",
button_class=ButtonColorChoices.BLUE,
permissions=["nautobot_device_lifecycle_mgmt.add_validatedsoftwarelcm"],
),
),
permissions=[
"nautobot_device_lifecycle_mgmt.view_validatedsoftwarelcm",
Expand Down Expand Up @@ -246,6 +260,13 @@
color=ButtonColorChoices.GREEN,
permissions=["nautobot_device_lifecycle_mgmt.add_softwarelcm"],
),
PluginMenuButton(
link="plugins:nautobot_device_lifecycle_mgmt:softwarelcm_import",
title="Import",
icon_class="mdi mdi-database-import-outline",
color=ButtonColorChoices.BLUE,
permissions=["nautobot_device_lifecycle_mgmt.add_softwarelcm"],
),
),
),
PluginMenuItem(
Expand All @@ -259,6 +280,13 @@
color=ButtonColorChoices.GREEN,
permissions=["nautobot_device_lifecycle_mgmt.add_validatedsoftwarelcm"],
),
PluginMenuButton(
link="plugins:nautobot_device_lifecycle_mgmt:validatedsoftwarelcm_import",
title="Import",
icon_class="mdi mdi-database-import-outline",
color=ButtonColorChoices.BLUE,
permissions=["nautobot_device_lifecycle_mgmt.add_validatedsoftwarelcm"],
),
),
),
)
39 changes: 6 additions & 33 deletions nautobot_device_lifecycle_mgmt/urls.py
Expand Up @@ -40,6 +40,7 @@
name="softwarelcm_changelog",
kwargs={"model": SoftwareLCM},
),
path("software/import/", views.SoftwareLCMBulkImportView.as_view(), name="softwarelcm_import"),
# ValidatedSoftware
path("validated-software/", views.ValidatedSoftwareLCMListView.as_view(), name="validatedsoftwarelcm_list"),
path("validated-software/<uuid:pk>/", views.ValidatedSoftwareLCMView.as_view(), name="validatedsoftwarelcm"),
Expand All @@ -60,7 +61,11 @@
name="validatedsoftwarelcm_changelog",
kwargs={"model": ValidatedSoftwareLCM},
),
path("hardware/import/", views.HardwareLCMBulkImportView.as_view(), name="hardwarelcm_import"),
path(
"validated-software/import/",
views.ValidatedSoftwareLCMBulkImportView.as_view(),
name="validatedsoftwarelcm_import",
),
# Contract LifeCycle Management URLs
path("contract/", views.ContractLCMListView.as_view(), name="contractlcm_list"),
path("contract/<uuid:pk>/", views.ContractLCMView.as_view(), name="contractlcm"),
Expand Down Expand Up @@ -106,36 +111,4 @@
kwargs={"model": ContactLCM},
),
path("contact/import/", views.ContactLCMBulkImportView.as_view(), name="contactlcm_import"),
# Software LifeCycle Management URLs
path("software/", views.SoftwareLCMListView.as_view(), name="softwarelcm_list"),
path("software/<uuid:pk>/", views.SoftwareLCMView.as_view(), name="softwarelcm"),
path("software/add/", views.SoftwareLCMCreateView.as_view(), name="softwarelcm_add"),
path("software/<uuid:pk>/delete/", views.SoftwareLCMDeleteView.as_view(), name="softwarelcm_delete"),
path("software/<uuid:pk>/edit/", views.SoftwareLCMEditView.as_view(), name="softwarelcm_edit"),
path(
"software/<uuid:pk>/changelog/",
ObjectChangeLogView.as_view(),
name="softwarelcm_changelog",
kwargs={"model": SoftwareLCM},
),
# ValidatedSoftware
path("validated-software/", views.ValidatedSoftwareLCMListView.as_view(), name="validatedsoftwarelcm_list"),
path("validated-software/<uuid:pk>/", views.ValidatedSoftwareLCMView.as_view(), name="validatedsoftwarelcm"),
path("validated-software/add/", views.ValidatedSoftwareLCMEditView.as_view(), name="validatedsoftwarelcm_add"),
path(
"validated-software/<uuid:pk>/delete/",
views.ValidatedSoftwareLCMDeleteView.as_view(),
name="validatedsoftwarelcm_delete",
),
path(
"validated-software/<uuid:pk>/edit/",
views.ValidatedSoftwareLCMEditView.as_view(),
name="validatedsoftwarelcm_edit",
),
path(
"validated-software/<uuid:pk>/changelog/",
ObjectChangeLogView.as_view(),
name="validatedsoftwarelcm_changelog",
kwargs={"model": ValidatedSoftwareLCM},
),
]
24 changes: 24 additions & 0 deletions nautobot_device_lifecycle_mgmt/views.py
Expand Up @@ -24,8 +24,10 @@
HardwareLCMCSVForm,
SoftwareLCMForm,
SoftwareLCMFilterForm,
SoftwareLCMCSVForm,
ValidatedSoftwareLCMForm,
ValidatedSoftwareLCMFilterForm,
ValidatedSoftwareLCMCSVForm,
ContractLCMForm,
ContractLCMBulkEditForm,
ContractLCMFilterForm,
Expand Down Expand Up @@ -150,6 +152,8 @@ class SoftwareLCMListView(generic.ObjectListView):
action_buttons = (
"add",
"delete",
"import",
"export",
)


Expand Down Expand Up @@ -186,6 +190,15 @@ class SoftwareLCMEditView(generic.ObjectEditView):
default_return_url = URL.SoftwareLCM.View


class SoftwareLCMBulkImportView(generic.BulkImportView):
"""View for bulk import of SoftwareLCM."""

queryset = SoftwareLCM.objects.prefetch_related("device_platform")
model_form = SoftwareLCMCSVForm
table = SoftwareLCMTable
default_return_url = "plugins:nautobot_device_lifecycle_mgmt:softwarelcm_list"


class ValidatedSoftwareLCMListView(generic.ObjectListView):
"""ValidatedSoftware List view."""

Expand All @@ -196,6 +209,8 @@ class ValidatedSoftwareLCMListView(generic.ObjectListView):
action_buttons = (
"add",
"delete",
"import",
"export",
)


Expand Down Expand Up @@ -224,6 +239,15 @@ class ValidatedSoftwareLCMDeleteView(generic.ObjectDeleteView):
template_name = "nautobot_device_lifecycle_mgmt/validatedsoftwarelcm_delete.html"


class ValidatedSoftwareLCMBulkImportView(generic.BulkImportView):
"""View for bulk import of ValidatedSoftwareLCM."""

queryset = ValidatedSoftwareLCM.objects.all()
model_form = ValidatedSoftwareLCMCSVForm
table = ValidatedSoftwareLCMTable
default_return_url = "plugins:nautobot_device_lifecycle_mgmt:validatedsoftwarelcm_list"


# ---------------------------------------------------------------------------------
# Contract LifeCycle Management Views
# ---------------------------------------------------------------------------------
Expand Down